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