Turns out I can eliminate NewXyzPager funcs.
diff --git a/pagination/linked.go b/pagination/linked.go
index 3b26013..fc88a55 100644
--- a/pagination/linked.go
+++ b/pagination/linked.go
@@ -1,9 +1,6 @@
package pagination
-import (
- "github.com/mitchellh/mapstructure"
- "github.com/rackspace/gophercloud"
-)
+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
@@ -30,25 +27,3 @@
return *r.Links.Next, nil
}
-
-// NewLinkedPager creates a Pager that uses a "links" element in the JSON response to locate the next page.
-func NewLinkedPager(client *gophercloud.ServiceClient, initialURL string, createPage func(resp LastHTTPResponse) Page) 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 createPage(cp), nil
- }
-
- return Pager{
- initialURL: initialURL,
- fetchNextPage: fetchNextPage,
- }
-}
diff --git a/pagination/linked_test.go b/pagination/linked_test.go
index 6346126..2093755 100644
--- a/pagination/linked_test.go
+++ b/pagination/linked_test.go
@@ -61,7 +61,7 @@
return LinkedPageResult{LinkedPageBase(r)}
}
- return NewLinkedPager(client, testhelper.Server.URL+"/page1", createPage)
+ return NewPager(client, testhelper.Server.URL+"/page1", createPage)
}
func TestEnumerateLinked(t *testing.T) {
diff --git a/pagination/marker.go b/pagination/marker.go
index 00ad88e..41b493a 100644
--- a/pagination/marker.go
+++ b/pagination/marker.go
@@ -1,7 +1,5 @@
package pagination
-import "github.com/rackspace/gophercloud"
-
// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
// For convenience, embed the MarkedPageBase struct.
type MarkerPage interface {
@@ -34,27 +32,3 @@
return currentURL.String(), nil
}
-
-// NewMarkerPager creates a Pager that iterates over successive pages by issuing requests with a "marker" parameter set to the
-// final element of the previous Page.
-func NewMarkerPager(client *gophercloud.ServiceClient, initialURL string, createPage func(resp LastHTTPResponse) MarkerPage) Pager {
-
- fetchNextPage := func(currentURL string) (Page, error) {
- resp, err := Request(client, currentURL)
- if err != nil {
- return nullPage{}, err
- }
-
- last, err := RememberHTTPResponse(resp)
- if err != nil {
- return nullPage{}, err
- }
-
- return createPage(last), nil
- }
-
- return Pager{
- initialURL: initialURL,
- fetchNextPage: fetchNextPage,
- }
-}
diff --git a/pagination/marker_test.go b/pagination/marker_test.go
index 742a1d1..e30264c 100644
--- a/pagination/marker_test.go
+++ b/pagination/marker_test.go
@@ -56,13 +56,13 @@
client := createClient()
- createPage := func(r LastHTTPResponse) MarkerPage {
+ createPage := func(r LastHTTPResponse) Page {
p := MarkerPageResult{MarkerPageBase{LastHTTPResponse: r}}
p.MarkerPageBase.Owner = p
return p
}
- return NewMarkerPager(client, testhelper.Server.URL+"/page", createPage)
+ return NewPager(client, testhelper.Server.URL+"/page", createPage)
}
func ExtractMarkerStrings(page Page) ([]string, error) {
diff --git a/pagination/pager.go b/pagination/pager.go
index c95d744..52ebb96 100644
--- a/pagination/pager.go
+++ b/pagination/pager.go
@@ -1,6 +1,10 @@
package pagination
-import "errors"
+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.
@@ -27,18 +31,35 @@
type Pager struct {
initialURL string
- fetchNextPage func(string) (Page, error)
+ client *gophercloud.ServiceClient
+
+ createPage func(r LastHTTPResponse) Page
}
// 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(initialURL string, fetchNextPage func(string) (Page, error)) Pager {
+func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r LastHTTPResponse) Page) Pager {
return Pager{
- initialURL: initialURL,
- fetchNextPage: fetchNextPage,
+ initialURL: initialURL,
+ client: client,
+ createPage: createPage,
}
}
+func (p Pager) fetchNextPage(url string) (Page, error) {
+ resp, err := Request(p.client, 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 {
diff --git a/pagination/single.go b/pagination/single.go
index 0b5ce09..a7f6fde 100644
--- a/pagination/single.go
+++ b/pagination/single.go
@@ -1,7 +1,5 @@
package pagination
-import "github.com/rackspace/gophercloud"
-
// SinglePageBase may be embedded in a Page that contains all of the results from an operation at once.
type SinglePageBase LastHTTPResponse
@@ -9,30 +7,3 @@
func (current SinglePageBase) NextPageURL() (string, error) {
return "", nil
}
-
-// NewSinglePager constructs a Pager that "iterates" over a single Page.
-// Supply the URL to request and a function that creates a Page of the appropriate type.
-func NewSinglePager(client *gophercloud.ServiceClient, onlyURL string, createPage func(resp LastHTTPResponse) Page) Pager {
- consumed := false
- single := func(_ string) (Page, error) {
- if !consumed {
- consumed = true
- resp, err := Request(client, onlyURL)
- if err != nil {
- return nullPage{}, err
- }
-
- cp, err := RememberHTTPResponse(resp)
- if err != nil {
- return nullPage{}, err
- }
- return createPage(cp), nil
- }
- return nullPage{}, ErrPageNotAvailable
- }
-
- return Pager{
- initialURL: "",
- fetchNextPage: single,
- }
-}
diff --git a/pagination/single_test.go b/pagination/single_test.go
index a470b6b..31003e5 100644
--- a/pagination/single_test.go
+++ b/pagination/single_test.go
@@ -49,7 +49,7 @@
return SinglePageResult{SinglePageBase(r)}
}
- return NewSinglePager(client, testhelper.Server.URL+"/only", createPage)
+ return NewPager(client, testhelper.Server.URL+"/only", createPage)
}
func TestEnumerateSinglePaged(t *testing.T) {