Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1] Fix options initialization in ServiceClient.Request (fixes #2798) #2800

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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