Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 1 | package pagination |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io/ioutil" |
| 6 | "net/http" |
| 7 | "net/url" |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 8 | "strings" |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 9 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud" |
| 11 | ) |
| 12 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 13 | // PageResult stores the HTTP response that returned the current page of results. |
| 14 | type PageResult struct { |
| 15 | gophercloud.Result |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 16 | url.URL |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 17 | } |
| 18 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 19 | // PageResultFrom parses an HTTP response as JSON and returns a PageResult containing the |
| 20 | // results, interpreting it as JSON if the content type indicates. |
Ash Wilson | e58e5c3 | 2015-02-12 14:34:39 -0500 | [diff] [blame] | 21 | func PageResultFrom(resp *http.Response) (PageResult, error) { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 22 | var parsedBody interface{} |
| 23 | |
| 24 | defer resp.Body.Close() |
| 25 | rawBody, err := ioutil.ReadAll(resp.Body) |
| 26 | if err != nil { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 27 | return PageResult{}, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 30 | if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 31 | err = json.Unmarshal(rawBody, &parsedBody) |
| 32 | if err != nil { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 33 | return PageResult{}, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 34 | } |
| 35 | } else { |
| 36 | parsedBody = rawBody |
| 37 | } |
| 38 | |
Ash Wilson | 2c749a0 | 2015-06-24 10:16:16 -0400 | [diff] [blame] | 39 | return PageResultFromParsed(resp, parsedBody), err |
| 40 | } |
| 41 | |
| 42 | // PageResultFromParsed constructs a PageResult from an HTTP response that has already had its |
| 43 | // body parsed as JSON (and closed). |
| 44 | func PageResultFromParsed(resp *http.Response, body interface{}) PageResult { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 45 | return PageResult{ |
| 46 | Result: gophercloud.Result{ |
Ash Wilson | 2c749a0 | 2015-06-24 10:16:16 -0400 | [diff] [blame] | 47 | Body: body, |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 48 | Header: resp.Header, |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 49 | }, |
| 50 | URL: *resp.Request.URL, |
Ash Wilson | 2c749a0 | 2015-06-24 10:16:16 -0400 | [diff] [blame] | 51 | } |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 52 | } |
| 53 | |
Ash Wilson | e58e5c3 | 2015-02-12 14:34:39 -0500 | [diff] [blame] | 54 | // Request performs an HTTP request and extracts the http.Response from the result. |
| 55 | func Request(client *gophercloud.ServiceClient, headers map[string]string, url string) (*http.Response, error) { |
| 56 | return client.Request("GET", url, gophercloud.RequestOpts{ |
| 57 | MoreHeaders: headers, |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 58 | OkCodes: []int{200, 204}, |
| 59 | }) |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 60 | } |