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