Merge pull request #203 from smashwilson/paginate-storage
Mark-and-limit pagination and storage
diff --git a/acceptance/openstack/identity/v3/endpoint_test.go b/acceptance/openstack/identity/v3/endpoint_test.go
index 5c0e774..9032ec3 100644
--- a/acceptance/openstack/identity/v3/endpoint_test.go
+++ b/acceptance/openstack/identity/v3/endpoint_test.go
@@ -8,6 +8,7 @@
"github.com/rackspace/gophercloud"
endpoints3 "github.com/rackspace/gophercloud/openstack/identity/v3/endpoints"
services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
+ "github.com/rackspace/gophercloud/pagination"
)
func TestListEndpoints(t *testing.T) {
@@ -19,7 +20,7 @@
// Use the service to list all available endpoints.
pager := endpoints3.List(serviceClient, endpoints3.ListOpts{})
- err := pager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err := pager.EachPage(func(page pagination.Page) (bool, error) {
t.Logf("--- Page ---")
endpoints, err := endpoints3.ExtractEndpoints(page)
@@ -51,7 +52,7 @@
// Discover the service we're interested in.
servicePager := services3.List(client, services3.ListOpts{ServiceType: "compute"})
- err := servicePager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
part, err := services3.ExtractServices(page)
if err != nil {
return false, err
@@ -81,7 +82,7 @@
Availability: gophercloud.AvailabilityPublic,
ServiceID: compute.ID,
})
- err = computePager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err = computePager.EachPage(func(page pagination.Page) (bool, error) {
part, err := endpoints3.ExtractEndpoints(page)
if err != nil {
return false, err
diff --git a/acceptance/openstack/identity/v3/service_test.go b/acceptance/openstack/identity/v3/service_test.go
index 11f039d..082bd11 100644
--- a/acceptance/openstack/identity/v3/service_test.go
+++ b/acceptance/openstack/identity/v3/service_test.go
@@ -5,8 +5,8 @@
import (
"testing"
- "github.com/rackspace/gophercloud"
services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
+ "github.com/rackspace/gophercloud/pagination"
)
func TestListServices(t *testing.T) {
@@ -18,7 +18,7 @@
// Use the client to list all available services.
pager := services3.List(serviceClient, services3.ListOpts{})
- err := pager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err := pager.EachPage(func(page pagination.Page) (bool, error) {
parts, err := services3.ExtractServices(page)
if err != nil {
return false, err
diff --git a/acceptance/openstack/storage_test.go b/acceptance/openstack/storage_test.go
index 833e5a3..e7907d1 100644
--- a/acceptance/openstack/storage_test.go
+++ b/acceptance/openstack/storage_test.go
@@ -15,6 +15,7 @@
"github.com/rackspace/gophercloud/openstack/storage/v1/containers"
"github.com/rackspace/gophercloud/openstack/storage/v1/objects"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
var metadata = map[string]string{"gopher": "cloud"}
@@ -112,35 +113,41 @@
}
}()
- lr, err := containers.List(client, containers.ListOpts{
- Full: false,
+ cns := make([]string, 0, numContainers)
+ pager := containers.List(client, containers.ListOpts{Full: false})
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ names, err := containers.ExtractNames(page)
+ if err != nil {
+ return false, err
+ }
+
+ cns = append(cns, names...)
+
+ return true, nil
})
if err != nil {
- t.Error(err)
+ t.Fatal(err)
return
}
- cns, err := containers.ExtractNames(lr)
- if err != nil {
- t.Error(err)
- return
- }
+
if len(cns) != len(cNames) {
t.Errorf("Expected %d names and got %d", len(cNames), len(cns))
return
}
- lr, err = containers.List(client, containers.ListOpts{
- Full: true,
+ cis := make([]containers.Container, 0, numContainers)
+ pager = containers.List(client, containers.ListOpts{Full: true})
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ cisPage, err := containers.ExtractInfo(page)
+ if err != nil {
+ return false, err
+ }
+
+ cis = append(cis, cisPage...)
+
+ return true, nil
})
- if err != nil {
- t.Error(err)
- return
- }
- cis, err := containers.ExtractInfo(lr)
- if err != nil {
- t.Error(err)
- return
- }
+
if len(cis) != len(cNames) {
t.Errorf("Expected %d containers and got %d", len(cNames), len(cis))
return
@@ -235,37 +242,42 @@
}
}()
- lr, err := objects.List(client, objects.ListOpts{
- Full: false,
- Container: cName,
+ pager := objects.List(client, objects.ListOpts{Full: false, Container: cName})
+ ons := make([]string, 0, len(oNames))
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ names, err := objects.ExtractNames(page)
+ if err != nil {
+ return false, err
+ }
+ ons = append(ons, names...)
+
+ return true, nil
})
if err != nil {
t.Error(err)
return
}
- ons, err := objects.ExtractNames(lr)
- if err != nil {
- t.Error(err)
- return
- }
if len(ons) != len(oNames) {
t.Errorf("Expected %d names and got %d", len(oNames), len(ons))
return
}
- lr, err = objects.List(client, objects.ListOpts{
- Full: true,
- Container: cName,
+ pager = objects.List(client, objects.ListOpts{Full: true, Container: cName})
+ ois := make([]objects.Object, 0, len(oNames))
+ err = pager.EachPage(func(page pagination.Page) (bool, error) {
+ info, err := objects.ExtractInfo(page)
+ if err != nil {
+ return false, nil
+ }
+
+ ois = append(ois, info...)
+
+ return true, nil
})
if err != nil {
t.Error(err)
return
}
- ois, err := objects.ExtractInfo(lr)
- if err != nil {
- t.Error(err)
- return
- }
if len(ois) != len(oNames) {
t.Errorf("Expected %d containers and got %d", len(oNames), len(ois))
return
diff --git a/openstack/client.go b/openstack/client.go
index a5d0bc8..4fcf057 100644
--- a/openstack/client.go
+++ b/openstack/client.go
@@ -11,6 +11,7 @@
services3 "github.com/rackspace/gophercloud/openstack/identity/v3/services"
tokens3 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
const (
@@ -194,7 +195,7 @@
// Discover the service we're interested in.
var services = make([]services3.Service, 0, 1)
servicePager := services3.List(v3Client, services3.ListOpts{ServiceType: opts.Type})
- err := servicePager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err := servicePager.EachPage(func(page pagination.Page) (bool, error) {
part, err := services3.ExtractServices(page)
if err != nil {
return false, err
@@ -226,7 +227,7 @@
Availability: opts.Availability,
ServiceID: service.ID,
})
- err = endpointPager.EachPage(func(page gophercloud.Page) (bool, error) {
+ err = endpointPager.EachPage(func(page pagination.Page) (bool, error) {
part, err := endpoints3.ExtractEndpoints(page)
if err != nil {
return false, err
diff --git a/openstack/identity/v3/endpoints/requests.go b/openstack/identity/v3/endpoints/requests.go
index 7fc4544..71be65d 100644
--- a/openstack/identity/v3/endpoints/requests.go
+++ b/openstack/identity/v3/endpoints/requests.go
@@ -6,6 +6,7 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
// maybeString returns nil for empty strings and nil for empty.
@@ -94,7 +95,7 @@
}
// List enumerates endpoints in a paginated collection, optionally filtered by ListOpts criteria.
-func List(client *gophercloud.ServiceClient, opts ListOpts) gophercloud.Pager {
+func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
q := make(map[string]string)
if opts.Availability != "" {
q["interface"] = string(opts.Availability)
@@ -109,8 +110,12 @@
q["per_page"] = strconv.Itoa(opts.Page)
}
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ return EndpointPage{pagination.LinkedPageBase(r)}
+ }
+
u := getListURL(client) + utils.BuildQuery(q)
- return gophercloud.NewLinkedPager(client, u)
+ return pagination.NewPager(client, u, createPage)
}
// Update changes an existing endpoint with new data.
diff --git a/openstack/identity/v3/endpoints/requests_test.go b/openstack/identity/v3/endpoints/requests_test.go
index f988770..241d175 100644
--- a/openstack/identity/v3/endpoints/requests_test.go
+++ b/openstack/identity/v3/endpoints/requests_test.go
@@ -7,6 +7,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/testhelper"
)
@@ -91,6 +92,7 @@
testhelper.TestMethod(t, r, "GET")
testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
+ w.Header().Add("Content-Type", "application/json")
fmt.Fprintf(w, `
{
"endpoints": [
@@ -128,7 +130,7 @@
client := serviceClient()
count := 0
- List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
+ List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractEndpoints(page)
if err != nil {
diff --git a/openstack/identity/v3/endpoints/results.go b/openstack/identity/v3/endpoints/results.go
index 4d7bfbc..8da90f3 100644
--- a/openstack/identity/v3/endpoints/results.go
+++ b/openstack/identity/v3/endpoints/results.go
@@ -3,6 +3,7 @@
import (
"github.com/mitchellh/mapstructure"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
)
// Endpoint describes the entry point for another service's API.
@@ -15,13 +16,27 @@
URL string `mapstructure:"url" json:"url"`
}
+// EndpointPage is a single page of Endpoint results.
+type EndpointPage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty returns true if no Endpoints were returned.
+func (p EndpointPage) IsEmpty() (bool, error) {
+ es, err := ExtractEndpoints(p)
+ if err != nil {
+ return true, err
+ }
+ return len(es) == 0, nil
+}
+
// ExtractEndpoints extracts an Endpoint slice from a Page.
-func ExtractEndpoints(page gophercloud.Page) ([]Endpoint, error) {
+func ExtractEndpoints(page pagination.Page) ([]Endpoint, error) {
var response struct {
Endpoints []Endpoint `mapstructure:"endpoints"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &response)
+ err := mapstructure.Decode(page.(EndpointPage).Body, &response)
if err != nil {
return nil, err
}
diff --git a/openstack/identity/v3/services/requests.go b/openstack/identity/v3/services/requests.go
index 34df63f..fbd6a7c 100644
--- a/openstack/identity/v3/services/requests.go
+++ b/openstack/identity/v3/services/requests.go
@@ -6,6 +6,7 @@
"github.com/racker/perigee"
"github.com/rackspace/gophercloud"
"github.com/rackspace/gophercloud/openstack/utils"
+ "github.com/rackspace/gophercloud/pagination"
)
type response struct {
@@ -42,7 +43,7 @@
}
// List enumerates the services available to a specific user.
-func List(client *gophercloud.ServiceClient, opts ListOpts) gophercloud.Pager {
+func List(client *gophercloud.ServiceClient, opts ListOpts) pagination.Pager {
q := make(map[string]string)
if opts.ServiceType != "" {
q["type"] = opts.ServiceType
@@ -55,7 +56,11 @@
}
u := getListURL(client) + utils.BuildQuery(q)
- return gophercloud.NewLinkedPager(client, u)
+ createPage := func(r pagination.LastHTTPResponse) pagination.Page {
+ return ServicePage{pagination.LinkedPageBase(r)}
+ }
+
+ return pagination.NewPager(client, u, createPage)
}
// Get returns additional information about a service, given its ID.
diff --git a/openstack/identity/v3/services/requests_test.go b/openstack/identity/v3/services/requests_test.go
index 59cefe3..804f034 100644
--- a/openstack/identity/v3/services/requests_test.go
+++ b/openstack/identity/v3/services/requests_test.go
@@ -7,6 +7,7 @@
"testing"
"github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
"github.com/rackspace/gophercloud/testhelper"
)
@@ -99,7 +100,7 @@
client := serviceClient()
count := 0
- err := List(client, ListOpts{}).EachPage(func(page gophercloud.Page) (bool, error) {
+ err := List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
count++
actual, err := ExtractServices(page)
if err != nil {
diff --git a/openstack/identity/v3/services/results.go b/openstack/identity/v3/services/results.go
index 537ea2e..cccea8e 100644
--- a/openstack/identity/v3/services/results.go
+++ b/openstack/identity/v3/services/results.go
@@ -1,7 +1,7 @@
package services
import (
- "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/pagination"
"github.com/mitchellh/mapstructure"
)
@@ -14,12 +14,26 @@
Type string `json:"type"`
}
+// ServicePage is a single page of Service results.
+type ServicePage struct {
+ pagination.LinkedPageBase
+}
+
+// IsEmpty returns true if the page contains no results.
+func (p ServicePage) IsEmpty() (bool, error) {
+ services, err := ExtractServices(p)
+ if err != nil {
+ return true, err
+ }
+ return len(services) == 0, nil
+}
+
// ExtractServices extracts a slice of Services from a Collection acquired from List.
-func ExtractServices(page gophercloud.Page) ([]Service, error) {
+func ExtractServices(page pagination.Page) ([]Service, error) {
var response struct {
Services []Service `mapstructure:"services"`
}
- err := mapstructure.Decode(page.(gophercloud.LinkedPage).Body, &response)
+ err := mapstructure.Decode(page.(ServicePage).Body, &response)
return response.Services, err
}
diff --git a/openstack/storage/v1/containers/containers.go b/openstack/storage/v1/containers/containers.go
index 3a00647..8fa19aa 100644
--- a/openstack/storage/v1/containers/containers.go
+++ b/openstack/storage/v1/containers/containers.go
@@ -1,9 +1,10 @@
package containers
import (
- "encoding/json"
- "io/ioutil"
+ "fmt"
"strings"
+
+ "github.com/rackspace/gophercloud/pagination"
)
// Container is a structure that holds information related to a storage container.
@@ -42,32 +43,47 @@
Metadata map[string]string
}
-// ExtractInfo is a function that takes a ListResult (of type *http.Response)
-// and returns the containers' information.
-func ExtractInfo(lr ListResult) ([]Container, error) {
- var ci []Container
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return ci, err
+// ExtractInfo is a function that takes a ListResult and returns the containers' information.
+func ExtractInfo(page pagination.Page) ([]Container, error) {
+ untyped := page.(ListResult).Body.([]interface{})
+ results := make([]Container, len(untyped))
+ for index, each := range untyped {
+ results[index] = Container(each.(map[string]interface{}))
}
- err = json.Unmarshal(body, &ci)
- return ci, err
+ return results, nil
}
-// ExtractNames is a function that takes a ListResult (of type *http.Response)
-// and returns the containers' names.
-func ExtractNames(lr ListResult) ([]string, error) {
- var cns []string
- defer lr.Body.Close()
- body, err := ioutil.ReadAll(lr.Body)
- if err != nil {
- return cns, err
+// ExtractNames is a function that takes a ListResult and returns the containers' 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 _, container := range parsed {
+ names = append(names, container["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)
- cns = strings.Split(jr, "\n")
- cns = cns[:len(cns)-1]
- return cns, nil
}
// ExtractMetadata is a function that takes a GetResult (of type *http.Response)
diff --git a/openstack/storage/v1/containers/requests.go b/openstack/storage/v1/containers/requests.go
index 0db691c..2aa263a 100644
--- a/openstack/storage/v1/containers/requests.go
+++ b/openstack/storage/v1/containers/requests.go
@@ -6,10 +6,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
+type ListResult struct {
+ pagination.MarkerPageBase
+}
+
+// IsEmpty returns true if a ListResult contains no container 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 container 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
+}
// GetResult is a *http.Response that is returned from a call to the Get function.
type GetResult *http.Response
@@ -17,24 +41,25 @@
// List is a function that retrieves all objects in a container. It also returns the details
// for the account. To extract just the container 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 := getAccountURL(c) + 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
}
// Create is a function that creates a new container.
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.
diff --git a/pagination.go b/pagination.go
deleted file mode 100644
index 004c542..0000000
--- a/pagination.go
+++ /dev/null
@@ -1,187 +0,0 @@
-package gophercloud
-
-import (
- "encoding/json"
- "errors"
- "io/ioutil"
- "net/http"
-
- "github.com/mitchellh/mapstructure"
- "github.com/racker/perigee"
-)
-
-var (
- // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist.
- ErrPageNotAvailable = errors.New("The requested Collection page does not exist.")
-)
-
-// LastHTTPResponse stores generic information derived from an HTTP response.
-type LastHTTPResponse struct {
- http.Header
- Body interface{}
-}
-
-// RememberHTTPResponse parses an HTTP response as JSON and returns a LastHTTPResponse containing the results.
-// The main reason to do this instead of holding the response directly is that a response body can only be read once.
-// Also, this centralizes the JSON decoding.
-func RememberHTTPResponse(resp http.Response) (LastHTTPResponse, error) {
- var parsedBody interface{}
-
- defer resp.Body.Close()
- rawBody, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return LastHTTPResponse{}, err
- }
- err = json.Unmarshal(rawBody, &parsedBody)
- if err != nil {
- return LastHTTPResponse{}, err
- }
-
- return LastHTTPResponse{Header: resp.Header, Body: parsedBody}, err
-}
-
-// request performs a Perigee request and extracts the http.Response from the result.
-func request(client *ServiceClient, url string) (http.Response, error) {
- resp, err := perigee.Request("GET", url, perigee.Options{
- MoreHeaders: client.Provider.AuthenticatedHeaders(),
- OkCodes: []int{200},
- })
- if err != nil {
- return http.Response{}, err
- }
- return resp.HttpResponse, nil
-}
-
-// Page must be satisfied by the result type of any resource collection.
-// It allows clients to interact with the resource uniformly, regardless of whether or not or how it's paginated.
-type Page interface {
-
- // NextPageURL generates the URL for the page of data that follows this collection.
- // Return "" if no such page exists.
- NextPageURL() (string, error)
-}
-
-// SinglePage is a page that contains all of the results from an operation.
-type SinglePage LastHTTPResponse
-
-// NextPageURL always returns "" to indicate that there are no more pages to return.
-func (current SinglePage) NextPageURL() (string, error) {
- return "", nil
-}
-
-// LinkedPage is a page in a collection that provides navigational "Next" and "Previous" links within its result.
-type LinkedPage LastHTTPResponse
-
-// NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present.
-func (current LinkedPage) NextPageURL() (string, error) {
- type response struct {
- Links struct {
- Next *string `mapstructure:"next,omitempty"`
- } `mapstructure:"links"`
- }
-
- var r response
- err := mapstructure.Decode(current.Body, &r)
- if err != nil {
- return "", err
- }
-
- if r.Links.Next == nil {
- return "", nil
- }
-
- return *r.Links.Next, nil
-}
-
-// Pager knows how to advance through a specific resource collection, one page at a time.
-type Pager struct {
- initialURL string
-
- fetchNextPage func(string) (Page, error)
-}
-
-// NewPager constructs a manually-configured pager.
-// Supply the URL for the first page and a function that requests a specific page given a URL.
-func NewPager(initialURL string, fetchNextPage func(string) (Page, error)) Pager {
- return Pager{
- initialURL: initialURL,
- fetchNextPage: fetchNextPage,
- }
-}
-
-// NewSinglePager constructs a Pager that "iterates" over a single Page.
-// Supply the URL to request.
-func NewSinglePager(client *ServiceClient, onlyURL string) Pager {
- consumed := false
- single := func(_ string) (Page, error) {
- if !consumed {
- consumed = true
- resp, err := request(client, onlyURL)
- if err != nil {
- return SinglePage{}, err
- }
-
- cp, err := RememberHTTPResponse(resp)
- if err != nil {
- return SinglePage{}, err
- }
- return SinglePage(cp), nil
- }
- return SinglePage{}, ErrPageNotAvailable
- }
-
- return Pager{
- initialURL: "",
- fetchNextPage: single,
- }
-}
-
-// NewLinkedPager creates a Pager that uses a "links" element in the JSON response to locate the next page.
-func NewLinkedPager(client *ServiceClient, initialURL string) Pager {
- fetchNextPage := func(url string) (Page, error) {
- resp, err := request(client, url)
- if err != nil {
- return nil, err
- }
-
- cp, err := RememberHTTPResponse(resp)
- if err != nil {
- return nil, err
- }
-
- return LinkedPage(cp), nil
- }
-
- return Pager{
- initialURL: initialURL,
- fetchNextPage: fetchNextPage,
- }
-}
-
-// EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function.
-// Return "false" from the handler to prematurely stop iterating.
-func (p Pager) EachPage(handler func(Page) (bool, error)) error {
- currentURL := p.initialURL
- for {
- currentPage, err := p.fetchNextPage(currentURL)
- if err != nil {
- return err
- }
-
- ok, err := handler(currentPage)
- if err != nil {
- return err
- }
- if !ok {
- return nil
- }
-
- currentURL, err = currentPage.NextPageURL()
- if err != nil {
- return err
- }
- if currentURL == "" {
- return nil
- }
- }
-}
diff --git a/pagination/http.go b/pagination/http.go
new file mode 100644
index 0000000..dd2c2d7
--- /dev/null
+++ b/pagination/http.go
@@ -0,0 +1,65 @@
+package pagination
+
+import (
+ "encoding/json"
+ "io/ioutil"
+ "net/http"
+ "net/url"
+ "strings"
+
+ "github.com/racker/perigee"
+ "github.com/rackspace/gophercloud"
+)
+
+// LastHTTPResponse stores generic information derived from an HTTP response.
+// This exists primarily because the body of an http.Response can only be used once.
+type LastHTTPResponse struct {
+ url.URL
+ http.Header
+ Body interface{}
+}
+
+// RememberHTTPResponse parses an HTTP response as JSON and returns a LastHTTPResponse containing the results.
+// The main reason to do this instead of holding the response directly is that a response body can only be read once.
+// Also, this centralizes the JSON decoding.
+func RememberHTTPResponse(resp http.Response) (LastHTTPResponse, error) {
+ var parsedBody interface{}
+
+ defer resp.Body.Close()
+ rawBody, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ return LastHTTPResponse{}, err
+ }
+
+ if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
+ err = json.Unmarshal(rawBody, &parsedBody)
+ if err != nil {
+ return LastHTTPResponse{}, err
+ }
+ } else {
+ parsedBody = rawBody
+ }
+
+ return LastHTTPResponse{
+ URL: *resp.Request.URL,
+ Header: resp.Header,
+ Body: parsedBody,
+ }, err
+}
+
+// Request performs a Perigee request and extracts the http.Response from the result.
+func Request(client *gophercloud.ServiceClient, headers map[string]string, url string) (http.Response, error) {
+ h := client.Provider.AuthenticatedHeaders()
+ for key, value := range headers {
+ h[key] = value
+ }
+
+ resp, err := perigee.Request("GET", url, perigee.Options{
+ MoreHeaders: h,
+ OkCodes: []int{200, 204},
+ })
+ if err != nil {
+ return http.Response{}, err
+ }
+ return resp.HttpResponse, nil
+}
diff --git a/pagination/linked.go b/pagination/linked.go
new file mode 100644
index 0000000..fc88a55
--- /dev/null
+++ b/pagination/linked.go
@@ -0,0 +1,29 @@
+package pagination
+
+import "github.com/mitchellh/mapstructure"
+
+// LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result.
+type LinkedPageBase LastHTTPResponse
+
+// NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present.
+// It assumes that the links are available in a "links" element of the top-level response object.
+// If this is not the case, override NextPageURL on your result type.
+func (current LinkedPageBase) NextPageURL() (string, error) {
+ type response struct {
+ Links struct {
+ Next *string `mapstructure:"next,omitempty"`
+ } `mapstructure:"links"`
+ }
+
+ var r response
+ err := mapstructure.Decode(current.Body, &r)
+ if err != nil {
+ return "", err
+ }
+
+ if r.Links.Next == nil {
+ return "", nil
+ }
+
+ return *r.Links.Next, nil
+}
diff --git a/pagination/linked_test.go b/pagination/linked_test.go
new file mode 100644
index 0000000..2093755
--- /dev/null
+++ b/pagination/linked_test.go
@@ -0,0 +1,107 @@
+package pagination
+
+import (
+ "fmt"
+ "net/http"
+ "reflect"
+ "testing"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud/testhelper"
+)
+
+// LinkedPager sample and test cases.
+
+type LinkedPageResult struct {
+ LinkedPageBase
+}
+
+func (r LinkedPageResult) IsEmpty() (bool, error) {
+ is, err := ExtractLinkedInts(r)
+ if err != nil {
+ return true, nil
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractLinkedInts(page Page) ([]int, error) {
+ var response struct {
+ Ints []int `mapstructure:"ints"`
+ }
+
+ err := mapstructure.Decode(page.(LinkedPageResult).Body, &response)
+ if err != nil {
+ return nil, err
+ }
+
+ return response.Ints, nil
+}
+
+func createLinked(t *testing.T) Pager {
+ testhelper.SetupHTTP()
+
+ testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
+ })
+
+ testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
+ })
+
+ testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
+ })
+
+ client := createClient()
+
+ createPage := func(r LastHTTPResponse) Page {
+ return LinkedPageResult{LinkedPageBase(r)}
+ }
+
+ return NewPager(client, testhelper.Server.URL+"/page1", createPage)
+}
+
+func TestEnumerateLinked(t *testing.T) {
+ pager := createLinked(t)
+ defer testhelper.TeardownHTTP()
+
+ callCount := 0
+ err := pager.EachPage(func(page Page) (bool, error) {
+ actual, err := ExtractLinkedInts(page)
+ if err != nil {
+ return false, err
+ }
+
+ t.Logf("Handler invoked with %v", actual)
+
+ var expected []int
+ switch callCount {
+ case 0:
+ expected = []int{1, 2, 3}
+ case 1:
+ expected = []int{4, 5, 6}
+ case 2:
+ expected = []int{7, 8, 9}
+ default:
+ t.Fatalf("Unexpected call count: %d", callCount)
+ return false, nil
+ }
+
+ if !reflect.DeepEqual(expected, actual) {
+ t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
+ }
+
+ callCount++
+ return true, nil
+ })
+ if err != nil {
+ t.Errorf("Unexpected error for page iteration: %v", err)
+ }
+
+ if callCount != 3 {
+ t.Errorf("Expected 3 calls, but was %d", callCount)
+ }
+}
diff --git a/pagination/marker.go b/pagination/marker.go
new file mode 100644
index 0000000..41b493a
--- /dev/null
+++ b/pagination/marker.go
@@ -0,0 +1,34 @@
+package pagination
+
+// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
+// For convenience, embed the MarkedPageBase struct.
+type MarkerPage interface {
+ Page
+
+ // LastMarker returns the last "marker" value on this page.
+ LastMarker() (string, error)
+}
+
+// MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters.
+type MarkerPageBase struct {
+ LastHTTPResponse
+
+ // Owner is a reference to the embedding struct.
+ Owner MarkerPage
+}
+
+// NextPageURL generates the URL for the page of results after this one.
+func (current MarkerPageBase) NextPageURL() (string, error) {
+ currentURL := current.URL
+
+ mark, err := current.Owner.LastMarker()
+ if err != nil {
+ return "", err
+ }
+
+ q := currentURL.Query()
+ q.Set("marker", mark)
+ currentURL.RawQuery = q.Encode()
+
+ return currentURL.String(), nil
+}
diff --git a/pagination/marker_test.go b/pagination/marker_test.go
new file mode 100644
index 0000000..e30264c
--- /dev/null
+++ b/pagination/marker_test.go
@@ -0,0 +1,113 @@
+package pagination
+
+import (
+ "fmt"
+ "net/http"
+ "strings"
+ "testing"
+
+ "github.com/rackspace/gophercloud/testhelper"
+)
+
+// MarkerPager sample and test cases.
+
+type MarkerPageResult struct {
+ MarkerPageBase
+}
+
+func (r MarkerPageResult) IsEmpty() (bool, error) {
+ results, err := ExtractMarkerStrings(r)
+ if err != nil {
+ return true, err
+ }
+ return len(results) == 0, err
+}
+
+func (r MarkerPageResult) LastMarker() (string, error) {
+ results, err := ExtractMarkerStrings(r)
+ if err != nil {
+ return "", err
+ }
+ if len(results) == 0 {
+ return "", nil
+ }
+ return results[len(results)-1], nil
+}
+
+func createMarkerPaged(t *testing.T) Pager {
+ testhelper.SetupHTTP()
+
+ testhelper.Mux.HandleFunc("/page", func(w http.ResponseWriter, r *http.Request) {
+ r.ParseForm()
+ ms := r.Form["marker"]
+ switch {
+ case len(ms) == 0:
+ fmt.Fprintf(w, "aaa\nbbb\nccc")
+ case len(ms) == 1 && ms[0] == "ccc":
+ fmt.Fprintf(w, "ddd\neee\nfff")
+ case len(ms) == 1 && ms[0] == "fff":
+ fmt.Fprintf(w, "ggg\nhhh\niii")
+ case len(ms) == 1 && ms[0] == "iii":
+ w.WriteHeader(http.StatusNoContent)
+ default:
+ t.Errorf("Request with unexpected marker: [%v]", ms)
+ }
+ })
+
+ client := createClient()
+
+ createPage := func(r LastHTTPResponse) Page {
+ p := MarkerPageResult{MarkerPageBase{LastHTTPResponse: r}}
+ p.MarkerPageBase.Owner = p
+ return p
+ }
+
+ return NewPager(client, testhelper.Server.URL+"/page", createPage)
+}
+
+func ExtractMarkerStrings(page Page) ([]string, error) {
+ content := page.(MarkerPageResult).Body.([]uint8)
+ parts := strings.Split(string(content), "\n")
+ results := make([]string, 0, len(parts))
+ for _, part := range parts {
+ if len(part) > 0 {
+ results = append(results, part)
+ }
+ }
+ return results, nil
+}
+
+func TestEnumerateMarker(t *testing.T) {
+ pager := createMarkerPaged(t)
+ defer testhelper.TeardownHTTP()
+
+ callCount := 0
+ err := pager.EachPage(func(page Page) (bool, error) {
+ actual, err := ExtractMarkerStrings(page)
+ if err != nil {
+ return false, err
+ }
+
+ t.Logf("Handler invoked with %v", actual)
+
+ var expected []string
+ switch callCount {
+ case 0:
+ expected = []string{"aaa", "bbb", "ccc"}
+ case 1:
+ expected = []string{"ddd", "eee", "fff"}
+ case 2:
+ expected = []string{"ggg", "hhh", "iii"}
+ default:
+ t.Fatalf("Unexpected call count: %d", callCount)
+ return false, nil
+ }
+
+ testhelper.CheckDeepEquals(t, expected, actual)
+
+ callCount++
+ return true, nil
+ })
+ testhelper.AssertNoErr(t, err)
+ testhelper.AssertEquals(t, callCount, 3)
+}
diff --git a/pagination/null.go b/pagination/null.go
new file mode 100644
index 0000000..ae57e18
--- /dev/null
+++ b/pagination/null.go
@@ -0,0 +1,20 @@
+package pagination
+
+// nullPage is an always-empty page that trivially satisfies all Page interfacts.
+// It's useful to be returned along with an error.
+type nullPage struct{}
+
+// NextPageURL always returns "" to indicate that there are no more pages to return.
+func (p nullPage) NextPageURL() (string, error) {
+ return "", nil
+}
+
+// IsEmpty always returns true to prevent iteration over nullPages.
+func (p nullPage) IsEmpty() (bool, error) {
+ return true, nil
+}
+
+// LastMark always returns "" because the nullPage contains no items to have a mark.
+func (p nullPage) LastMark() (string, error) {
+ return "", nil
+}
diff --git a/pagination/pager.go b/pagination/pager.go
new file mode 100644
index 0000000..806d98a
--- /dev/null
+++ b/pagination/pager.go
@@ -0,0 +1,100 @@
+package pagination
+
+import (
+ "errors"
+
+ "github.com/rackspace/gophercloud"
+)
+
+var (
+ // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist.
+ ErrPageNotAvailable = errors.New("The requested page does not exist.")
+)
+
+// Page must be satisfied by the result type of any resource collection.
+// It allows clients to interact with the resource uniformly, regardless of whether or not or how it's paginated.
+// Generally, rather than implementing this interface directly, implementors should embed one of the concrete PageBase structs,
+// instead.
+// Depending on the pagination strategy of a particular resource, there may be an additional subinterface that the result type
+// will need to implement.
+type Page interface {
+
+ // NextPageURL generates the URL for the page of data that follows this collection.
+ // Return "" if no such page exists.
+ NextPageURL() (string, error)
+
+ // IsEmpty returns true if this Page has no items in it.
+ IsEmpty() (bool, error)
+}
+
+// Pager knows how to advance through a specific resource collection, one page at a time.
+type Pager struct {
+ initialURL string
+
+ client *gophercloud.ServiceClient
+
+ createPage func(r LastHTTPResponse) Page
+
+ // Headers supplies additional HTTP headers to populate on each paged request.
+ Headers map[string]string
+}
+
+// NewPager constructs a manually-configured pager.
+// Supply the URL for the first page, a function that requests a specific page given a URL, and a function that counts a page.
+func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r LastHTTPResponse) Page) Pager {
+ return Pager{
+ initialURL: initialURL,
+ client: client,
+ createPage: createPage,
+ }
+}
+
+func (p Pager) fetchNextPage(url string) (Page, error) {
+ resp, err := Request(p.client, p.Headers, url)
+ if err != nil {
+ return nil, err
+ }
+
+ remembered, err := RememberHTTPResponse(resp)
+ if err != nil {
+ return nil, err
+ }
+
+ return p.createPage(remembered), nil
+}
+
+// EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function.
+// Return "false" from the handler to prematurely stop iterating.
+func (p Pager) EachPage(handler func(Page) (bool, error)) error {
+ currentURL := p.initialURL
+ for {
+ currentPage, err := p.fetchNextPage(currentURL)
+ if err != nil {
+ return err
+ }
+
+ empty, err := currentPage.IsEmpty()
+ if err != nil {
+ return err
+ }
+ if empty {
+ return nil
+ }
+
+ ok, err := handler(currentPage)
+ if err != nil {
+ return err
+ }
+ if !ok {
+ return nil
+ }
+
+ currentURL, err = currentPage.NextPageURL()
+ if err != nil {
+ return err
+ }
+ if currentURL == "" {
+ return nil
+ }
+ }
+}
diff --git a/pagination/pagination_test.go b/pagination/pagination_test.go
new file mode 100644
index 0000000..779bd79
--- /dev/null
+++ b/pagination/pagination_test.go
@@ -0,0 +1,13 @@
+package pagination
+
+import (
+ "github.com/rackspace/gophercloud"
+ "github.com/rackspace/gophercloud/testhelper"
+)
+
+func createClient() *gophercloud.ServiceClient {
+ return &gophercloud.ServiceClient{
+ Provider: &gophercloud.ProviderClient{TokenID: "abc123"},
+ Endpoint: testhelper.Endpoint(),
+ }
+}
diff --git a/pagination/pkg.go b/pagination/pkg.go
new file mode 100644
index 0000000..912daea
--- /dev/null
+++ b/pagination/pkg.go
@@ -0,0 +1,4 @@
+/*
+Package pagination contains utilities and convenience structs that implement common pagination idioms within OpenStack APIs.
+*/
+package pagination
diff --git a/pagination/single.go b/pagination/single.go
new file mode 100644
index 0000000..a7f6fde
--- /dev/null
+++ b/pagination/single.go
@@ -0,0 +1,9 @@
+package pagination
+
+// SinglePageBase may be embedded in a Page that contains all of the results from an operation at once.
+type SinglePageBase LastHTTPResponse
+
+// NextPageURL always returns "" to indicate that there are no more pages to return.
+func (current SinglePageBase) NextPageURL() (string, error) {
+ return "", nil
+}
diff --git a/pagination/single_test.go b/pagination/single_test.go
new file mode 100644
index 0000000..31003e5
--- /dev/null
+++ b/pagination/single_test.go
@@ -0,0 +1,71 @@
+package pagination
+
+import (
+ "fmt"
+ "net/http"
+ "testing"
+
+ "github.com/mitchellh/mapstructure"
+ "github.com/rackspace/gophercloud/testhelper"
+)
+
+// SinglePage sample and test cases.
+
+type SinglePageResult struct {
+ SinglePageBase
+}
+
+func (r SinglePageResult) IsEmpty() (bool, error) {
+ is, err := ExtractSingleInts(r)
+ if err != nil {
+ return true, err
+ }
+ return len(is) == 0, nil
+}
+
+func ExtractSingleInts(page Page) ([]int, error) {
+ var response struct {
+ Ints []int `mapstructure:"ints"`
+ }
+
+ err := mapstructure.Decode(page.(SinglePageResult).Body, &response)
+ if err != nil {
+ return nil, err
+ }
+
+ return response.Ints, nil
+}
+
+func setupSinglePaged() Pager {
+ testhelper.SetupHTTP()
+ client := createClient()
+
+ testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Add("Content-Type", "application/json")
+ fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`)
+ })
+
+ createPage := func(r LastHTTPResponse) Page {
+ return SinglePageResult{SinglePageBase(r)}
+ }
+
+ return NewPager(client, testhelper.Server.URL+"/only", createPage)
+}
+
+func TestEnumerateSinglePaged(t *testing.T) {
+ callCount := 0
+ pager := setupSinglePaged()
+ defer testhelper.TeardownHTTP()
+
+ err := pager.EachPage(func(page Page) (bool, error) {
+ callCount++
+
+ expected := []int{1, 2, 3}
+ actual, err := ExtractSingleInts(page)
+ testhelper.AssertNoErr(t, err)
+ testhelper.CheckDeepEquals(t, expected, actual)
+ return true, nil
+ })
+ testhelper.CheckNoErr(t, err)
+ testhelper.CheckEquals(t, 1, callCount)
+}
diff --git a/pagination_test.go b/pagination_test.go
deleted file mode 100644
index d215c33..0000000
--- a/pagination_test.go
+++ /dev/null
@@ -1,148 +0,0 @@
-package gophercloud
-
-import (
- "fmt"
- "net/http"
- "reflect"
- "testing"
-
- "github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud/testhelper"
-)
-
-func createClient() *ServiceClient {
- return &ServiceClient{
- Provider: &ProviderClient{TokenID: "abc123"},
- Endpoint: testhelper.Endpoint(),
- }
-}
-
-// SinglePage sample and test cases.
-
-func ExtractSingleInts(page Page) ([]int, error) {
- var response struct {
- Ints []int `mapstructure:"ints"`
- }
-
- err := mapstructure.Decode(page.(SinglePage).Body, &response)
- if err != nil {
- return nil, err
- }
-
- return response.Ints, nil
-}
-
-func setupSinglePaged() Pager {
- testhelper.SetupHTTP()
- client := createClient()
-
- testhelper.Mux.HandleFunc("/only", func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`)
- })
-
- return NewSinglePager(client, testhelper.Server.URL+"/only")
-}
-
-func TestEnumerateSinglePaged(t *testing.T) {
- callCount := 0
- pager := setupSinglePaged()
- defer testhelper.TeardownHTTP()
-
- err := pager.EachPage(func(page Page) (bool, error) {
- callCount++
-
- expected := []int{1, 2, 3}
- actual, err := ExtractSingleInts(page)
- if err != nil {
- return false, err
- }
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Expected %v, but was %v", expected, actual)
- }
- return true, nil
- })
- if err != nil {
- t.Fatalf("Unexpected error calling EachPage: %v", err)
- }
-
- if callCount != 1 {
- t.Errorf("Callback was invoked %d times", callCount)
- }
-}
-
-// LinkedPager sample and test cases.
-
-func ExtractLinkedInts(page Page) ([]int, error) {
- var response struct {
- Ints []int `mapstructure:"ints"`
- }
-
- err := mapstructure.Decode(page.(LinkedPage).Body, &response)
- if err != nil {
- return nil, err
- }
-
- return response.Ints, nil
-}
-
-func createLinked(t *testing.T) Pager {
- testhelper.SetupHTTP()
-
- testhelper.Mux.HandleFunc("/page1", func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, `{ "ints": [1, 2, 3], "links": { "next": "%s/page2" } }`, testhelper.Server.URL)
- })
-
- testhelper.Mux.HandleFunc("/page2", func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, `{ "ints": [4, 5, 6], "links": { "next": "%s/page3" } }`, testhelper.Server.URL)
- })
-
- testhelper.Mux.HandleFunc("/page3", func(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, `{ "ints": [7, 8, 9], "links": { "next": null } }`)
- })
-
- client := createClient()
-
- return NewLinkedPager(client, testhelper.Server.URL+"/page1")
-}
-
-func TestEnumerateLinked(t *testing.T) {
- pager := createLinked(t)
- defer testhelper.TeardownHTTP()
-
- callCount := 0
- err := pager.EachPage(func(page Page) (bool, error) {
- actual, err := ExtractLinkedInts(page)
- if err != nil {
- return false, err
- }
-
- t.Logf("Handler invoked with %v", actual)
-
- var expected []int
- switch callCount {
- case 0:
- expected = []int{1, 2, 3}
- case 1:
- expected = []int{4, 5, 6}
- case 2:
- expected = []int{7, 8, 9}
- default:
- t.Fatalf("Unexpected call count: %d", callCount)
- return false, nil
- }
-
- if !reflect.DeepEqual(expected, actual) {
- t.Errorf("Call %d: Expected %#v, but was %#v", callCount, expected, actual)
- }
-
- callCount++
- return true, nil
- })
- if err != nil {
- t.Errorf("Unexpected error for page iteration: %v", err)
- }
-
- if callCount != 3 {
- t.Errorf("Expected 3 calls, but was %d", callCount)
- }
-}