Paginate the objects.List call.
diff --git a/openstack/storage/v1/objects/objects.go b/openstack/storage/v1/objects/objects.go
index a4fbd8c..4cf3d2d 100644
--- a/openstack/storage/v1/objects/objects.go
+++ b/openstack/storage/v1/objects/objects.go
@@ -1,10 +1,12 @@
package objects
import (
- "encoding/json"
+ "fmt"
"io"
"io/ioutil"
"strings"
+
+ "github.com/rackspace/gophercloud/pagination"
)
// Object is a structure that holds information related to a storage object.
@@ -69,32 +71,47 @@
Headers map[string]string
}
-// ExtractInfo is a function that takes a ListResult (of type *http.Response)
-// and returns the objects' information.
-func ExtractInfo(lr ListResult) ([]Object, error) {
- var oi []Object
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return oi, err
+// ExtractInfo is a function that takes a page of objects and returns their full information.
+func ExtractInfo(page pagination.Page) ([]Object, error) {
+ untyped := page.(ListResult).Body.([]interface{})
+ results := make([]Object, len(untyped))
+ for index, each := range untyped {
+ results[index] = Object(each.(map[string]interface{}))
}
- err = json.Unmarshal(body, &oi)
- return oi, err
+ return results, nil
}
-// ExtractNames is a function that takes a ListResult (of type *http.Response)
-// and returns the objects' names.
-func ExtractNames(lr ListResult) ([]string, error) {
- var ons []string
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return ons, err
+// ExtractNames is a function that takes a page of objects and returns only their names.
+func ExtractNames(page pagination.Page) ([]string, error) {
+ casted := page.(ListResult)
+ ct := casted.Header.Get("Content-Type")
+
+ switch {
+ case strings.HasPrefix(ct, "application/json"):
+ parsed, err := ExtractInfo(page)
+ if err != nil {
+ return nil, err
+ }
+
+ names := make([]string, 0, len(parsed))
+ for _, object := range parsed {
+ names = append(names, object["name"].(string))
+ }
+ return names, nil
+ case strings.HasPrefix(ct, "text/plain"):
+ names := make([]string, 0, 50)
+
+ body := string(page.(ListResult).Body.([]uint8))
+ for _, name := range strings.Split(body, "\n") {
+ if len(name) > 0 {
+ names = append(names, name)
+ }
+ }
+
+ return names, nil
+ default:
+ return nil, fmt.Errorf("Cannot extract names from response with content-type: [%s]", ct)
}
- jr := string(body)
- ons = strings.Split(jr, "\n")
- ons = ons[:len(ons)-1]
- return ons, nil
}
// ExtractContent is a function that takes a DownloadResult (of type *http.Response)
diff --git a/openstack/storage/v1/objects/requests.go b/openstack/storage/v1/objects/requests.go
index 931653e..67aba34 100644
--- a/openstack/storage/v1/objects/requests.go
+++ b/openstack/storage/v1/objects/requests.go
@@ -7,10 +7,34 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
-// ListResult is a *http.Response that is returned from a call to the List function.
-type ListResult *http.Response
+// ListResult is a single page of objects that is returned from a call to the List function.
+type ListResult struct {
+ pagination.MarkerPageBase
+}
+
+// IsEmpty returns true if a ListResult contains no object names.
+func (r ListResult) IsEmpty() (bool, error) {
+ names, err := ExtractNames(r)
+ if err != nil {
+ return true, err
+ }
+ return len(names) == 0, nil
+}
+
+// LastMarker returns the last object name in a ListResult.
+func (r ListResult) LastMarker() (string, error) {
+ names, err := ExtractNames(r)
+ if err != nil {
+ return "", err
+ }
+ if len(names) == 0 {
+ return "", nil
+ }
+ return names[len(names)-1], nil
+}
// DownloadResult is a *http.Response that is returned from a call to the Download function.
type DownloadResult *http.Response
@@ -21,24 +45,25 @@
// List is a function that retrieves all objects in a container. It also returns the details
// for the container. To extract only the object information or names, pass the ListResult
// response to the ExtractInfo or ExtractNames function, respectively.
-func List(c *gophercloud.ServiceClient, opts ListOpts) (ListResult, error) {
- contentType := ""
-
- h := c.Provider.AuthenticatedHeaders()
+func List(c *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
+ var headers map[string]string
query := utils.BuildQuery(opts.Params)
if !opts.Full {
- contentType = "text/plain"
+ headers = map[string]string{"Content-Type": "text/plain"}
+ }
+
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ p := ListResult{pagination.MarkerPageBase{LastHTTPResponse: r}}
+ p.MarkerPageBase.Owner = p
+ return p
}
url := getContainerURL(c, opts.Container) + query
- resp, err := perigee.Request("GET", url, perigee.Options{
- MoreHeaders: h,
- Accept: contentType,
- OkCodes: []int{200, 204},
- })
- return &resp.HttpResponse, err
+ pager := pagination.NewPager(c, url, createPage)
+ pager.Headers = headers
+ return pager
}
// Download is a function that retrieves the content and metadata for an object.