Jamie Hannaford | 4baa123 | 2014-09-23 15:23:04 +0200 | [diff] [blame] | 1 | package gophercloud |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 2 | |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | ) |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 7 | |
| 8 | // Result acts as a base struct that other results can embed. |
| 9 | type Result struct { |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 10 | // Body is the payload of the HTTP response from the server. In most cases, this will be the |
| 11 | // deserialized JSON structure. |
| 12 | Body interface{} |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 13 | |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 14 | // Header contains the HTTP header structure from the original response. |
| 15 | Header http.Header |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 16 | |
| 17 | // Err is an error that occurred during the operation. It's deferred until extraction to make |
| 18 | // it easier to chain operations. |
| 19 | Err error |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 20 | } |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 21 | |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 22 | // PrettyPrintJSON creates a string containing the full response body as pretty-printed JSON. |
| 23 | func (r Result) PrettyPrintJSON() string { |
| 24 | pretty, err := json.MarshalIndent(r.Body, "", " ") |
| 25 | if err != nil { |
| 26 | panic(err.Error()) |
| 27 | } |
| 28 | return string(pretty) |
| 29 | } |
| 30 | |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 31 | // ErrResult represents results that only contain a potential error and |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 32 | // nothing else. Usually if the operation executed successfully, the Err field |
| 33 | // will be nil; otherwise it will be stocked with a relevant error. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 34 | type ErrResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 35 | Result |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 36 | } |
| 37 | |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 38 | // ExtractErr is a function that extracts error information from a result. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 39 | func (r ErrResult) ExtractErr() error { |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 40 | return r.Err |
| 41 | } |
| 42 | |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 43 | // HeaderResult represents a result that only contains an `error` (possibly nil) |
| 44 | // and an http.Header. This is used, for example, by the `objectstorage` packages |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 45 | // in `openstack`, because most of the operations don't return response bodies. |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 46 | type HeaderResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 47 | Result |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // ExtractHeader will return the http.Header and error from the HeaderResult. |
| 51 | // Usage: header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader() |
| 52 | func (hr HeaderResult) ExtractHeader() (http.Header, error) { |
| 53 | return hr.Header, hr.Err |
| 54 | } |
| 55 | |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 56 | // RFC3339Milli describes a time format used by API responses. |
| 57 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 58 | |
| 59 | // Link represents a structure that enables paginated collections how to |
| 60 | // traverse backward or forward. The "Rel" field is usually either "next". |
| 61 | type Link struct { |
| 62 | Href string `mapstructure:"href"` |
| 63 | Rel string `mapstructure:"rel"` |
| 64 | } |
| 65 | |
| 66 | // ExtractNextURL attempts to extract the next URL from a JSON structure. It |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 67 | // follows the common convention of nesting back and next URLs in a "links" |
| 68 | // JSON array. |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 69 | func ExtractNextURL(links []Link) (string, error) { |
| 70 | var url string |
| 71 | |
| 72 | for _, l := range links { |
| 73 | if l.Rel == "next" { |
| 74 | url = l.Href |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | if url == "" { |
| 79 | return "", nil |
| 80 | } |
| 81 | |
| 82 | return url, nil |
| 83 | } |