blob: 0b5ce09f1b8461e44fde9c5c59af4e27de8b7f43 [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
3import "github.com/rackspace/gophercloud"
4
5// SinglePageBase may be embedded in a Page that contains all of the results from an operation at once.
6type SinglePageBase LastHTTPResponse
7
8// NextPageURL always returns "" to indicate that there are no more pages to return.
9func (current SinglePageBase) NextPageURL() (string, error) {
10 return "", nil
11}
12
13// NewSinglePager constructs a Pager that "iterates" over a single Page.
14// Supply the URL to request and a function that creates a Page of the appropriate type.
15func NewSinglePager(client *gophercloud.ServiceClient, onlyURL string, createPage func(resp LastHTTPResponse) Page) Pager {
16 consumed := false
17 single := func(_ string) (Page, error) {
18 if !consumed {
19 consumed = true
20 resp, err := Request(client, onlyURL)
21 if err != nil {
22 return nullPage{}, err
23 }
24
25 cp, err := RememberHTTPResponse(resp)
26 if err != nil {
27 return nullPage{}, err
28 }
29 return createPage(cp), nil
30 }
31 return nullPage{}, ErrPageNotAvailable
32 }
33
34 return Pager{
35 initialURL: "",
36 fetchNextPage: single,
37 }
38}