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 | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 3 | import "net/http" |
| 4 | |
| 5 | // Result acts as a base struct that other results can embed. |
| 6 | type Result struct { |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 7 | // Body is the payload of the HTTP response from the server. In most cases, this will be the |
| 8 | // deserialized JSON structure. |
| 9 | Body interface{} |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 10 | |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 11 | // Header contains the HTTP header structure from the original response. |
| 12 | Header http.Header |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 13 | |
| 14 | // Err is an error that occurred during the operation. It's deferred until extraction to make |
| 15 | // it easier to chain operations. |
| 16 | Err error |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 17 | } |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 18 | |
| 19 | // RFC3339Milli describes a time format used by API responses. |
| 20 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 21 | |
| 22 | // Link represents a structure that enables paginated collections how to |
| 23 | // traverse backward or forward. The "Rel" field is usually either "next". |
| 24 | type Link struct { |
| 25 | Href string `mapstructure:"href"` |
| 26 | Rel string `mapstructure:"rel"` |
| 27 | } |
| 28 | |
| 29 | // ExtractNextURL attempts to extract the next URL from a JSON structure. It |
| 30 | // follows the common structure of nesting back and next links. |
| 31 | func ExtractNextURL(links []Link) (string, error) { |
| 32 | var url string |
| 33 | |
| 34 | for _, l := range links { |
| 35 | if l.Rel == "next" { |
| 36 | url = l.Href |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if url == "" { |
| 41 | return "", nil |
| 42 | } |
| 43 | |
| 44 | return url, nil |
| 45 | } |