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 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 8 | /* |
| 9 | Result is an internal type to be used by individual resource packages. |
| 10 | |
| 11 | It acts as a base struct that other Result types, returned from request |
| 12 | functions, can embed for convenience. All Results capture basic information |
| 13 | from the HTTP transaction that was performed, including the response body, |
| 14 | HTTP headers, and any errors that happened. |
| 15 | |
| 16 | Generally, each Result type will have an Extract method that can be used to |
| 17 | further interpret the result's payload in a specific context. Extensions or |
| 18 | providers can then provide additional extraction functions to pull out |
| 19 | provider- or extension-specific information as well. |
| 20 | */ |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 21 | type Result struct { |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 22 | // Body is the payload of the HTTP response from the server. In most cases, |
| 23 | // this will be the deserialized JSON structure. |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 24 | Body interface{} |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 25 | |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 26 | // Header contains the HTTP header structure from the original response. |
| 27 | Header http.Header |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 28 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 29 | // Err is an error that occurred during the operation. It's deferred until |
| 30 | // extraction to make it easier to chain the Extract call. |
Ash Wilson | eab6a70 | 2014-10-20 08:18:30 -0400 | [diff] [blame] | 31 | Err error |
Jamie Hannaford | b3120f5 | 2014-09-23 15:17:57 +0200 | [diff] [blame] | 32 | } |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 33 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 34 | // PrettyPrintJSON creates a string containing the full response body as |
| 35 | // pretty-printed JSON. It's useful for capturing test fixtures and for |
| 36 | // debugging extraction bugs. |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 37 | func (r Result) PrettyPrintJSON() string { |
| 38 | pretty, err := json.MarshalIndent(r.Body, "", " ") |
| 39 | if err != nil { |
| 40 | panic(err.Error()) |
| 41 | } |
| 42 | return string(pretty) |
| 43 | } |
| 44 | |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 45 | // ErrResult represents results that only contain a potential error and |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 46 | // nothing else. Usually, if the operation executed successfully, the Err field |
| 47 | // will be nil; otherwise it will be stocked with a relevant error. Use the |
| 48 | // ExtractErr method to cleanly pull it out. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 49 | type ErrResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 50 | Result |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 51 | } |
| 52 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 53 | // ExtractErr is a function that extracts error information, or nil, from a result. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 54 | func (r ErrResult) ExtractErr() error { |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 55 | return r.Err |
| 56 | } |
| 57 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 58 | /* |
| 59 | HeaderResult is an internal type to be used by individual resource packages. |
| 60 | |
| 61 | It represents a result that only contains an error (possibly nil) and an |
| 62 | http.Header. This is used, for example, by the objectstorage packages in |
| 63 | openstack, because most of the operations don't return response bodies, but do |
| 64 | have relevant information in headers. |
| 65 | */ |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 66 | type HeaderResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 67 | Result |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | // ExtractHeader will return the http.Header and error from the HeaderResult. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 71 | // |
| 72 | // header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader() |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 73 | func (hr HeaderResult) ExtractHeader() (http.Header, error) { |
| 74 | return hr.Header, hr.Err |
| 75 | } |
| 76 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 77 | // RFC3339Milli describes a common time format used by some API responses. |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 78 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 79 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 80 | /* |
| 81 | Link is an internal type to be used in packages of collection resources that are |
| 82 | paginated in a certain way. |
| 83 | |
| 84 | It's a response substructure common to many paginated collection results that is |
| 85 | used to point to related pages. Usually, the one we care about is the one with |
| 86 | Rel field set to "next". |
| 87 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 88 | type Link struct { |
| 89 | Href string `mapstructure:"href"` |
| 90 | Rel string `mapstructure:"rel"` |
| 91 | } |
| 92 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame^] | 93 | /* |
| 94 | ExtractNextURL is an internal function useful for packages of collection |
| 95 | resources that are paginated in a certain way. |
| 96 | |
| 97 | It attempts attempts to extract the "next" URL from slice of Link structs, or |
| 98 | "" if no such URL is present. |
| 99 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 100 | func ExtractNextURL(links []Link) (string, error) { |
| 101 | var url string |
| 102 | |
| 103 | for _, l := range links { |
| 104 | if l.Rel == "next" { |
| 105 | url = l.Href |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | if url == "" { |
| 110 | return "", nil |
| 111 | } |
| 112 | |
| 113 | return url, nil |
| 114 | } |