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