Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame^] | 1 | package pagination |
| 2 | |
| 3 | import "github.com/rackspace/gophercloud" |
| 4 | |
| 5 | // SinglePageBase may be embedded in a Page that contains all of the results from an operation at once. |
| 6 | type SinglePageBase LastHTTPResponse |
| 7 | |
| 8 | // NextPageURL always returns "" to indicate that there are no more pages to return. |
| 9 | func (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. |
| 15 | func 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 | } |