| 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 | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 37 |  | 
| Jon Perritt | 9bd7bd9 | 2014-09-28 20:10:27 -0500 | [diff] [blame] | 38 | Err error | 
|  | 39 |  | 
| Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 40 | // Headers supplies additional HTTP headers to populate on each paged request. | 
|  | 41 | Headers map[string]string | 
| Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
|  | 44 | // NewPager constructs a manually-configured pager. | 
|  | 45 | // 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] | 46 | 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] | 47 | return Pager{ | 
| Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 48 | initialURL: initialURL, | 
|  | 49 | client:     client, | 
|  | 50 | createPage: createPage, | 
| Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 |  | 
| Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 54 | func (p Pager) fetchNextPage(url string) (Page, error) { | 
| Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 55 | resp, err := Request(p.client, p.Headers, url) | 
| Ash Wilson | 7049af4 | 2014-09-16 13:04:48 -0400 | [diff] [blame] | 56 | if err != nil { | 
|  | 57 | return nil, err | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | remembered, err := RememberHTTPResponse(resp) | 
|  | 61 | if err != nil { | 
|  | 62 | return nil, err | 
|  | 63 | } | 
|  | 64 |  | 
|  | 65 | return p.createPage(remembered), nil | 
|  | 66 | } | 
|  | 67 |  | 
| Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 68 | // EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function. | 
|  | 69 | // Return "false" from the handler to prematurely stop iterating. | 
|  | 70 | func (p Pager) EachPage(handler func(Page) (bool, error)) error { | 
| Jon Perritt | 6f9e4ff | 2014-09-30 13:29:47 -0500 | [diff] [blame^] | 71 | if p.Err != nil { | 
|  | 72 | return p.Err | 
|  | 73 | } | 
| Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 74 | currentURL := p.initialURL | 
|  | 75 | for { | 
|  | 76 | currentPage, err := p.fetchNextPage(currentURL) | 
|  | 77 | if err != nil { | 
|  | 78 | return err | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | empty, err := currentPage.IsEmpty() | 
|  | 82 | if err != nil { | 
|  | 83 | return err | 
|  | 84 | } | 
|  | 85 | if empty { | 
|  | 86 | return nil | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | ok, err := handler(currentPage) | 
|  | 90 | if err != nil { | 
|  | 91 | return err | 
|  | 92 | } | 
|  | 93 | if !ok { | 
|  | 94 | return nil | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | currentURL, err = currentPage.NextPageURL() | 
|  | 98 | if err != nil { | 
|  | 99 | return err | 
|  | 100 | } | 
|  | 101 | if currentURL == "" { | 
|  | 102 | return nil | 
|  | 103 | } | 
|  | 104 | } | 
|  | 105 | } |