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

Add HTTP headers to the API client #3394

Merged
merged 2 commits into from
Oct 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ type Client struct {
addr *url.URL
config *Config
token string
headers http.Header
wrappingLookupFunc WrappingLookupFunc
}

Expand Down Expand Up @@ -350,6 +351,11 @@ func (c *Client) ClearToken() {
c.token = ""
}

// SetHeaders sets the headers to be used for future requests.
func (c *Client) SetHeaders(headers http.Header) {
c.headers = headers
}

// Clone creates a copy of this client.
func (c *Client) Clone() (*Client, error) {
return NewClient(c.config)
Expand Down Expand Up @@ -399,6 +405,9 @@ func (c *Client) NewRequest(method, requestPath string) *Request {
if c.config.Timeout != 0 {
c.config.HttpClient.Timeout = c.config.Timeout
}
if c.headers != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this actually needs to iterate through and add headers, not replace the whole header structure unilaterally.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this works now because the ToHTTP method that is called from RawRequest already does that if any headers in the passed in request is set: request.go. Before these changes no headers were set before ToHTTP was called.

It might be better to do as you suggested though. Let me know what you think and I'll update accordingly!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, no -- that looks fine. Thanks for clarifying!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! No problem, thanks for merging this!

req.Headers = c.headers
}

return req
}
Expand Down