Jamie Hannaford | 8c072a3 | 2014-10-16 14:33:32 +0200 | [diff] [blame] | 1 | package pagination |
| 2 | |
| 3 | // MarkerPage is a stricter Page interface that describes additional functionality required for use with NewMarkerPager. |
| 4 | // For convenience, embed the MarkedPageBase struct. |
| 5 | type MarkerPage interface { |
| 6 | Page |
| 7 | |
| 8 | // LastMarker returns the last "marker" value on this page. |
| 9 | LastMarker() (string, error) |
| 10 | } |
| 11 | |
| 12 | // MarkerPageBase is a page in a collection that's paginated by "limit" and "marker" query parameters. |
| 13 | type MarkerPageBase struct { |
| 14 | LastHTTPResponse |
| 15 | |
| 16 | // Owner is a reference to the embedding struct. |
| 17 | Owner MarkerPage |
| 18 | } |
| 19 | |
| 20 | // NextPageURL generates the URL for the page of results after this one. |
| 21 | func (current MarkerPageBase) NextPageURL() (string, error) { |
| 22 | currentURL := current.URL |
| 23 | |
| 24 | mark, err := current.Owner.LastMarker() |
| 25 | if err != nil { |
| 26 | return "", err |
| 27 | } |
| 28 | |
| 29 | q := currentURL.Query() |
| 30 | q.Set("marker", mark) |
| 31 | currentURL.RawQuery = q.Encode() |
| 32 | |
| 33 | return currentURL.String(), nil |
| 34 | } |