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" |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 6 | "errors" |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 7 | "fmt" |
| 8 | "io" |
| 9 | "io/ioutil" |
| 10 | "net/http" |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 11 | "runtime" |
Jon Perritt | 2b5e3e1 | 2015-02-13 12:15:08 -0700 | [diff] [blame] | 12 | "strings" |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
Jon Perritt | 2b5e3e1 | 2015-02-13 12:15:08 -0700 | [diff] [blame] | 15 | // DefaultUserAgent is the default User-Agent string set in the request header. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 16 | const DefaultUserAgent = "gophercloud/2.0.0" |
Jon Perritt | 2b5e3e1 | 2015-02-13 12:15:08 -0700 | [diff] [blame] | 17 | |
| 18 | // UserAgent represents a User-Agent header. |
| 19 | type UserAgent struct { |
| 20 | // prepend is the slice of User-Agent strings to prepend to DefaultUserAgent. |
| 21 | // All the strings to prepend are accumulated and prepended in the Join method. |
| 22 | prepend []string |
| 23 | } |
| 24 | |
| 25 | // Prepend prepends a user-defined string to the default User-Agent string. Users |
| 26 | // may pass in one or more strings to prepend. |
| 27 | func (ua *UserAgent) Prepend(s ...string) { |
| 28 | ua.prepend = append(s, ua.prepend...) |
| 29 | } |
| 30 | |
| 31 | // Join concatenates all the user-defined User-Agend strings with the default |
| 32 | // Gophercloud User-Agent string. |
| 33 | func (ua *UserAgent) Join() string { |
| 34 | uaSlice := append(ua.prepend, DefaultUserAgent) |
| 35 | return strings.Join(uaSlice, " ") |
| 36 | } |
| 37 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 38 | // ProviderClient stores details that are required to interact with any |
| 39 | // services within a specific provider's API. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 40 | // |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 41 | // Generally, you acquire a ProviderClient by calling the NewClient method in |
| 42 | // the appropriate provider's child package, providing whatever authentication |
| 43 | // credentials are required. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 44 | type ProviderClient struct { |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 45 | // IdentityBase is the base URL used for a particular provider's identity |
| 46 | // service - it will be used when issuing authenticatation requests. It |
| 47 | // should point to the root resource of the identity service, not a specific |
| 48 | // identity version. |
Ash Wilson | 09694b9 | 2014-09-09 14:08:27 -0400 | [diff] [blame] | 49 | IdentityBase string |
| 50 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 51 | // IdentityEndpoint is the identity endpoint. This may be a specific version |
| 52 | // of the identity service. If this is the case, this endpoint is used rather |
| 53 | // than querying versions first. |
Ash Wilson | c6372fe | 2014-09-03 11:24:52 -0400 | [diff] [blame] | 54 | IdentityEndpoint string |
| 55 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 56 | // TokenID is the ID of the most recently issued valid token. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 57 | TokenID string |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 58 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 59 | // EndpointLocator describes how this provider discovers the endpoints for |
| 60 | // its constituent services. |
Ash Wilson | b8401a7 | 2014-09-08 17:07:49 -0400 | [diff] [blame] | 61 | EndpointLocator EndpointLocator |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 62 | |
| 63 | // HTTPClient allows users to interject arbitrary http, https, or other transit behaviors. |
| 64 | HTTPClient http.Client |
Jon Perritt | 2b5e3e1 | 2015-02-13 12:15:08 -0700 | [diff] [blame] | 65 | |
| 66 | // UserAgent represents the User-Agent header in the HTTP request. |
| 67 | UserAgent UserAgent |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 68 | |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 69 | // ReauthFunc is the function used to re-authenticate the user if the request |
| 70 | // fails with a 401 HTTP response code. This a needed because there may be multiple |
| 71 | // authentication functions for different Identity service versions. |
Jon Perritt | 6fe7c40 | 2015-02-17 12:24:53 -0700 | [diff] [blame] | 72 | ReauthFunc func() error |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Jamie Hannaford | b280dea | 2014-10-24 15:14:06 +0200 | [diff] [blame] | 75 | // AuthenticatedHeaders returns a map of HTTP headers that are common for all |
| 76 | // authenticated service requests. |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 77 | func (client *ProviderClient) AuthenticatedHeaders() map[string]string { |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 78 | if client.TokenID == "" { |
| 79 | return map[string]string{} |
| 80 | } |
Ash Wilson | 89466cc | 2014-08-29 11:27:39 -0400 | [diff] [blame] | 81 | return map[string]string{"X-Auth-Token": client.TokenID} |
| 82 | } |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 83 | |
| 84 | // RequestOpts customizes the behavior of the provider.Request() method. |
| 85 | type RequestOpts struct { |
| 86 | // JSONBody, if provided, will be encoded as JSON and used as the body of the HTTP request. The |
| 87 | // content type of the request will default to "application/json" unless overridden by MoreHeaders. |
| 88 | // It's an error to specify both a JSONBody and a RawBody. |
| 89 | JSONBody interface{} |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 90 | // RawBody contains an io.Reader that will be consumed by the request directly. No content-type |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 91 | // will be set unless one is provided explicitly by MoreHeaders. |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 92 | RawBody io.Reader |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 93 | // JSONResponse, if provided, will be populated with the contents of the response body parsed as |
| 94 | // JSON. |
Ash Wilson | 2491b4c | 2015-02-12 16:13:39 -0500 | [diff] [blame] | 95 | JSONResponse interface{} |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 96 | // OkCodes contains a list of numeric HTTP status codes that should be interpreted as success. If |
| 97 | // the response has a different code, an error will be returned. |
| 98 | OkCodes []int |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 99 | // MoreHeaders specifies additional HTTP headers to be provide on the request. If a header is |
| 100 | // provided with a blank value (""), that header will be *omitted* instead: use this to suppress |
| 101 | // the default Accept header or an inferred Content-Type, for example. |
| 102 | MoreHeaders map[string]string |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 103 | // ErrorType specifies the resource error type to return if an error is encountered. |
| 104 | // This lets resources override default error messages based on the response status code. |
| 105 | ErrorContext error |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 106 | } |
| 107 | |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 108 | var applicationJSON = "application/json" |
| 109 | |
| 110 | // Request performs an HTTP request using the ProviderClient's current HTTPClient. An authentication |
| 111 | // header will automatically be provided. |
| 112 | func (client *ProviderClient) Request(method, url string, options RequestOpts) (*http.Response, error) { |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 113 | var body io.Reader |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 114 | var contentType *string |
| 115 | |
| 116 | // Derive the content body by either encoding an arbitrary object as JSON, or by taking a provided |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 117 | // io.ReadSeeker as-is. Default the content-type to application/json. |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 118 | if options.JSONBody != nil { |
| 119 | if options.RawBody != nil { |
| 120 | panic("Please provide only one of JSONBody or RawBody to gophercloud.Request().") |
| 121 | } |
| 122 | |
| 123 | rendered, err := json.Marshal(options.JSONBody) |
| 124 | if err != nil { |
| 125 | return nil, err |
| 126 | } |
| 127 | |
| 128 | body = bytes.NewReader(rendered) |
| 129 | contentType = &applicationJSON |
| 130 | } |
| 131 | |
| 132 | if options.RawBody != nil { |
| 133 | body = options.RawBody |
| 134 | } |
| 135 | |
| 136 | // Construct the http.Request. |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 137 | req, err := http.NewRequest(method, url, body) |
| 138 | if err != nil { |
| 139 | return nil, err |
| 140 | } |
| 141 | |
| 142 | // Populate the request headers. Apply options.MoreHeaders last, to give the caller the chance to |
| 143 | // modify or omit any header. |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 144 | if contentType != nil { |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame] | 145 | req.Header.Set("Content-Type", *contentType) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 146 | } |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame] | 147 | req.Header.Set("Accept", applicationJSON) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 148 | |
| 149 | for k, v := range client.AuthenticatedHeaders() { |
| 150 | req.Header.Add(k, v) |
| 151 | } |
| 152 | |
Jon Perritt | f0a1fee | 2015-02-13 12:53:23 -0700 | [diff] [blame] | 153 | // Set the User-Agent header |
| 154 | req.Header.Set("User-Agent", client.UserAgent.Join()) |
| 155 | |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 156 | if options.MoreHeaders != nil { |
| 157 | for k, v := range options.MoreHeaders { |
| 158 | if v != "" { |
Ash Wilson | 54d62fa | 2015-02-12 15:09:46 -0500 | [diff] [blame] | 159 | req.Header.Set(k, v) |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 160 | } else { |
| 161 | req.Header.Del(k) |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Kostiantyn Yarovyi | 3fa30bb | 2015-11-25 17:21:03 +0200 | [diff] [blame] | 166 | // Set connection parameter to close the connection immediately when we've got the response |
| 167 | req.Close = true |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 168 | |
Jon Perritt | 2b5e3e1 | 2015-02-13 12:15:08 -0700 | [diff] [blame] | 169 | // Issue the request. |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 170 | resp, err := client.HTTPClient.Do(req) |
| 171 | if err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | |
Jon Perritt | 6fe7c40 | 2015-02-17 12:24:53 -0700 | [diff] [blame] | 175 | if resp.StatusCode == http.StatusUnauthorized { |
| 176 | if client.ReauthFunc != nil { |
| 177 | err = client.ReauthFunc() |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 178 | if err != nil { |
| 179 | return nil, fmt.Errorf("Error trying to re-authenticate: %s", err) |
| 180 | } |
jrperritt | b101323 | 2016-02-10 19:01:53 -0600 | [diff] [blame] | 181 | if seeker, ok := options.RawBody.(io.ReadSeeker); ok && options.RawBody != nil { |
| 182 | seeker.Seek(0, 0) |
Jon Perritt | fcedd7b | 2015-06-15 19:41:01 -0600 | [diff] [blame] | 183 | } |
Fredi Pevcin | a979be9 | 2015-10-20 09:13:29 +0200 | [diff] [blame] | 184 | resp.Body.Close() |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 185 | resp, err = client.Request(method, url, options) |
| 186 | if err != nil { |
| 187 | return nil, fmt.Errorf("Successfully re-authenticated, but got error executing request: %s", err) |
| 188 | } |
Clinton Kitson | fcd283a | 2016-01-07 09:00:56 -0800 | [diff] [blame] | 189 | |
| 190 | return resp, nil |
Jon Perritt | f4052c6 | 2015-02-14 09:48:18 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
Jamie Hannaford | 647cea5 | 2015-03-23 17:15:07 +0100 | [diff] [blame] | 194 | // Allow default OkCodes if none explicitly set |
| 195 | if options.OkCodes == nil { |
| 196 | options.OkCodes = defaultOkCodes(method) |
| 197 | } |
| 198 | |
| 199 | // Validate the HTTP response status. |
| 200 | var ok bool |
| 201 | for _, code := range options.OkCodes { |
| 202 | if resp.StatusCode == code { |
| 203 | ok = true |
| 204 | break |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 205 | } |
Jamie Hannaford | 647cea5 | 2015-03-23 17:15:07 +0100 | [diff] [blame] | 206 | } |
| 207 | if !ok { |
| 208 | body, _ := ioutil.ReadAll(resp.Body) |
| 209 | resp.Body.Close() |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 210 | pc := make([]uintptr, 1) // at least 1 entry needed |
| 211 | runtime.Callers(2, pc) |
| 212 | f := runtime.FuncForPC(pc[0]) |
| 213 | respErr := &ErrUnexpectedResponseCode{ |
| 214 | BaseError: &BaseError{ |
| 215 | Function: f.Name(), |
| 216 | }, |
Jamie Hannaford | 647cea5 | 2015-03-23 17:15:07 +0100 | [diff] [blame] | 217 | URL: url, |
| 218 | Method: method, |
| 219 | Expected: options.OkCodes, |
| 220 | Actual: resp.StatusCode, |
| 221 | Body: body, |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 222 | } |
Jon Perritt | e0f9e4f | 2016-02-21 21:41:03 -0600 | [diff] [blame^] | 223 | |
| 224 | errType := options.ErrorContext |
| 225 | switch resp.StatusCode { |
| 226 | case http.StatusBadRequest: |
| 227 | err = ErrDefault400{respErr} |
| 228 | if error400er, ok := errType.(Err400er); ok { |
| 229 | err = error400er.Error400(respErr) |
| 230 | } |
| 231 | case http.StatusUnauthorized: |
| 232 | if client.ReauthFunc != nil { |
| 233 | err = client.ReauthFunc() |
| 234 | if err != nil { |
| 235 | return nil, &ErrUnableToReauthenticate{ |
| 236 | &BaseError{ |
| 237 | OriginalError: respErr, |
| 238 | }, |
| 239 | } |
| 240 | } |
| 241 | if options.RawBody != nil { |
| 242 | seeker, ok := options.RawBody.(io.Seeker) |
| 243 | if !ok { |
| 244 | return nil, &ErrErrorAfterReauthentication{ |
| 245 | &BaseError{ |
| 246 | OriginalError: errors.New("Couldn't seek to beginning of content."), |
| 247 | }, |
| 248 | } |
| 249 | } |
| 250 | seeker.Seek(0, 0) |
| 251 | } |
| 252 | resp, err = client.Request(method, url, options) |
| 253 | if err != nil { |
| 254 | switch err.(type) { |
| 255 | case *ErrUnexpectedResponseCode: |
| 256 | return nil, &ErrErrorAfterReauthentication{&BaseError{OriginalError: err.(*ErrUnexpectedResponseCode)}} |
| 257 | default: |
| 258 | return nil, &ErrErrorAfterReauthentication{ |
| 259 | &BaseError{ |
| 260 | OriginalError: err, |
| 261 | }, |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | return resp, nil |
| 266 | } |
| 267 | err = ErrDefault401{respErr} |
| 268 | if error401er, ok := errType.(Err401er); ok { |
| 269 | err = error401er.Error401(respErr) |
| 270 | } |
| 271 | case http.StatusNotFound: |
| 272 | err = ErrDefault404{respErr} |
| 273 | if error404er, ok := errType.(Err404er); ok { |
| 274 | err = error404er.Error404(respErr) |
| 275 | } |
| 276 | case http.StatusMethodNotAllowed: |
| 277 | err = ErrDefault405{respErr} |
| 278 | if error405er, ok := errType.(Err405er); ok { |
| 279 | err = error405er.Error405(respErr) |
| 280 | } |
| 281 | case http.StatusRequestTimeout: |
| 282 | err = ErrDefault408{respErr} |
| 283 | if error408er, ok := errType.(Err408er); ok { |
| 284 | err = error408er.Error408(respErr) |
| 285 | } |
| 286 | case 429: |
| 287 | err = ErrDefault429{respErr} |
| 288 | if error429er, ok := errType.(Err429er); ok { |
| 289 | err = error429er.Error429(respErr) |
| 290 | } |
| 291 | case http.StatusInternalServerError: |
| 292 | err = ErrDefault500{respErr} |
| 293 | if error500er, ok := errType.(Err500er); ok { |
| 294 | err = error500er.Error500(respErr) |
| 295 | } |
| 296 | case http.StatusServiceUnavailable: |
| 297 | err = ErrDefault503{respErr} |
| 298 | if error503er, ok := errType.(Err503er); ok { |
| 299 | err = error503er.Error503(respErr) |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | if err == nil { |
| 304 | err = respErr |
| 305 | } |
| 306 | |
| 307 | return resp, err |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | // Parse the response body as JSON, if requested to do so. |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 311 | if options.JSONResponse != nil { |
| 312 | defer resp.Body.Close() |
Pratik Mallya | ee675fd | 2015-09-14 14:07:30 -0500 | [diff] [blame] | 313 | if err := json.NewDecoder(resp.Body).Decode(options.JSONResponse); err != nil { |
| 314 | return nil, err |
| 315 | } |
Ash Wilson | 89eec33 | 2015-02-12 13:40:32 -0500 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return resp, nil |
| 319 | } |
Jamie Hannaford | 647cea5 | 2015-03-23 17:15:07 +0100 | [diff] [blame] | 320 | |
| 321 | func defaultOkCodes(method string) []int { |
| 322 | switch { |
| 323 | case method == "GET": |
| 324 | return []int{200} |
| 325 | case method == "POST": |
| 326 | return []int{201, 202} |
| 327 | case method == "PUT": |
| 328 | return []int{201, 202} |
Krzysztof Kwapisiewicz | 136d2c2 | 2016-02-03 15:36:06 +0100 | [diff] [blame] | 329 | case method == "PATCH": |
| 330 | return []int{200, 204} |
Jamie Hannaford | 647cea5 | 2015-03-23 17:15:07 +0100 | [diff] [blame] | 331 | case method == "DELETE": |
| 332 | return []int{202, 204} |
| 333 | } |
| 334 | |
| 335 | return []int{} |
| 336 | } |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 337 | |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 338 | // Get calls `Request` with the "GET" HTTP verb. |
| 339 | func (client *ProviderClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 340 | if opts == nil { |
| 341 | opts = &RequestOpts{} |
| 342 | } |
| 343 | if JSONResponse != nil { |
| 344 | opts.JSONResponse = JSONResponse |
| 345 | } |
| 346 | return client.Request("GET", url, *opts) |
| 347 | } |
| 348 | |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 349 | // Post calls `Request` with the "POST" HTTP verb. |
| 350 | func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 351 | if opts == nil { |
| 352 | opts = &RequestOpts{} |
| 353 | } |
| 354 | |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 355 | if v, ok := (JSONBody).(io.ReadSeeker); ok { |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 356 | opts.RawBody = v |
| 357 | } else if JSONBody != nil { |
| 358 | opts.JSONBody = JSONBody |
| 359 | } |
| 360 | |
| 361 | if JSONResponse != nil { |
| 362 | opts.JSONResponse = JSONResponse |
| 363 | } |
| 364 | |
| 365 | return client.Request("POST", url, *opts) |
| 366 | } |
| 367 | |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 368 | // Put calls `Request` with the "PUT" HTTP verb. |
| 369 | func (client *ProviderClient) Put(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 370 | if opts == nil { |
| 371 | opts = &RequestOpts{} |
| 372 | } |
| 373 | |
Brendan ODonnell | a69b347 | 2015-04-27 13:59:41 -0500 | [diff] [blame] | 374 | if v, ok := (JSONBody).(io.ReadSeeker); ok { |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 375 | opts.RawBody = v |
| 376 | } else if JSONBody != nil { |
| 377 | opts.JSONBody = JSONBody |
| 378 | } |
| 379 | |
| 380 | if JSONResponse != nil { |
| 381 | opts.JSONResponse = JSONResponse |
| 382 | } |
| 383 | |
| 384 | return client.Request("PUT", url, *opts) |
| 385 | } |
| 386 | |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 387 | // Patch calls `Request` with the "PATCH" HTTP verb. |
| 388 | func (client *ProviderClient) Patch(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) { |
Krzysztof Kwapisiewicz | 136d2c2 | 2016-02-03 15:36:06 +0100 | [diff] [blame] | 389 | if opts == nil { |
| 390 | opts = &RequestOpts{} |
| 391 | } |
| 392 | |
| 393 | if v, ok := (JSONBody).(io.ReadSeeker); ok { |
| 394 | opts.RawBody = v |
| 395 | } else if JSONBody != nil { |
| 396 | opts.JSONBody = JSONBody |
| 397 | } |
| 398 | |
| 399 | if JSONResponse != nil { |
| 400 | opts.JSONResponse = JSONResponse |
| 401 | } |
| 402 | |
| 403 | return client.Request("PATCH", url, *opts) |
| 404 | } |
| 405 | |
Jon Perritt | aaafa61 | 2016-02-21 18:23:38 -0600 | [diff] [blame] | 406 | // Delete calls `Request` with the "DELETE" HTTP verb. |
Jamie Hannaford | 2a9e74f | 2015-03-24 14:55:24 +0100 | [diff] [blame] | 407 | func (client *ProviderClient) Delete(url string, opts *RequestOpts) (*http.Response, error) { |
| 408 | if opts == nil { |
| 409 | opts = &RequestOpts{} |
| 410 | } |
| 411 | |
| 412 | return client.Request("DELETE", url, *opts) |
| 413 | } |