Skip to content

Commit

Permalink
Merge pull request #2315 from freedywu/master
Browse files Browse the repository at this point in the history
Identity v2&v3: add RequestOpts.OmitHeaders, remove blank header
  • Loading branch information
EmilienM authored May 30, 2022
2 parents 2c22255 + 613261b commit c0c8d2a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion openstack/identity/v2/tokens/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r Creat
}
resp, err := client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 203},
MoreHeaders: map[string]string{"X-Auth-Token": ""},
OmitHeaders: []string{"X-Auth-Token"},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
Expand Down
2 changes: 1 addition & 1 deletion openstack/identity/v3/tokens/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func Create(c *gophercloud.ServiceClient, opts AuthOptionsBuilder) (r CreateResu
}

resp, err := c.Post(tokenURL(c), b, &r.Body, &gophercloud.RequestOpts{
MoreHeaders: map[string]string{"X-Auth-Token": ""},
OmitHeaders: []string{"X-Auth-Token"},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
Expand Down
15 changes: 11 additions & 4 deletions provider_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,12 @@ type RequestOpts struct {
// OkCodes contains a list of numeric HTTP status codes that should be interpreted as success. If
// the response has a different code, an error will be returned.
OkCodes []int
// MoreHeaders specifies additional HTTP headers to be provide on the request. If a header is
// provided with a blank value (""), that header will be *omitted* instead: use this to suppress
// the default Accept header or an inferred Content-Type, for example.
// MoreHeaders specifies additional HTTP headers to be provided on the request.
// MoreHeaders will be overridden by OmitHeaders
MoreHeaders map[string]string
// OmitHeaders specifies the HTTP headers which should be omitted.
// OmitHeaders will override MoreHeaders
OmitHeaders []string
// ErrorContext specifies the resource error type to return if an error is encountered.
// This lets resources override default error messages based on the response status code.
ErrorContext error
Expand Down Expand Up @@ -396,7 +398,8 @@ func (client *ProviderClient) doRequest(method, url string, options *RequestOpts
req = req.WithContext(client.Context)
}

// Populate the request headers. Apply options.MoreHeaders last, to give the caller the chance to
// Populate the request headers.
// Apply options.MoreHeaders and options.OmitHeaders, to give the caller the chance to
// modify or omit any header.
if contentType != nil {
req.Header.Set("Content-Type", *contentType)
Expand All @@ -412,6 +415,10 @@ func (client *ProviderClient) doRequest(method, url string, options *RequestOpts
}
}

for _, v := range options.OmitHeaders {
req.Header.Del(v)
}

// get latest token from client
for k, v := range client.AuthenticatedHeaders() {
req.Header.Set(k, v)
Expand Down

0 comments on commit c0c8d2a

Please sign in to comment.