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

Commit d1ede46

Browse files
James FisherSamuel Ortiz
authored andcommitted
factor out repeated RawBody/JSONBody logic
1 parent b037586 commit d1ede46

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

provider_client.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ type RequestOpts struct {
102102
MoreHeaders map[string]string
103103
}
104104

105+
func (opts *RequestOpts) setBody(body interface{}) {
106+
if v, ok := (body).(io.ReadSeeker); ok {
107+
opts.RawBody = v
108+
} else if body != nil {
109+
opts.JSONBody = body
110+
}
111+
}
112+
105113
// UnexpectedResponseCodeError is returned by the Request method when a response code other than
106114
// those listed in OkCodes is encountered.
107115
type UnexpectedResponseCodeError struct {
@@ -268,16 +276,12 @@ func (client *ProviderClient) Get(url string, JSONResponse *interface{}, opts *R
268276
return client.Request("GET", url, *opts)
269277
}
270278

271-
func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
279+
func (client *ProviderClient) Post(url string, body interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
272280
if opts == nil {
273281
opts = &RequestOpts{}
274282
}
275283

276-
if v, ok := (JSONBody).(io.ReadSeeker); ok {
277-
opts.RawBody = v
278-
} else if JSONBody != nil {
279-
opts.JSONBody = JSONBody
280-
}
284+
opts.setBody(body)
281285

282286
if JSONResponse != nil {
283287
opts.JSONResponse = JSONResponse
@@ -286,16 +290,12 @@ func (client *ProviderClient) Post(url string, JSONBody interface{}, JSONRespons
286290
return client.Request("POST", url, *opts)
287291
}
288292

289-
func (client *ProviderClient) Put(url string, JSONBody interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
293+
func (client *ProviderClient) Put(url string, body interface{}, JSONResponse *interface{}, opts *RequestOpts) (*http.Response, error) {
290294
if opts == nil {
291295
opts = &RequestOpts{}
292296
}
293297

294-
if v, ok := (JSONBody).(io.ReadSeeker); ok {
295-
opts.RawBody = v
296-
} else if JSONBody != nil {
297-
opts.JSONBody = JSONBody
298-
}
298+
opts.setBody(body)
299299

300300
if JSONResponse != nil {
301301
opts.JSONResponse = JSONResponse

0 commit comments

Comments
 (0)