blob: 5c20e16c6cd8e8e5a77487f12fb32ae4a011b8cb [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 {
Ash Wilson7049af42014-09-16 13:04:48 -040032 client *gophercloud.ServiceClient
33
Ash Wilsonfc4191f2014-10-10 15:05:27 -040034 initialURL string
Ash Wilson5bc7ba82014-10-09 13:57:34 -040035
Ash Wilsonb8b16f82014-10-20 10:19:49 -040036 createPage func(r PageResult) 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 Wilsonb8b16f82014-10-20 10:19:49 -040046func NewPager(client *gophercloud.ServiceClient, initialURL string, createPage func(r PageResult) Page) Pager {
Ash Wilsonc8e68872014-09-16 10:36:56 -040047 return Pager{
Ash Wilson7049af42014-09-16 13:04:48 -040048 client: client,
Ash Wilsonfc4191f2014-10-10 15:05:27 -040049 initialURL: initialURL,
50 createPage: createPage,
51 }
52}
53
54// WithPageCreator returns a new Pager that substitutes a different page creation function. This is
55// useful for overriding List functions in delegation.
Ash Wilsonb8b16f82014-10-20 10:19:49 -040056func (p Pager) WithPageCreator(createPage func(r PageResult) Page) Pager {
Ash Wilsonfc4191f2014-10-10 15:05:27 -040057 return Pager{
58 client: p.client,
59 initialURL: p.initialURL,
60 createPage: createPage,
Ash Wilsonc8e68872014-09-16 10:36:56 -040061 }
62}
63
Ash Wilson7049af42014-09-16 13:04:48 -040064func (p Pager) fetchNextPage(url string) (Page, error) {
Ash Wilsona7402472014-09-16 15:18:34 -040065 resp, err := Request(p.client, p.Headers, url)
Ash Wilson7049af42014-09-16 13:04:48 -040066 if err != nil {
67 return nil, err
68 }
69
Ash Wilsonb8b16f82014-10-20 10:19:49 -040070 remembered, err := PageResultFrom(resp)
Ash Wilson7049af42014-09-16 13:04:48 -040071 if err != nil {
72 return nil, err
73 }
74
Ash Wilsonfc4191f2014-10-10 15:05:27 -040075 return p.createPage(remembered), nil
Ash Wilson7049af42014-09-16 13:04:48 -040076}
77
Ash Wilsonc8e68872014-09-16 10:36:56 -040078// EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function.
79// Return "false" from the handler to prematurely stop iterating.
80func (p Pager) EachPage(handler func(Page) (bool, error)) error {
Jon Perritt6f9e4ff2014-09-30 13:29:47 -050081 if p.Err != nil {
82 return p.Err
83 }
Ash Wilsonfc4191f2014-10-10 15:05:27 -040084 currentURL := p.initialURL
Ash Wilsonc8e68872014-09-16 10:36:56 -040085 for {
86 currentPage, err := p.fetchNextPage(currentURL)
87 if err != nil {
88 return err
89 }
90
91 empty, err := currentPage.IsEmpty()
92 if err != nil {
93 return err
94 }
95 if empty {
96 return nil
97 }
98
99 ok, err := handler(currentPage)
100 if err != nil {
101 return err
102 }
103 if !ok {
104 return nil
105 }
106
107 currentURL, err = currentPage.NextPageURL()
108 if err != nil {
109 return err
110 }
111 if currentURL == "" {
112 return nil
113 }
114 }
115}