blob: fc88a5536b7cbd14b81f440eb0f6380d6c996e5a [file] [log] [blame]
Ash Wilsonc8e68872014-09-16 10:36:56 -04001package pagination
2
Ash Wilson7049af42014-09-16 13:04:48 -04003import "github.com/mitchellh/mapstructure"
Ash Wilsonc8e68872014-09-16 10:36:56 -04004
5// LinkedPageBase may be embedded to implement a page that provides navigational "Next" and "Previous" links within its result.
6type LinkedPageBase LastHTTPResponse
7
8// NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present.
9// It assumes that the links are available in a "links" element of the top-level response object.
10// If this is not the case, override NextPageURL on your result type.
11func (current LinkedPageBase) NextPageURL() (string, error) {
12 type response struct {
13 Links struct {
14 Next *string `mapstructure:"next,omitempty"`
15 } `mapstructure:"links"`
16 }
17
18 var r response
19 err := mapstructure.Decode(current.Body, &r)
20 if err != nil {
21 return "", err
22 }
23
24 if r.Links.Next == nil {
25 return "", nil
26 }
27
28 return *r.Links.Next, nil
29}