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

Adding option in client to specify extra headers #276

Merged
merged 1 commit into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type Client struct {
//The Http Client that is used to make requests
HttpClient *http.Client
RetryTimeout time.Duration

//Option to specify extra headers like User-Agent
ExtraHeader map[string]string
}

// valid is the struct to unmarshal validation endpoint responses into.
Expand Down
7 changes: 7 additions & 0 deletions integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,10 @@ func TestBaseUrl(t *testing.T) {
assert.Equal(t, "https://another.datadoghq.com", c.GetBaseUrl())
})
}

func TestExtraHeader(t *testing.T) {
t.Run("No Extra Header for backward compatibility", func(t *testing.T) {
c := datadog.NewClient("foo", "bar")
assert.Empty(t, c.ExtraHeader)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we also add a test that ensures an extra header was set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's what I wanted to do originally but it would require some refactoring of the code as there are no interfaces, and all the methods I am interested in to test are private in the datadog package.

Do you think it's worth a refactoring ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we're just currently testing that the client now has a new attribute 😄 But if its refactoring the whole suite, perhaps not currently worthwhile.

})
}
4 changes: 4 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,5 +209,9 @@ func (client *Client) createRequest(method, api string, reqbody interface{}) (*h
if bodyReader != nil {
req.Header.Add("Content-Type", "application/json")
}
for k, v := range client.ExtraHeader {
req.Header.Add(k, v)
Copy link
Collaborator

Choose a reason for hiding this comment

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

do we need to replace if a user specifies a header that is already set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like they get appended using curl, I would follow this implementation in case for whatever reason the same key can be used multiple times.

➜  ~ curl -H "Content-Type:application/json"  -H "Content-Type:test" -vvv localhost
* Rebuilt URL to: localhost/
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 80 (#0)
> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.54.0
> Accept: */*
> Content-Type:application/json
> Content-Type:test

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh good point 😄 Is the order important here? Should/does extra headers always come at the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the order is not important because it's sent in the request, and the server can process it however it wants.

}

return req, nil
}