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