Skip to content

Commit

Permalink
Merge pull request #2799 from dtantsur/nil-map
Browse files Browse the repository at this point in the history
Fix options initialization in ServiceClient.Request (fixes #2798)
  • Loading branch information
pierreprinetti committed Oct 6, 2023
2 parents f276bd9 + 0f4a9ff commit 4e19ac8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (client *ServiceClient) ServiceURL(parts ...string) string {
return client.ResourceBaseURL() + strings.Join(parts, "/")
}

func (client *ServiceClient) initReqOpts(url string, JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) {
func (client *ServiceClient) initReqOpts(JSONBody interface{}, JSONResponse interface{}, opts *RequestOpts) {
if v, ok := (JSONBody).(io.Reader); ok {
opts.RawBody = v
} else if JSONBody != nil {
Expand All @@ -57,22 +57,14 @@ func (client *ServiceClient) initReqOpts(url string, JSONBody interface{}, JSONR
if JSONResponse != nil {
opts.JSONResponse = JSONResponse
}

if opts.MoreHeaders == nil {
opts.MoreHeaders = make(map[string]string)
}

if client.Microversion != "" {
client.setMicroversionHeader(opts)
}
}

// Get calls `Request` with the "GET" HTTP verb.
func (client *ServiceClient) Get(url string, JSONResponse interface{}, opts *RequestOpts) (*http.Response, error) {
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, nil, JSONResponse, opts)
client.initReqOpts(nil, JSONResponse, opts)
return client.Request("GET", url, opts)
}

Expand All @@ -81,7 +73,7 @@ func (client *ServiceClient) Post(url string, JSONBody interface{}, JSONResponse
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, JSONBody, JSONResponse, opts)
client.initReqOpts(JSONBody, JSONResponse, opts)
return client.Request("POST", url, opts)
}

Expand All @@ -90,7 +82,7 @@ func (client *ServiceClient) Put(url string, JSONBody interface{}, JSONResponse
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, JSONBody, JSONResponse, opts)
client.initReqOpts(JSONBody, JSONResponse, opts)
return client.Request("PUT", url, opts)
}

Expand All @@ -99,7 +91,7 @@ func (client *ServiceClient) Patch(url string, JSONBody interface{}, JSONRespons
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, JSONBody, JSONResponse, opts)
client.initReqOpts(JSONBody, JSONResponse, opts)
return client.Request("PATCH", url, opts)
}

Expand All @@ -108,7 +100,7 @@ func (client *ServiceClient) Delete(url string, opts *RequestOpts) (*http.Respon
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, nil, nil, opts)
client.initReqOpts(nil, nil, opts)
return client.Request("DELETE", url, opts)
}

Expand All @@ -117,7 +109,7 @@ func (client *ServiceClient) Head(url string, opts *RequestOpts) (*http.Response
if opts == nil {
opts = new(RequestOpts)
}
client.initReqOpts(url, nil, nil, opts)
client.initReqOpts(nil, nil, opts)
return client.Request("HEAD", url, opts)
}

Expand All @@ -142,10 +134,19 @@ func (client *ServiceClient) setMicroversionHeader(opts *RequestOpts) {

// Request carries out the HTTP operation for the service client
func (client *ServiceClient) Request(method, url string, options *RequestOpts) (*http.Response, error) {
if options.MoreHeaders == nil {
options.MoreHeaders = make(map[string]string)
}

if client.Microversion != "" {
client.setMicroversionHeader(options)
}

if len(client.MoreHeaders) > 0 {
if options == nil {
options = new(RequestOpts)
}

for k, v := range client.MoreHeaders {
options.MoreHeaders[k] = v
}
Expand Down

0 comments on commit 4e19ac8

Please sign in to comment.