Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | |
| 9 | "github.com/mitchellh/mapstructure" |
| 10 | ) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 11 | |
| 12 | var ( |
| 13 | // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist. |
| 14 | ErrPageNotAvailable = errors.New("The requested Collection page does not exist.") |
| 15 | ) |
| 16 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 17 | // Page must be satisfied by the result type of any resource collection. |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 18 | // It allows clients to interact with the resource uniformly, regardless of whether or not or how it's paginated. |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 19 | type Page interface { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 20 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 21 | // NextPageURL generates the URL for the page of data that follows this collection. |
| 22 | // Return "" if no such page exists. |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 23 | NextPageURL() (string, error) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 24 | } |
| 25 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 26 | // Pager knows how to advance through a specific resource collection, one page at a time. |
| 27 | type Pager struct { |
| 28 | initialURL string |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 29 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 30 | advance func(string) (Page, error) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // NewPager constructs a manually-configured pager. |
| 34 | // Supply the URL for the first page and a function that requests a specific page given a URL. |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 35 | func NewPager(initialURL string, advance func(string) (Page, error)) Pager { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 36 | return Pager{ |
| 37 | initialURL: initialURL, |
| 38 | advance: advance, |
| 39 | } |
| 40 | } |
| 41 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 42 | // EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function. |
| 43 | // Return "false" from the handler to prematurely stop iterating. |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 44 | func (p Pager) EachPage(handler func(Page) (bool, error)) error { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 45 | currentURL := p.initialURL |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 46 | for { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 47 | currentPage, err := p.advance(currentURL) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 48 | if err != nil { |
| 49 | return err |
| 50 | } |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 51 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 52 | ok, err := handler(currentPage) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if !ok { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 57 | return nil |
| 58 | } |
| 59 | |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 60 | currentURL, err = currentPage.NextPageURL() |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 64 | if currentURL == "" { |
| 65 | return nil |
| 66 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 70 | // ConcretePage stores generic information derived from an HTTP response. |
| 71 | type ConcretePage struct { |
| 72 | http.Header |
| 73 | Body map[string]interface{} |
| 74 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 75 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 76 | // NewConcretePage parses an HTTP response as JSON and returns a ConcretePage containing the results. |
| 77 | func NewConcretePage(resp http.Response) (ConcretePage, error) { |
| 78 | var parsedBody map[string]interface{} |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 79 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 80 | defer resp.Body.Close() |
| 81 | rawBody, err := ioutil.ReadAll(resp.Body) |
| 82 | if err != nil { |
| 83 | return ConcretePage{}, err |
| 84 | } |
| 85 | err = json.Unmarshal(rawBody, &parsedBody) |
| 86 | if err != nil { |
| 87 | return ConcretePage{}, err |
| 88 | } |
| 89 | |
| 90 | return ConcretePage{Header: resp.Header, Body: parsedBody}, err |
| 91 | } |
| 92 | |
| 93 | // SinglePage is a page that contains all of the results from an operation. |
| 94 | type SinglePage ConcretePage |
| 95 | |
| 96 | // NextPageURL always returns "" to indicate that there are no more pages to return. |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 97 | func (current SinglePage) NextPageURL() (string, error) { |
| 98 | return "", nil |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // NewSinglePager constructs a Pager that "iterates" over a single Page. |
| 102 | // Supply a function that returns the only page. |
| 103 | func NewSinglePager(only func() (http.Response, error)) Pager { |
| 104 | consumed := false |
| 105 | single := func(_ string) (Page, error) { |
| 106 | if !consumed { |
| 107 | consumed = true |
| 108 | resp, err := only() |
| 109 | if err != nil { |
| 110 | return SinglePage{}, err |
| 111 | } |
| 112 | |
| 113 | cp, err := NewConcretePage(resp) |
| 114 | if err != nil { |
| 115 | return SinglePage{}, err |
| 116 | } |
| 117 | return SinglePage(cp), nil |
| 118 | } |
| 119 | return SinglePage{}, ErrPageNotAvailable |
| 120 | } |
| 121 | |
| 122 | return Pager{ |
| 123 | initialURL: "", |
| 124 | advance: single, |
| 125 | } |
| 126 | } |
| 127 | |
Ash Wilson | 583dc73 | 2014-09-12 13:30:05 -0400 | [diff] [blame] | 128 | // LinkedPage is a page in a collection that provides navigational "Next" and "Previous" links within its result. |
| 129 | type LinkedPage ConcretePage |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 130 | |
| 131 | // NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present. |
Ash Wilson | 583dc73 | 2014-09-12 13:30:05 -0400 | [diff] [blame] | 132 | func (current LinkedPage) NextPageURL() (string, error) { |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 133 | type response struct { |
| 134 | Links struct { |
| 135 | Next *string `mapstructure:"next,omitempty"` |
| 136 | } `mapstructure:"links"` |
| 137 | } |
| 138 | |
| 139 | var r response |
| 140 | err := mapstructure.Decode(current.Body, &r) |
| 141 | if err != nil { |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 142 | return "", err |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | if r.Links.Next == nil { |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 146 | return "", nil |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 149 | return *r.Links.Next, nil |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // NewLinkedPager creates a Pager that uses a "links" element in the JSON response to locate the next page. |
| 153 | func NewLinkedPager(initialURL string, request func(string) (http.Response, error)) Pager { |
| 154 | advance := func(url string) (Page, error) { |
| 155 | resp, err := request(url) |
| 156 | if err != nil { |
| 157 | return nil, err |
| 158 | } |
| 159 | |
| 160 | cp, err := NewConcretePage(resp) |
| 161 | if err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | |
Ash Wilson | 583dc73 | 2014-09-12 13:30:05 -0400 | [diff] [blame] | 165 | return LinkedPage(cp), nil |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | return Pager{ |
| 169 | initialURL: initialURL, |
| 170 | advance: advance, |
| 171 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 172 | } |