add commonResult types for ExtractHeaders method
diff --git a/openstack/objectstorage/v1/objects/requests.go b/openstack/objectstorage/v1/objects/requests.go
index 900290c..bc21496 100644
--- a/openstack/objectstorage/v1/objects/requests.go
+++ b/openstack/objectstorage/v1/objects/requests.go
@@ -40,6 +40,8 @@
if !opts.Full {
headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
}
+ } else {
+ headers = map[string]string{"Accept": "text/plain", "Content-Type": "text/plain"}
}
createPage := func(r pagination.LastHTTPResponse) pagination.Page {
@@ -69,7 +71,7 @@
// To extract just the content, pass the DownloadResult response to the ExtractContent
// function.
func Download(c *gophercloud.ServiceClient, containerName, objectName string, opts *DownloadOpts) DownloadResult {
- var dr DownloadResult
+ var res DownloadResult
url := objectURL(c, containerName, objectName)
h := c.Provider.AuthenticatedHeaders()
@@ -77,8 +79,8 @@
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- dr.Err = err
- return dr
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -87,8 +89,8 @@
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- dr.Err = err
- return dr
+ res.Err = err
+ return res
}
url += query.String()
}
@@ -97,9 +99,9 @@
MoreHeaders: h,
OkCodes: []int{200},
})
- dr.Err = err
- dr.Resp = &resp.HttpResponse
- return dr
+ res.Err = err
+ res.Resp = &resp.HttpResponse
+ return res
}
// CreateOpts is a structure that holds parameters for creating an object.
@@ -123,7 +125,8 @@
}
// Create is a function that creates a new object or replaces an existing object.
-func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts *CreateOpts) error {
+func Create(c *gophercloud.ServiceClient, containerName, objectName string, content io.Reader, opts *CreateOpts) CreateResult {
+ var res CreateResult
var reqBody []byte
url := objectURL(c, containerName, objectName)
@@ -132,7 +135,8 @@
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return nil
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -145,8 +149,10 @@
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
+
url += query.String()
}
@@ -154,16 +160,19 @@
reqBody = make([]byte, 0)
_, err := content.Read(reqBody)
if err != nil {
- return err
+ res.Err = err
+ return res
}
}
- _, err := perigee.Request("PUT", url, perigee.Options{
+ resp, err := perigee.Request("PUT", url, perigee.Options{
ReqBody: reqBody,
MoreHeaders: h,
OkCodes: []int{201},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// CopyOpts is a structure that holds parameters for copying one object to another.
@@ -176,15 +185,18 @@
}
// Copy is a function that copies one object to another.
-func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts *CopyOpts) error {
+func Copy(c *gophercloud.ServiceClient, containerName, objectName string, opts *CopyOpts) CopyResult {
+ var res CopyResult
h := c.Provider.AuthenticatedHeaders()
if opts == nil {
- return fmt.Errorf("Required CopyOpts field 'Destination' not set.")
+ res.Err = fmt.Errorf("Required CopyOpts field 'Destination' not set.")
+ return res
}
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
for k, v := range headers {
h[k] = v
@@ -195,11 +207,12 @@
}
url := objectURL(c, containerName, objectName)
- _, err = perigee.Request("COPY", url, perigee.Options{
+ resp, err := perigee.Request("COPY", url, perigee.Options{
MoreHeaders: h,
OkCodes: []int{201},
})
- return err
+ res.Resp = &resp.HttpResponse
+ return res
}
// DeleteOpts is a structure that holds parameters for deleting an object.
@@ -208,22 +221,26 @@
}
// Delete is a function that deletes an object.
-func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts *DeleteOpts) error {
+func Delete(c *gophercloud.ServiceClient, containerName, objectName string, opts *DeleteOpts) DeleteResult {
+ var res DeleteResult
url := objectURL(c, containerName, objectName)
if opts != nil {
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- return err
+ res.Err = err
+ return res
}
url += query.String()
}
- _, err := perigee.Request("DELETE", url, perigee.Options{
+ resp, err := perigee.Request("DELETE", url, perigee.Options{
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{204},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
// GetOpts is a structure that holds parameters for getting an object's metadata.
@@ -235,14 +252,14 @@
// Get is a function that retrieves the metadata of an object. To extract just the custom
// metadata, pass the GetResult response to the ExtractMetadata function.
func Get(c *gophercloud.ServiceClient, containerName, objectName string, opts *GetOpts) GetResult {
- var gr GetResult
+ var res GetResult
url := objectURL(c, containerName, objectName)
if opts != nil {
query, err := gophercloud.BuildQueryString(opts)
if err != nil {
- gr.Err = err
- return gr
+ res.Err = err
+ return res
}
url += query.String()
}
@@ -251,9 +268,9 @@
MoreHeaders: c.Provider.AuthenticatedHeaders(),
OkCodes: []int{200, 204},
})
- gr.Err = err
- gr.Resp = &resp.HttpResponse
- return gr
+ res.Err = err
+ res.Resp = &resp.HttpResponse
+ return res
}
// UpdateOpts is a structure that holds parameters for updating, creating, or deleting an
@@ -269,13 +286,15 @@
}
// Update is a function that creates, updates, or deletes an object's metadata.
-func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts *UpdateOpts) error {
+func Update(c *gophercloud.ServiceClient, containerName, objectName string, opts *UpdateOpts) UpdateResult {
+ var res UpdateResult
h := c.Provider.AuthenticatedHeaders()
if opts != nil {
headers, err := gophercloud.BuildHeaders(opts)
if err != nil {
- return nil
+ res.Err = err
+ return res
}
for k, v := range headers {
@@ -288,9 +307,11 @@
}
url := objectURL(c, containerName, objectName)
- _, err := perigee.Request("POST", url, perigee.Options{
+ resp, err := perigee.Request("POST", url, perigee.Options{
MoreHeaders: h,
OkCodes: []int{202},
})
- return err
+ res.Resp = &resp.HttpResponse
+ res.Err = err
+ return res
}
diff --git a/openstack/objectstorage/v1/objects/requests_test.go b/openstack/objectstorage/v1/objects/requests_test.go
index 6747544..15956cd 100644
--- a/openstack/objectstorage/v1/objects/requests_test.go
+++ b/openstack/objectstorage/v1/objects/requests_test.go
@@ -180,7 +180,7 @@
client := serviceClient()
content := bytes.NewBufferString("Did gyre and gimble in the wabe")
- err := Create(client, "testContainer", "testObject", content, nil)
+ _, err := Create(client, "testContainer", "testObject", content, nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error creating object: %v", err)
}
@@ -199,7 +199,7 @@
})
client := serviceClient()
- err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"})
+ _, err := Copy(client, "testContainer", "testObject", &CopyOpts{Destination: "/newTestContainer/newTestObject"}).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error copying object: %v", err)
}
@@ -217,7 +217,7 @@
})
client := serviceClient()
- err := Delete(client, "testContainer", "testObject", nil)
+ _, err := Delete(client, "testContainer", "testObject", nil).ExtractHeaders()
if err != nil {
t.Fatalf("Unexpected error deleting object: %v", err)
}
@@ -236,7 +236,7 @@
})
client := serviceClient()
- err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata})
+ _, err := Update(client, "testContainer", "testObject", &UpdateOpts{Metadata: metadata}).ExtractHeaders()
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 cab0dfb..aaeb040 100644
--- a/openstack/objectstorage/v1/objects/results.go
+++ b/openstack/objectstorage/v1/objects/results.go
@@ -45,18 +45,6 @@
return names[len(names)-1], nil
}
-// DownloadResult is a *http.Response that is returned from a call to the Download function.
-type DownloadResult struct {
- Resp *http.Response
- Err error
-}
-
-// GetResult is a *http.Response that is returned from a call to the Get function.
-type GetResult struct {
- Resp *http.Response
- Err error
-}
-
// ExtractInfo is a function that takes a page of objects and returns their full information.
func ExtractInfo(page pagination.Page) ([]Object, error) {
untyped := page.(ObjectPage).Body.([]interface{})
@@ -106,6 +94,11 @@
}
}
+// DownloadResult is a *http.Response that is returned from a call to the Download function.
+type DownloadResult struct {
+ commonResult
+}
+
// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
// and returns the object's content.
func (dr DownloadResult) ExtractContent() ([]byte, error) {
@@ -121,6 +114,11 @@
return body, nil
}
+// GetResult is a *http.Response that is returned from a call to the Get function.
+type GetResult struct {
+ commonResult
+}
+
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
// and returns the custom metadata associated with the object.
func (gr GetResult) ExtractMetadata() (map[string]string, error) {
@@ -136,3 +134,33 @@
}
return metadata, nil
}
+
+type commonResult struct {
+ Resp *http.Response
+ Err error
+}
+
+func (cr commonResult) ExtractHeaders() (http.Header, error) {
+ var headers http.Header
+ if cr.Err != nil {
+ return headers, cr.Err
+ }
+
+ return cr.Resp.Header, nil
+}
+
+type CreateResult struct {
+ commonResult
+}
+
+type UpdateResult struct {
+ commonResult
+}
+
+type DeleteResult struct {
+ commonResult
+}
+
+type CopyResult struct {
+ commonResult
+}