blob: cc1c04870a75cfcfa73bc7e46bfa1e51c3a50131 [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
Ash Wilson7049af42014-09-16 13:04:48 -04003import (
4 "errors"
5
6 "github.com/rackspace/gophercloud"
7)
Ash Wilsonc8e68872014-09-16 10:36:56 -04008
9var (
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.
20type 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.
31type Pager struct {
32 initialURL string
33
Ash Wilson7049af42014-09-16 13:04:48 -040034 client *gophercloud.ServiceClient
35
36 createPage func(r LastHTTPResponse) Page
Ash Wilsona7402472014-09-16 15:18:34 -040037
Jon Perritt9bd7bd92014-09-28 20:10:27 -050038 Err error
39
Ash Wilsona7402472014-09-16 15:18:34 -040040 // Headers supplies additional HTTP headers to populate on each paged request.
41 Headers map[string]string
Ash Wilsonc8e68872014-09-16 10:36:56 -040042}
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 Wilson7049af42014-09-16 13:04:48 -040046func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r LastHTTPResponse) Page) Pager {
Ash Wilsonc8e68872014-09-16 10:36:56 -040047 return Pager{
Ash Wilson7049af42014-09-16 13:04:48 -040048 initialURL: initialURL,
49 client: client,
50 createPage: createPage,
Ash Wilsonc8e68872014-09-16 10:36:56 -040051 }
52}
53
Ash Wilson7049af42014-09-16 13:04:48 -040054func (p Pager) fetchNextPage(url string) (Page, error) {
Ash Wilsona7402472014-09-16 15:18:34 -040055 resp, err := Request(p.client, p.Headers, url)
Ash Wilson7049af42014-09-16 13:04:48 -040056 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 Wilsonc8e68872014-09-16 10:36:56 -040068// 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.
70func (p Pager) EachPage(handler func(Page) (bool, error)) error {
71 currentURL := p.initialURL
72 for {
73 currentPage, err := p.fetchNextPage(currentURL)
74 if err != nil {
75 return err
76 }
77
78 empty, err := currentPage.IsEmpty()
79 if err != nil {
80 return err
81 }
82 if empty {
83 return nil
84 }
85
86 ok, err := handler(currentPage)
87 if err != nil {
88 return err
89 }
90 if !ok {
91 return nil
92 }
93
94 currentURL, err = currentPage.NextPageURL()
95 if err != nil {
96 return err
97 }
98 if currentURL == "" {
99 return nil
100 }
101 }
102}