blob: 22d6d84563e59f81f1cae20dc652572e962a68b8 [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 {
Jon Perritt6f9e4ff2014-09-30 13:29:47 -050071 if p.Err != nil {
72 return p.Err
73 }
Ash Wilsonc8e68872014-09-16 10:36:56 -040074 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}