Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame^] | 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "github.com/rackspace/gophercloud" |
| 7 | ) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 8 | |
| 9 | var ( |
| 10 | // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist. |
| 11 | ErrPageNotAvailable = errors.New("The requested page does not exist.") |
| 12 | ) |
| 13 | |
| 14 | // Page must be satisfied by the result type of any resource collection. |
| 15 | // It allows clients to interact with the resource uniformly, regardless of whether or not or how it's paginated. |
| 16 | // Generally, rather than implementing this interface directly, implementors should embed one of the concrete PageBase structs, |
| 17 | // instead. |
| 18 | // Depending on the pagination strategy of a particular resource, there may be an additional subinterface that the result type |
| 19 | // will need to implement. |
| 20 | type Page interface { |
| 21 | |
| 22 | // NextPageURL generates the URL for the page of data that follows this collection. |
| 23 | // Return "" if no such page exists. |
| 24 | NextPageURL() (string, error) |
| 25 | |
| 26 | // IsEmpty returns true if this Page has no items in it. |
| 27 | IsEmpty() (bool, error) |
| 28 | } |
| 29 | |
| 30 | // Pager knows how to advance through a specific resource collection, one page at a time. |
| 31 | type Pager struct { |
| 32 | initialURL string |
| 33 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame^] | 34 | client *gophercloud.ServiceClient |
| 35 | |
| 36 | createPage func(r LastHTTPResponse) Page |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | // NewPager constructs a manually-configured pager. |
| 40 | // Supply the URL for the first page, a function that requests a specific page given a URL, and a function that counts a page. |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame^] | 41 | func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r LastHTTPResponse) Page) Pager { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 42 | return Pager{ |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame^] | 43 | initialURL: initialURL, |
| 44 | client: client, |
| 45 | createPage: createPage, |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame^] | 49 | func (p Pager) fetchNextPage(url string) (Page, error) { |
| 50 | resp, err := Request(p.client, url) |
| 51 | if err != nil { |
| 52 | return nil, err |
| 53 | } |
| 54 | |
| 55 | remembered, err := RememberHTTPResponse(resp) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | |
| 60 | return p.createPage(remembered), nil |
| 61 | } |
| 62 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 63 | // EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function. |
| 64 | // Return "false" from the handler to prematurely stop iterating. |
| 65 | func (p Pager) EachPage(handler func(Page) (bool, error)) error { |
| 66 | currentURL := p.initialURL |
| 67 | for { |
| 68 | currentPage, err := p.fetchNextPage(currentURL) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
| 73 | empty, err := currentPage.IsEmpty() |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | if empty { |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | ok, err := handler(currentPage) |
| 82 | if err != nil { |
| 83 | return err |
| 84 | } |
| 85 | if !ok { |
| 86 | return nil |
| 87 | } |
| 88 | |
| 89 | currentURL, err = currentPage.NextPageURL() |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | if currentURL == "" { |
| 94 | return nil |
| 95 | } |
| 96 | } |
| 97 | } |