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" |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 8 | "net/url" |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 9 | |
| 10 | "github.com/mitchellh/mapstructure" |
Ash Wilson | 18f3252 | 2014-09-15 08:52:12 -0400 | [diff] [blame] | 11 | "github.com/racker/perigee" |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 12 | ) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 13 | |
| 14 | var ( |
| 15 | // ErrPageNotAvailable is returned from a Pager when a next or previous page is requested, but does not exist. |
| 16 | ErrPageNotAvailable = errors.New("The requested Collection page does not exist.") |
| 17 | ) |
| 18 | |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 19 | // LastHTTPResponse stores generic information derived from an HTTP response. |
| 20 | type LastHTTPResponse struct { |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 21 | url.URL |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 22 | http.Header |
Ash Wilson | f52b08b | 2014-09-15 10:00:43 -0400 | [diff] [blame] | 23 | Body interface{} |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // RememberHTTPResponse parses an HTTP response as JSON and returns a LastHTTPResponse containing the results. |
| 27 | // The main reason to do this instead of holding the response directly is that a response body can only be read once. |
| 28 | // Also, this centralizes the JSON decoding. |
| 29 | func RememberHTTPResponse(resp http.Response) (LastHTTPResponse, error) { |
Ash Wilson | f52b08b | 2014-09-15 10:00:43 -0400 | [diff] [blame] | 30 | var parsedBody interface{} |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 31 | |
| 32 | defer resp.Body.Close() |
| 33 | rawBody, err := ioutil.ReadAll(resp.Body) |
| 34 | if err != nil { |
| 35 | return LastHTTPResponse{}, err |
| 36 | } |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 37 | |
| 38 | if resp.Header.Get("Content-Type") == "application/json" { |
| 39 | err = json.Unmarshal(rawBody, &parsedBody) |
| 40 | if err != nil { |
| 41 | return LastHTTPResponse{}, err |
| 42 | } |
| 43 | } else { |
| 44 | parsedBody = rawBody |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 47 | return LastHTTPResponse{ |
| 48 | URL: *resp.Request.URL, |
| 49 | Header: resp.Header, |
| 50 | Body: parsedBody, |
| 51 | }, err |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | // request performs a Perigee request and extracts the http.Response from the result. |
| 55 | func request(client *ServiceClient, url string) (http.Response, error) { |
| 56 | resp, err := perigee.Request("GET", url, perigee.Options{ |
| 57 | MoreHeaders: client.Provider.AuthenticatedHeaders(), |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 58 | OkCodes: []int{200, 204}, |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 59 | }) |
| 60 | if err != nil { |
| 61 | return http.Response{}, err |
| 62 | } |
| 63 | return resp.HttpResponse, nil |
| 64 | } |
| 65 | |
Ash Wilson | 5bf6f66 | 2014-09-12 12:31:17 -0400 | [diff] [blame] | 66 | // Page must be satisfied by the result type of any resource collection. |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 67 | // 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] | 68 | type Page interface { |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 69 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 70 | // NextPageURL generates the URL for the page of data that follows this collection. |
| 71 | // Return "" if no such page exists. |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 72 | NextPageURL() (string, error) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 75 | // SinglePage is a page that contains all of the results from an operation. |
| 76 | type SinglePage LastHTTPResponse |
| 77 | |
| 78 | // NextPageURL always returns "" to indicate that there are no more pages to return. |
| 79 | func (current SinglePage) NextPageURL() (string, error) { |
| 80 | return "", nil |
| 81 | } |
| 82 | |
| 83 | // LinkedPage is a page in a collection that provides navigational "Next" and "Previous" links within its result. |
| 84 | type LinkedPage LastHTTPResponse |
| 85 | |
| 86 | // NextPageURL extracts the pagination structure from a JSON response and returns the "next" link, if one is present. |
| 87 | func (current LinkedPage) NextPageURL() (string, error) { |
| 88 | type response struct { |
| 89 | Links struct { |
| 90 | Next *string `mapstructure:"next,omitempty"` |
| 91 | } `mapstructure:"links"` |
| 92 | } |
| 93 | |
| 94 | var r response |
| 95 | err := mapstructure.Decode(current.Body, &r) |
| 96 | if err != nil { |
| 97 | return "", err |
| 98 | } |
| 99 | |
| 100 | if r.Links.Next == nil { |
| 101 | return "", nil |
| 102 | } |
| 103 | |
| 104 | return *r.Links.Next, nil |
| 105 | } |
| 106 | |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 107 | // MarkerPage is a page in a collection that's paginated by "limit" and "marker" query parameters. |
| 108 | type MarkerPage struct { |
| 109 | LastHTTPResponse |
| 110 | |
| 111 | // lastMark is a captured function that returns the final entry on a given page. |
| 112 | lastMark func(MarkerPage) (string, error) |
| 113 | } |
| 114 | |
| 115 | // NextPageURL generates the URL for the page of results after this one. |
| 116 | func (current MarkerPage) NextPageURL() (string, error) { |
| 117 | currentURL := current.LastHTTPResponse.URL |
| 118 | |
| 119 | mark, err := current.lastMark(current) |
| 120 | if err != nil { |
| 121 | return "", err |
| 122 | } |
| 123 | |
| 124 | q := currentURL.Query() |
| 125 | q.Set("marker", mark) |
| 126 | currentURL.RawQuery = q.Encode() |
| 127 | |
| 128 | return currentURL.String(), nil |
| 129 | } |
| 130 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 131 | // Pager knows how to advance through a specific resource collection, one page at a time. |
| 132 | type Pager struct { |
| 133 | initialURL string |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 134 | |
Ash Wilson | 3658d38 | 2014-09-15 08:12:33 -0400 | [diff] [blame] | 135 | fetchNextPage func(string) (Page, error) |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // NewPager constructs a manually-configured pager. |
| 139 | // Supply the URL for the first page and a function that requests a specific page given a URL. |
Ash Wilson | 3658d38 | 2014-09-15 08:12:33 -0400 | [diff] [blame] | 140 | func NewPager(initialURL string, fetchNextPage func(string) (Page, error)) Pager { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 141 | return Pager{ |
Ash Wilson | 3658d38 | 2014-09-15 08:12:33 -0400 | [diff] [blame] | 142 | initialURL: initialURL, |
| 143 | fetchNextPage: fetchNextPage, |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Ash Wilson | 36986c6 | 2014-09-15 09:56:34 -0400 | [diff] [blame] | 147 | // NewSinglePager constructs a Pager that "iterates" over a single Page. |
| 148 | // Supply the URL to request. |
| 149 | func NewSinglePager(client *ServiceClient, onlyURL string) Pager { |
| 150 | consumed := false |
| 151 | single := func(_ string) (Page, error) { |
| 152 | if !consumed { |
| 153 | consumed = true |
| 154 | resp, err := request(client, onlyURL) |
| 155 | if err != nil { |
| 156 | return SinglePage{}, err |
| 157 | } |
| 158 | |
| 159 | cp, err := RememberHTTPResponse(resp) |
| 160 | if err != nil { |
| 161 | return SinglePage{}, err |
| 162 | } |
| 163 | return SinglePage(cp), nil |
| 164 | } |
| 165 | return SinglePage{}, ErrPageNotAvailable |
| 166 | } |
| 167 | |
| 168 | return Pager{ |
| 169 | initialURL: "", |
| 170 | fetchNextPage: single, |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | // NewLinkedPager creates a Pager that uses a "links" element in the JSON response to locate the next page. |
| 175 | func NewLinkedPager(client *ServiceClient, initialURL string) Pager { |
| 176 | fetchNextPage := func(url string) (Page, error) { |
| 177 | resp, err := request(client, url) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | |
| 182 | cp, err := RememberHTTPResponse(resp) |
| 183 | if err != nil { |
| 184 | return nil, err |
| 185 | } |
| 186 | |
| 187 | return LinkedPage(cp), nil |
| 188 | } |
| 189 | |
| 190 | return Pager{ |
| 191 | initialURL: initialURL, |
| 192 | fetchNextPage: fetchNextPage, |
| 193 | } |
| 194 | } |
| 195 | |
Ash Wilson | 993cf32 | 2014-09-15 14:34:12 -0400 | [diff] [blame^] | 196 | // NewMarkerPager creates a Pager that iterates over successive pages by issuing requests with a "marker" parameter set to the |
| 197 | // final element of the previous Page. |
| 198 | func NewMarkerPager(client *ServiceClient, initialURL string, lastMark func(MarkerPage) (string, error)) Pager { |
| 199 | fetchNextPage := func(currentURL string) (Page, error) { |
| 200 | resp, err := request(client, currentURL) |
| 201 | if err != nil { |
| 202 | return nil, err |
| 203 | } |
| 204 | |
| 205 | last, err := RememberHTTPResponse(resp) |
| 206 | if err != nil { |
| 207 | return nil, err |
| 208 | } |
| 209 | |
| 210 | return MarkerPage{LastHTTPResponse: last, lastMark: lastMark}, nil |
| 211 | } |
| 212 | |
| 213 | return Pager{ |
| 214 | initialURL: initialURL, |
| 215 | fetchNextPage: fetchNextPage, |
| 216 | } |
| 217 | } |
| 218 | |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 219 | // EachPage iterates over each page returned by a Pager, yielding one at a time to a handler function. |
| 220 | // Return "false" from the handler to prematurely stop iterating. |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 221 | func (p Pager) EachPage(handler func(Page) (bool, error)) error { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 222 | currentURL := p.initialURL |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 223 | for { |
Ash Wilson | 3658d38 | 2014-09-15 08:12:33 -0400 | [diff] [blame] | 224 | currentPage, err := p.fetchNextPage(currentURL) |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 225 | if err != nil { |
| 226 | return err |
| 227 | } |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 228 | |
Ash Wilson | 6b35e50 | 2014-09-12 15:15:23 -0400 | [diff] [blame] | 229 | ok, err := handler(currentPage) |
| 230 | if err != nil { |
| 231 | return err |
| 232 | } |
| 233 | if !ok { |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 234 | return nil |
| 235 | } |
| 236 | |
Ash Wilson | 976d2e6 | 2014-09-12 13:29:29 -0400 | [diff] [blame] | 237 | currentURL, err = currentPage.NextPageURL() |
| 238 | if err != nil { |
| 239 | return err |
| 240 | } |
Ash Wilson | e30b76b | 2014-09-12 08:36:17 -0400 | [diff] [blame] | 241 | if currentURL == "" { |
| 242 | return nil |
| 243 | } |
Ash Wilson | 64d67b2 | 2014-09-05 13:04:12 -0400 | [diff] [blame] | 244 | } |
| 245 | } |