Permalink
Browse files

factor out repeated RawBody/JSONBody logic

  • Loading branch information...
1 parent b037586 commit d1ede46af644ef0fa2871dfb0ac7db64b149949c James Fisher committed with sameo Jun 8, 2015
Showing with 12 additions and 12 deletions.
  1. +12 −12 provider_client.go
View
@@ -102,6 +102,14 @@ type RequestOpts struct {
MoreHeaders map[string]string
}
+func (opts *RequestOpts) setBody(body interface{}) {
+ if v, ok := (body).(io.ReadSeeker); ok {
+ opts.RawBody = v
+ } else if body != nil {
+ opts.JSONBody = body
+ }
+}
+
// UnexpectedResponseCodeError is returned by the Request method when a response code other than
// those listed in OkCodes is encountered.
type UnexpectedResponseCodeError struct {
@@ -268,16 +276,12 @@ func (client *ProviderClient) Get(url string, JSONResponse *interface{}, opts *R
return client.Request("GET", url, *opts)
}
-func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
+func (client *ProviderClient) Post(url string, body interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}
- if v, ok := (JSONBody).(io.ReadSeeker); ok {
- opts.RawBody = v
- } else if JSONBody != nil {
- opts.JSONBody = JSONBody
- }
+ opts.setBody(body)
if JSONResponse != nil {
opts.JSONResponse = JSONResponse
@@ -286,16 +290,12 @@ func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONRespons
return client.Request("POST", url, *opts)
}
-func (client *ProviderClient) Put(url string, JSONBody interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
+func (client *ProviderClient) Put(url string, body interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = &RequestOpts{}
}
- if v, ok := (JSONBody).(io.ReadSeeker); ok {
- opts.RawBody = v
- } else if JSONBody != nil {
- opts.JSONBody = JSONBody
- }
+ opts.setBody(body)
if JSONResponse != nil {
opts.JSONResponse = JSONResponse

0 comments on commit d1ede46

Please sign in to comment.