Base a pagination.PageResult on gophercloud.Result.
diff --git a/pagination/http.go b/pagination/http.go
index dd2c2d7..d3bd490 100644
--- a/pagination/http.go
+++ b/pagination/http.go
@@ -11,39 +11,38 @@
"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 {
+// PageResult stores the HTTP response that returned the current page of results.
+type PageResult struct {
+ gophercloud.Result
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) {
+// PageResultFrom parses an HTTP response as JSON and returns a PageResult containing the
+// results, interpreting it as JSON if the content type indicates.
+func PageResultFrom(resp http.Response) (PageResult, error) {
var parsedBody interface{}
defer resp.Body.Close()
rawBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
- return LastHTTPResponse{}, err
+ return PageResult{}, err
}
if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
err = json.Unmarshal(rawBody, &parsedBody)
if err != nil {
- return LastHTTPResponse{}, err
+ return PageResult{}, err
}
} else {
parsedBody = rawBody
}
- return LastHTTPResponse{
- URL: *resp.Request.URL,
- Header: resp.Header,
- Body: parsedBody,
+ return PageResult{
+ Result: gophercloud.Result{
+ Body: parsedBody,
+ Headers: resp.Header,
+ },
+ URL: *resp.Request.URL,
}, err
}
diff --git a/pagination/linked.go b/pagination/linked.go
index 0376edb..447d4b1 100644
--- a/pagination/linked.go
+++ b/pagination/linked.go
@@ -4,7 +4,7 @@
// LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result.
type LinkedPageBase struct {
- LastHTTPResponse
+ PageResult
// LinkPath lists the keys that should be traversed within a response to arrive at the "next" pointer.
// If any link along the path is missing, an empty URL will be returned.
diff --git a/pagination/linked_test.go b/pagination/linked_test.go
index 2621f98..4d3248e 100644
--- a/pagination/linked_test.go
+++ b/pagination/linked_test.go
@@ -57,8 +57,8 @@
client := createClient()
- createPage := func(r LastHTTPResponse) Page {
- return LinkedPageResult{LinkedPageBase{LastHTTPResponse: r}}
+ createPage := func(r PageResult) Page {
+ return LinkedPageResult{LinkedPageBase{PageResult: r}}
}
return NewPager(client, testhelper.Server.URL+"/page1", createPage)
diff --git a/pagination/marker.go b/pagination/marker.go
index 41b493a..e7688c2 100644
--- a/pagination/marker.go
+++ b/pagination/marker.go
@@ -11,7 +11,7 @@
// MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters.
type MarkerPageBase struct {
- LastHTTPResponse
+ PageResult
// Owner is a reference to the embedding struct.
Owner MarkerPage
diff --git a/pagination/marker_test.go b/pagination/marker_test.go
index e30264c..3b1df1d 100644
--- a/pagination/marker_test.go
+++ b/pagination/marker_test.go
@@ -56,8 +56,8 @@
client := createClient()
- createPage := func(r LastHTTPResponse) Page {
- p := MarkerPageResult{MarkerPageBase{LastHTTPResponse: r}}
+ createPage := func(r PageResult) Page {
+ p := MarkerPageResult{MarkerPageBase{PageResult: r}}
p.MarkerPageBase.Owner = p
return p
}
diff --git a/pagination/pager.go b/pagination/pager.go
index 75fe408..5c20e16 100644
--- a/pagination/pager.go
+++ b/pagination/pager.go
@@ -33,7 +33,7 @@
initialURL string
- createPage func(r LastHTTPResponse) Page
+ createPage func(r PageResult) Page
Err error
@@ -43,7 +43,7 @@
// 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 {
+func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r PageResult) Page) Pager {
return Pager{
client: client,
initialURL: initialURL,
@@ -53,7 +53,7 @@
// WithPageCreator returns a new Pager that substitutes a different page creation function. This is
// useful for overriding List functions in delegation.
-func (p Pager) WithPageCreator(createPage func(r LastHTTPResponse) Page) Pager {
+func (p Pager) WithPageCreator(createPage func(r PageResult) Page) Pager {
return Pager{
client: p.client,
initialURL: p.initialURL,
@@ -67,7 +67,7 @@
return nil, err
}
- remembered, err := RememberHTTPResponse(resp)
+ remembered, err := PageResultFrom(resp)
if err != nil {
return nil, err
}
diff --git a/pagination/single.go b/pagination/single.go
index a7f6fde..4dd3c5c 100644
--- a/pagination/single.go
+++ b/pagination/single.go
@@ -1,7 +1,7 @@
package pagination
// SinglePageBase may be embedded in a Page that contains all of the results from an operation at once.
-type SinglePageBase LastHTTPResponse
+type SinglePageBase PageResult
// NextPageURL always returns "" to indicate that there are no more pages to return.
func (current SinglePageBase) NextPageURL() (string, error) {
diff --git a/pagination/single_test.go b/pagination/single_test.go
index 31003e5..8817d57 100644
--- a/pagination/single_test.go
+++ b/pagination/single_test.go
@@ -45,7 +45,7 @@
fmt.Fprintf(w, `{ "ints": [1, 2, 3] }`)
})
- createPage := func(r LastHTTPResponse) Page {
+ createPage := func(r PageResult) Page {
return SinglePageResult{SinglePageBase(r)}
}