Use gophercloud.Result in objectstorage.
diff --git a/openstack/objectstorage/v1/accounts/requests.go b/openstack/objectstorage/v1/accounts/requests.go
index 55fcb05..2706197 100644
--- a/openstack/objectstorage/v1/accounts/requests.go
+++ b/openstack/objectstorage/v1/accounts/requests.go
@@ -46,7 +46,7 @@
MoreHeaders: h,
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -80,8 +80,7 @@
}
// Update is a function that creates, updates, or deletes an account's metadata.
-// To extract the headers returned, call the ExtractHeaders method on the
-// UpdateResult.
+// To extract the headers returned, call the Extract method on the UpdateResult.
func Update(c *gophercloud.ServiceClient, opts UpdateOptsBuilder) UpdateResult {
var res UpdateResult
h := c.Provider.AuthenticatedHeaders()
@@ -101,7 +100,7 @@
MoreHeaders: h,
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
diff --git a/openstack/objectstorage/v1/accounts/requests_test.go b/openstack/objectstorage/v1/accounts/requests_test.go
index 0090eea..d109214 100644
--- a/openstack/objectstorage/v1/accounts/requests_test.go
+++ b/openstack/objectstorage/v1/accounts/requests_test.go
@@ -27,10 +27,8 @@
})
options := &UpdateOpts{Metadata: map[string]string{"gophercloud-test": "accounts"}}
- _, err := Update(fake.ServiceClient(), options).ExtractHeaders()
- if err != nil {
- t.Fatalf("Unable to update account: %v", err)
- }
+ _, err := Update(fake.ServiceClient(), options).Extract()
+ th.AssertNoErr(t, err)
}
func TestGetAccount(t *testing.T) {
diff --git a/openstack/objectstorage/v1/accounts/results.go b/openstack/objectstorage/v1/accounts/results.go
index 8ff8183..964a604 100644
--- a/openstack/objectstorage/v1/accounts/results.go
+++ b/openstack/objectstorage/v1/accounts/results.go
@@ -1,14 +1,15 @@
package accounts
import (
+ "net/http"
"strings"
- objectstorage "github.com/rackspace/gophercloud/openstack/objectstorage/v1"
+ "github.com/rackspace/gophercloud"
)
-// GetResult is returned from a call to the Get function. See v1.CommonResult.
+// GetResult is returned from a call to the Get function.
type GetResult struct {
- objectstorage.CommonResult
+ gophercloud.Result
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
@@ -19,7 +20,7 @@
}
metadata := make(map[string]string)
- for k, v := range gr.Resp.Header {
+ for k, v := range gr.Headers {
if strings.HasPrefix(k, "X-Account-Meta-") {
key := strings.TrimPrefix(k, "X-Account-Meta-")
metadata[key] = v[0]
@@ -28,7 +29,13 @@
return metadata, nil
}
-// UpdateResult is returned from a call to the Update function. See v1.CommonResult.
+// UpdateResult is returned from a call to the Update function.
type UpdateResult struct {
- objectstorage.CommonResult
+ gophercloud.Result
+}
+
+// Extract returns the unmodified HTTP headers and any error conditions encountered during the
+// metadata update.
+func (ur UpdateResult) Extract() (http.Header, error) {
+ return ur.Headers, ur.Err
}
diff --git a/openstack/objectstorage/v1/common.go b/openstack/objectstorage/v1/common.go
deleted file mode 100644
index 1a6c44a..0000000
--- a/openstack/objectstorage/v1/common.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package v1
-
-import (
- "net/http"
-)
-
-// CommonResult is a structure that contains the response and error of a call to an
-// object storage endpoint.
-type CommonResult struct {
- Resp *http.Response
- Err error
-}
-
-// ExtractHeaders will extract and return the headers from a *http.Response.
-func (cr CommonResult) ExtractHeaders() (http.Header, error) {
- if cr.Err != nil {
- return nil, cr.Err
- }
-
- var headers http.Header
- if cr.Err != nil {
- return headers, cr.Err
- }
- return cr.Resp.Header, nil
-}
diff --git a/openstack/objectstorage/v1/containers/requests.go b/openstack/objectstorage/v1/containers/requests.go
index ce3f540..004ef82 100644
--- a/openstack/objectstorage/v1/containers/requests.go
+++ b/openstack/objectstorage/v1/containers/requests.go
@@ -115,7 +115,7 @@
MoreHeaders: h,
OkCodes: []int{201, 204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -127,7 +127,7 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -186,7 +186,7 @@
MoreHeaders: h,
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -200,7 +200,7 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
diff --git a/openstack/objectstorage/v1/containers/requests_test.go b/openstack/objectstorage/v1/containers/requests_test.go
index 9562676..a134272 100644
--- a/openstack/objectstorage/v1/containers/requests_test.go
+++ b/openstack/objectstorage/v1/containers/requests_test.go
@@ -136,7 +136,7 @@
})
options := CreateOpts{ContentType: "application/json", Metadata: map[string]string{"foo": "bar"}}
- headers, err := Create(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ headers, err := Create(fake.ServiceClient(), "testContainer", options).Extract()
if err != nil {
t.Fatalf("Unexpected error creating container: %v", err)
}
@@ -154,7 +154,7 @@
w.WriteHeader(http.StatusNoContent)
})
- _, err := Delete(fake.ServiceClient(), "testContainer").ExtractHeaders()
+ _, err := Delete(fake.ServiceClient(), "testContainer").Extract()
if err != nil {
t.Fatalf("Unexpected error deleting container: %v", err)
}
@@ -172,7 +172,7 @@
})
options := &UpdateOpts{Metadata: map[string]string{"foo": "bar"}}
- _, err := Update(fake.ServiceClient(), "testContainer", options).ExtractHeaders()
+ _, err := Update(fake.ServiceClient(), "testContainer", options).Extract()
if err != nil {
t.Fatalf("Unexpected error updating container metadata: %v", err)
}
diff --git a/openstack/objectstorage/v1/containers/results.go b/openstack/objectstorage/v1/containers/results.go
index 227a9dc..3a6d3be 100644
--- a/openstack/objectstorage/v1/containers/results.go
+++ b/openstack/objectstorage/v1/containers/results.go
@@ -2,9 +2,10 @@
import (
"fmt"
+ "net/http"
"strings"
- objectstorage "github.com/rackspace/gophercloud/openstack/objectstorage/v1"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
@@ -98,7 +99,7 @@
// GetResult represents the result of a get operation.
type GetResult struct {
- objectstorage.CommonResult
+ gophercloud.Result
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
@@ -108,7 +109,7 @@
return nil, gr.Err
}
metadata := make(map[string]string)
- for k, v := range gr.Resp.Header {
+ for k, v := range gr.Headers {
if strings.HasPrefix(k, "X-Container-Meta-") {
key := strings.TrimPrefix(k, "X-Container-Meta-")
metadata[key] = v[0]
@@ -117,23 +118,32 @@
return metadata, nil
}
+type headerResult struct {
+ gophercloud.Result
+}
+
+// Extract pulls the unmodified headers from a Create, Update, or Delete result.
+func (result headerResult) Extract() (http.Header, error) {
+ return result.Headers, result.Err
+}
+
// CreateResult represents the result of a create operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
// method on the result struct.
type CreateResult struct {
- objectstorage.CommonResult
+ headerResult
}
// UpdateResult represents the result of an update operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
// method on the result struct.
type UpdateResult struct {
- objectstorage.CommonResult
+ headerResult
}
// DeleteResult represents the result of a delete operation. To extract the
// the headers from the HTTP response, you can invoke the 'ExtractHeaders'
// method on the result struct.
type DeleteResult struct {
- objectstorage.CommonResult
+ headerResult
}
diff --git a/openstack/objectstorage/v1/objects/requests.go b/openstack/objectstorage/v1/objects/requests.go
index 3274e04..c97b4fe 100644
--- a/openstack/objectstorage/v1/objects/requests.go
+++ b/openstack/objectstorage/v1/objects/requests.go
@@ -3,6 +3,7 @@
import (
"fmt"
"io"
+ "io/ioutil"
"time"
"github.com/racker/perigee"
@@ -128,8 +129,13 @@
MoreHeaders: h,
OkCodes: []int{200},
})
+ defer resp.HttpResponse.Body.Close()
+ body, err := ioutil.ReadAll(resp.HttpResponse.Body)
+ res.Resp = map[string]interface{}{
+ "body": body,
+ }
res.Err = err
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
return res
}
@@ -214,7 +220,7 @@
MoreHeaders: h,
OkCodes: []int{201},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -270,7 +276,7 @@
MoreHeaders: h,
OkCodes: []int{201},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
return res
}
@@ -312,7 +318,7 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
@@ -357,8 +363,8 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{200, 204},
})
+ res.Headers = resp.HttpResponse.Header
res.Err = err
- res.Resp = &resp.HttpResponse
return res
}
@@ -414,7 +420,7 @@
MoreHeaders: h,
OkCodes: []int{202},
})
- res.Resp = &resp.HttpResponse
+ res.Headers = resp.HttpResponse.Header
res.Err = err
return res
}
diff --git a/openstack/objectstorage/v1/objects/requests_test.go b/openstack/objectstorage/v1/objects/requests_test.go
index 11d7c44..7147782 100644
--- a/openstack/objectstorage/v1/objects/requests_test.go
+++ b/openstack/objectstorage/v1/objects/requests_test.go
@@ -167,7 +167,7 @@
content := bytes.NewBufferString("Did gyre and gimble in the wabe")
options := &CreateOpts{ContentType: "application/json"}
- _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).ExtractHeaders()
+ _, err := Create(fake.ServiceClient(), "testContainer", "testObject", content, options).Extract()
if err != nil {
t.Fatalf("Unexpected error creating object: %v", err)
}
@@ -186,7 +186,7 @@
})
options := &CopyOpts{Destination: "/newTestContainer/newTestObject"}
- _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).ExtractHeaders()
+ _, err := Copy(fake.ServiceClient(), "testContainer", "testObject", options).Extract()
if err != nil {
t.Fatalf("Unexpected error copying object: %v", err)
}
@@ -203,7 +203,7 @@
w.WriteHeader(http.StatusNoContent)
})
- _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).ExtractHeaders()
+ _, err := Delete(fake.ServiceClient(), "testContainer", "testObject", nil).Extract()
if err != nil {
t.Fatalf("Unexpected error deleting object: %v", err)
}
@@ -221,7 +221,7 @@
w.WriteHeader(http.StatusAccepted)
})
- _, err := Update(fake.ServiceClient(), "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).ExtractHeaders()
+ _, err := Update(fake.ServiceClient(), "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).Extract()
if err != nil {
t.Fatalf("Unexpected error updating object metadata: %v", err)
}
diff --git a/openstack/objectstorage/v1/objects/results.go b/openstack/objectstorage/v1/objects/results.go
index 1dda7a3..cc3a8ed 100644
--- a/openstack/objectstorage/v1/objects/results.go
+++ b/openstack/objectstorage/v1/objects/results.go
@@ -2,10 +2,10 @@
import (
"fmt"
- "io/ioutil"
+ "net/http"
"strings"
- objectstorage "github.com/rackspace/gophercloud/openstack/objectstorage/v1"
+ "github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
@@ -98,27 +98,21 @@
// DownloadResult is a *http.Response that is returned from a call to the Download function.
type DownloadResult struct {
- objectstorage.CommonResult
+ gophercloud.Result
}
// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
// and returns the object's content.
func (dr DownloadResult) ExtractContent() ([]byte, error) {
if dr.Err != nil {
- return nil, nil
+ return nil, dr.Err
}
- var body []byte
- defer dr.Resp.Body.Close()
- body, err := ioutil.ReadAll(dr.Resp.Body)
- if err != nil {
- return body, fmt.Errorf("Error trying to read DownloadResult body: %v", err)
- }
- return body, nil
+ return dr.Resp["body"].([]byte), nil
}
// GetResult is a *http.Response that is returned from a call to the Get function.
type GetResult struct {
- objectstorage.CommonResult
+ gophercloud.Result
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
@@ -128,7 +122,7 @@
return nil, gr.Err
}
metadata := make(map[string]string)
- for k, v := range gr.Resp.Header {
+ for k, v := range gr.Headers {
if strings.HasPrefix(k, "X-Object-Meta-") {
key := strings.TrimPrefix(k, "X-Object-Meta-")
metadata[key] = v[0]
@@ -137,22 +131,32 @@
return metadata, nil
}
+type headerResult struct {
+ gophercloud.Result
+}
+
+// Extract returns the unmodified HTTP response headers from a Create, Update, or Delete call, as
+// well as any errors that occurred during the call.
+func (result headerResult) Extract() (http.Header, error) {
+ return result.Headers, result.Err
+}
+
// CreateResult represents the result of a create operation.
type CreateResult struct {
- objectstorage.CommonResult
+ headerResult
}
// UpdateResult represents the result of an update operation.
type UpdateResult struct {
- objectstorage.CommonResult
+ headerResult
}
// DeleteResult represents the result of a delete operation.
type DeleteResult struct {
- objectstorage.CommonResult
+ headerResult
}
// CopyResult represents the result of a copy operation.
type CopyResult struct {
- objectstorage.CommonResult
+ headerResult
}