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 |
Ash Wilson | 0fe6c96 | 2014-10-31 15:34:24 -0400 | [diff] [blame] | 37 | // debugging extraction bugs. If you include its output in an issue related to |
| 38 | // a buggy extraction function, we will all love you forever. |
Ash Wilson | e8192ac | 2014-10-21 09:02:01 -0400 | [diff] [blame] | 39 | func (r Result) PrettyPrintJSON() string { |
| 40 | pretty, err := json.MarshalIndent(r.Body, "", " ") |
| 41 | if err != nil { |
| 42 | panic(err.Error()) |
| 43 | } |
| 44 | return string(pretty) |
| 45 | } |
| 46 | |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 47 | // ErrResult is an internal type to be used by individual resource packages, but |
| 48 | // its methods will be available on a wide variety of user-facing embedding |
| 49 | // types. |
| 50 | // |
| 51 | // It represents results that only contain a potential error and |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 52 | // nothing else. Usually, if the operation executed successfully, the Err field |
| 53 | // 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] | 54 | // ExtractErr method |
| 55 | // to cleanly pull it out. |
Jon Perritt | ba2395e | 2014-10-27 15:23:21 -0500 | [diff] [blame] | 56 | type ErrResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 57 | Result |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 58 | } |
| 59 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 60 | // 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] | 61 | func (r ErrResult) ExtractErr() error { |
Jamie Hannaford | 021b35c | 2014-10-27 14:01:53 +0100 | [diff] [blame] | 62 | return r.Err |
| 63 | } |
| 64 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 65 | /* |
Ash Wilson | 64ba49f | 2014-10-31 15:31:46 -0400 | [diff] [blame] | 66 | HeaderResult is an internal type to be used by individual resource packages, but |
| 67 | 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] | 68 | |
| 69 | It represents a result that only contains an error (possibly nil) and an |
| 70 | http.Header. This is used, for example, by the objectstorage packages in |
| 71 | openstack, because most of the operations don't return response bodies, but do |
| 72 | have relevant information in headers. |
| 73 | */ |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 74 | type HeaderResult struct { |
Jon Perritt | 0c2b037 | 2014-10-27 15:57:29 -0500 | [diff] [blame] | 75 | Result |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // ExtractHeader will return the http.Header and error from the HeaderResult. |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 79 | // |
| 80 | // header, err := objects.Create(client, "my_container", objects.CreateOpts{}).ExtractHeader() |
Jon Perritt | d50f93e | 2014-10-27 14:19:27 -0500 | [diff] [blame] | 81 | func (hr HeaderResult) ExtractHeader() (http.Header, error) { |
| 82 | return hr.Header, hr.Err |
| 83 | } |
| 84 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 85 | // RFC3339Milli describes a common time format used by some API responses. |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 86 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 87 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 88 | /* |
| 89 | Link is an internal type to be used in packages of collection resources that are |
| 90 | paginated in a certain way. |
| 91 | |
| 92 | It's a response substructure common to many paginated collection results that is |
| 93 | used to point to related pages. Usually, the one we care about is the one with |
| 94 | Rel field set to "next". |
| 95 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 96 | type Link struct { |
| 97 | Href string `mapstructure:"href"` |
| 98 | Rel string `mapstructure:"rel"` |
| 99 | } |
| 100 | |
Ash Wilson | 3ce1bd8 | 2014-10-31 12:20:00 -0400 | [diff] [blame] | 101 | /* |
| 102 | ExtractNextURL is an internal function useful for packages of collection |
| 103 | resources that are paginated in a certain way. |
| 104 | |
| 105 | It attempts attempts to extract the "next" URL from slice of Link structs, or |
| 106 | "" if no such URL is present. |
| 107 | */ |
Jamie Hannaford | 369c9c6 | 2014-10-08 15:14:43 +0200 | [diff] [blame] | 108 | func ExtractNextURL(links []Link) (string, error) { |
| 109 | var url string |
| 110 | |
| 111 | for _, l := range links { |
| 112 | if l.Rel == "next" { |
| 113 | url = l.Href |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if url == "" { |
| 118 | return "", nil |
| 119 | } |
| 120 | |
| 121 | return url, nil |
| 122 | } |