Skip to content
This repository has been archived by the owner on Aug 1, 2023. It is now read-only.

Commit

Permalink
factor out repeated RawBody/JSONBody logic
Browse files Browse the repository at this point in the history
  • Loading branch information
James Fisher authored and Samuel Ortiz committed Jun 21, 2016
1 parent b037586 commit d1ede46
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions provider_client.go
Expand Up @@ -102,6 +102,14 @@ type RequestOpts struct {
MoreHeaders map[string]string 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 // UnexpectedResponseCodeError is returned by the Request method when a response code other than
// those listed in OkCodes is encountered. // those listed in OkCodes is encountered.
type UnexpectedResponseCodeError struct { type UnexpectedResponseCodeError struct {
Expand Down Expand Up @@ -268,16 +276,12 @@ func (client *ProviderClient) Get(url string, JSONResponse *interface{}, opts *R
return client.Request("GET", url, *opts) 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 { if opts == nil {
opts = &RequestOpts{} opts = &RequestOpts{}
} }


if v, ok := (JSONBody).(io.ReadSeeker); ok { opts.setBody(body)
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}


if JSONResponse != nil { if JSONResponse != nil {
opts.JSONResponse = JSONResponse opts.JSONResponse = JSONResponse
Expand All @@ -286,16 +290,12 @@ func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONRespons
return client.Request("POST", url, *opts) 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 { if opts == nil {
opts = &RequestOpts{} opts = &RequestOpts{}
} }


if v, ok := (JSONBody).(io.ReadSeeker); ok { opts.setBody(body)
opts.RawBody = v
} else if JSONBody != nil {
opts.JSONBody = JSONBody
}


if JSONResponse != nil { if JSONResponse != nil {
opts.JSONResponse = JSONResponse opts.JSONResponse = JSONResponse
Expand Down

0 comments on commit d1ede46

Please sign in to comment.