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 | |
| 10 | "github.com/racker/perigee" |
| 11 | "github.com/rackspace/gophercloud" |
| 12 | ) |
| 13 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 14 | // PageResult stores the HTTP response that returned the current page of results. |
| 15 | type PageResult struct { |
| 16 | gophercloud.Result |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 17 | url.URL |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 18 | } |
| 19 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 20 | // PageResultFrom parses an HTTP response as JSON and returns a PageResult containing the |
| 21 | // results, interpreting it as JSON if the content type indicates. |
| 22 | func PageResultFrom(resp http.Response) (PageResult, error) { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 23 | var parsedBody interface{} |
| 24 | |
| 25 | defer resp.Body.Close() |
| 26 | rawBody, err := ioutil.ReadAll(resp.Body) |
| 27 | if err != nil { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 28 | return PageResult{}, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 29 | } |
| 30 | |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 31 | if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 32 | err = json.Unmarshal(rawBody, &parsedBody) |
| 33 | if err != nil { |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 34 | return PageResult{}, err |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 35 | } |
| 36 | } else { |
| 37 | parsedBody = rawBody |
| 38 | } |
| 39 | |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 40 | return PageResult{ |
| 41 | Result: gophercloud.Result{ |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 42 | Body: parsedBody, |
| 43 | Header: resp.Header, |
Ash Wilson | b8b16f8 | 2014-10-20 10:19:49 -0400 | [diff] [blame] | 44 | }, |
| 45 | URL: *resp.Request.URL, |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 46 | }, err |
| 47 | } |
| 48 | |
| 49 | // Request performs a Perigee request and extracts the http.Response from the result. |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 50 | func Request(client *gophercloud.ServiceClient, headers map[string]string, url string) (http.Response, error) { |
Ash Wilson | 77857dc | 2014-10-22 09:09:02 -0400 | [diff] [blame] | 51 | h := client.AuthenticatedHeaders() |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 52 | for key, value := range headers { |
| 53 | h[key] = value |
| 54 | } |
| 55 | |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 56 | resp, err := perigee.Request("GET", url, perigee.Options{ |
Ash Wilson | a740247 | 2014-09-16 15:18:34 -0400 | [diff] [blame] | 57 | MoreHeaders: h, |
Ash Wilson | c8e6887 | 2014-09-16 10:36:56 -0400 | [diff] [blame] | 58 | OkCodes: []int{200, 204}, |
| 59 | }) |
| 60 | if err != nil { |
| 61 | return http.Response{}, err |
| 62 | } |
| 63 | return resp.HttpResponse, nil |
| 64 | } |