blob: 6350e480b5ee1197ab360071806023326bf612d5 [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
3import "github.com/rackspace/gophercloud"
4
5// MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager.
6// For convenience, embed the MarkedPageBase struct.
7type MarkerPage interface {
8 Page
9
Ash Wilson74863512014-09-16 11:45:51 -040010 // LastMarker returns the last "marker" value on this page.
11 LastMarker() (string, error)
Ash Wilsonc8e68872014-09-16 10:36:56 -040012}
13
14// MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters.
15type MarkerPageBase struct {
16 LastHTTPResponse
17
18 // A reference to the embedding struct.
19 Self MarkerPage
20}
21
22// NextPageURL generates the URL for the page of results after this one.
23func (current MarkerPageBase) NextPageURL() (string, error) {
24 currentURL := current.URL
25
Ash Wilson74863512014-09-16 11:45:51 -040026 mark, err := current.Self.LastMarker()
Ash Wilsonc8e68872014-09-16 10:36:56 -040027 if err != nil {
28 return "", err
29 }
30
31 q := currentURL.Query()
32 q.Set("marker", mark)
33 currentURL.RawQuery = q.Encode()
34
35 return currentURL.String(), nil
36}
37
38// NewMarkerPager creates a Pager that iterates over successive pages by issuing requests with a "marker" parameter set to the
39// final element of the previous Page.
40func NewMarkerPager(client *gophercloud.ServiceClient, initialURL string, createPage func(resp LastHTTPResponse) MarkerPage) Pager {
41
42 fetchNextPage := func(currentURL string) (Page, error) {
43 resp, err := Request(client, currentURL)
44 if err != nil {
45 return nullPage{}, err
46 }
47
48 last, err := RememberHTTPResponse(resp)
49 if err != nil {
50 return nullPage{}, err
51 }
52
53 return createPage(last), nil
54 }
55
56 return Pager{
57 initialURL: initialURL,
58 fetchNextPage: fetchNextPage,
59 }
60}