10 "github.com/gophercloud/gophercloud"
13 // PageResult stores the HTTP response that returned the current page of results.
14 type PageResult struct {
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.
21 func PageResultFrom(resp *http.Response) (PageResult, error) {
22 var parsedBody interface{}
24 defer resp.Body.Close()
25 rawBody, err := ioutil.ReadAll(resp.Body)
27 return PageResult{}, err
30 if strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
31 err = json.Unmarshal(rawBody, &parsedBody)
33 return PageResult{}, err
39 return PageResultFromParsed(resp, parsedBody), err
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 {
46 Result: gophercloud.Result{
50 URL: *resp.Request.URL,
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.Get(url, nil, &gophercloud.RequestOpts{
58 OkCodes: []int{200, 204, 300},