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