blob: 52ebb96b147d907b20a5d34e8cf8abc3000290bc [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 Wilsonc8e68872014-09-16 10:36:56 -040037}
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 Wilson7049af42014-09-16 13:04:48 -040041func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r LastHTTPResponse) Page) Pager {
Ash Wilsonc8e68872014-09-16 10:36:56 -040042 return Pager{
Ash Wilson7049af42014-09-16 13:04:48 -040043 initialURL: initialURL,
44 client: client,
45 createPage: createPage,
Ash Wilsonc8e68872014-09-16 10:36:56 -040046 }
47}
48
Ash Wilson7049af42014-09-16 13:04:48 -040049func (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 Wilsonc8e68872014-09-16 10:36:56 -040063// 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.
65func (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}