Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "io/ioutil" |
| 9 | "net/http" |
| 10 | ) |
| 11 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 12 | // ProviderClient stores details that are required to interact with any |
| 13 | // services within a specific provider's API. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 14 | // |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 15 | // Generally, you acquire a ProviderClient by calling the NewClient method in |
| 16 | // the appropriate provider's child package, providing whatever authentication |
| 17 | // credentials are required. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 18 | type ProviderClient struct { |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 19 | // IdentityBase is the base URL used for a particular provider's identity |
| 20 | // service - it will be used when issuing authenticatation requests. It |
| 21 | // should point to the root resource of the identity service, not a specific |
| 22 | // identity version. |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 23 | IdentityBase string |
| 24 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 25 | // IdentityEndpoint is the identity endpoint. This may be a specific version |
| 26 | // of the identity service. If this is the case, this endpoint is used rather |
| 27 | // than querying versions first. |
Ash Wilson | c6372fe | 2014-09-03 11:24:52 -0400 | [diff] [blame] | 28 | IdentityEndpoint string |
| 29 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 30 | // TokenID is the ID of the most recently issued valid token. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 31 | TokenID string |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 32 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 33 | // EndpointLocator describes how this provider discovers the endpoints for |
| 34 | // its constituent services. |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 35 | EndpointLocator EndpointLocator |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 36 | |
| 37 | // HTTPClient allows users to interject arbitrary http, https, or other transit behaviors. |
| 38 | HTTPClient http.Client |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 39 | } |
| 40 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 41 | // AuthenticatedHeaders returns a map of HTTP headers that are common for all |
| 42 | // authenticated service requests. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 43 | func (client *ProviderClient) AuthenticatedHeaders() map[string]string { |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 44 | if client.TokenID == "" { |
| 45 | return map[string]string{} |
| 46 | } |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 47 | return map[string]string{"X-Auth-Token": client.TokenID} |
| 48 | } |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 49 | |
| 50 | // RequestOpts customizes the behavior of the provider.Request() method. |
| 51 | type RequestOpts struct { |
| 52 | // JSONBody, if provided, will be encoded as JSON and used as the body of the HTTP request. The |
| 53 | // content type of the request will default to "application/json" unless overridden by MoreHeaders. |
| 54 | // It's an error to specify both a JSONBody and a RawBody. |
| 55 | JSONBody interface{} |
| 56 | // RawBody contains an io.Reader that will be consumed by the request directly. No content-type |
| 57 | // will be set unless one is provided explicitly by MoreHeaders. |
| 58 | RawBody io.Reader |
| 59 | |
| 60 | // JSONResponse, if provided, will be populated with the contents of the response body parsed as |
| 61 | // JSON. |
| 62 | JSONResponse *interface{} |
| 63 | // OkCodes contains a list of numeric HTTP status codes that should be interpreted as success. If |
| 64 | // the response has a different code, an error will be returned. |
| 65 | OkCodes []int |
| 66 | |
| 67 | // MoreHeaders specifies additional HTTP headers to be provide on the request. If a header is |
| 68 | // provided with a blank value (""), that header will be *omitted* instead: use this to suppress |
| 69 | // the default Accept header or an inferred Content-Type, for example. |
| 70 | MoreHeaders map[string]string |
| 71 | } |
| 72 | |
| 73 | // UnexpectedResponseCodeError is returned by the Request method when a response code other than |
| 74 | // those listed in OkCodes is encountered. |
| 75 | type UnexpectedResponseCodeError struct { |
| 76 | URL string |
| 77 | Method string |
| 78 | Expected []int |
| 79 | Actual int |
| 80 | Body []byte |
| 81 | } |
| 82 | |
| 83 | func (err *UnexpectedResponseCodeError) Error() string { |
| 84 | return fmt.Sprintf( |
| 85 | "Expected HTTP response code %v when accessing [%s %s], but got %d instead\n%s", |
| 86 | err.Expected, err.Method, err.URL, err.Actual, err.Body, |
| 87 | ) |
| 88 | } |
| 89 | |
| 90 | var applicationJSON = "application/json" |
| 91 | |
| 92 | // Request performs an HTTP request using the ProviderClient's current HTTPClient. An authentication |
| 93 | // header will automatically be provided. |
| 94 | func (client *ProviderClient) Request(method, url string, options RequestOpts) (*http.Response, error) { |
| 95 | var body io.Reader |
| 96 | var contentType *string |
| 97 | |
| 98 | // Derive the content body by either encoding an arbitrary object as JSON, or by taking a provided |
| 99 | // io.Reader as-is. Default the content-type to application/json. |
| 100 | |
| 101 | if options.JSONBody != nil { |
| 102 | if options.RawBody != nil { |
| 103 | panic("Please provide only one of JSONBody or RawBody to gophercloud.Request().") |
| 104 | } |
| 105 | |
| 106 | rendered, err := json.Marshal(options.JSONBody) |
| 107 | if err != nil { |
| 108 | return nil, err |
| 109 | } |
| 110 | |
| 111 | body = bytes.NewReader(rendered) |
| 112 | contentType = &applicationJSON |
| 113 | } |
| 114 | |
| 115 | if options.RawBody != nil { |
| 116 | body = options.RawBody |
| 117 | } |
| 118 | |
| 119 | // Construct the http.Request. |
| 120 | |
| 121 | req, err := http.NewRequest(method, url, body) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 | |
| 126 | // Populate the request headers. Apply options.MoreHeaders last, to give the caller the chance to |
| 127 | // modify or omit any header. |
| 128 | |
| 129 | if contentType != nil { |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame^] | 130 | req.Header.Set("Content-Type", *contentType) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 131 | } |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame^] | 132 | req.Header.Set("Accept", applicationJSON) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 133 | |
| 134 | for k, v := range client.AuthenticatedHeaders() { |
| 135 | req.Header.Add(k, v) |
| 136 | } |
| 137 | |
| 138 | if options.MoreHeaders != nil { |
| 139 | for k, v := range options.MoreHeaders { |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame^] | 140 | fmt.Printf("Applying header [%s: %v]\n", k, v) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 141 | if v != "" { |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame^] | 142 | req.Header.Set(k, v) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 143 | } else { |
| 144 | req.Header.Del(k) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // Issue the request. |
| 150 | |
| 151 | resp, err := client.HTTPClient.Do(req) |
| 152 | if err != nil { |
| 153 | return nil, err |
| 154 | } |
| 155 | |
| 156 | // Validate the response code, if requested to do so. |
| 157 | |
| 158 | if options.OkCodes != nil { |
| 159 | var ok bool |
| 160 | for _, code := range options.OkCodes { |
| 161 | if resp.StatusCode == code { |
| 162 | ok = true |
| 163 | break |
| 164 | } |
| 165 | } |
| 166 | if !ok { |
| 167 | body, _ := ioutil.ReadAll(resp.Body) |
| 168 | resp.Body.Close() |
| 169 | return resp, &UnexpectedResponseCodeError{ |
| 170 | URL: url, |
| 171 | Method: method, |
| 172 | Expected: options.OkCodes, |
| 173 | Actual: resp.StatusCode, |
| 174 | Body: body, |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Parse the response body as JSON, if requested to do so. |
| 180 | |
| 181 | if options.JSONResponse != nil { |
| 182 | defer resp.Body.Close() |
| 183 | json.NewDecoder(resp.Body).Decode(options.JSONResponse) |
| 184 | } |
| 185 | |
| 186 | return resp, nil |
| 187 | } |