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

Conversation

CharlyF
Copy link
Contributor

@CharlyF CharlyF commented Oct 10, 2019

👋 In order to better identify Agents making calls to the API we need to be able to configure the request's context.
Having the extra options at the client level will also allow for other header options to be configured.

@@ -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.

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.

@nmuesch nmuesch merged commit 39d6fc4 into zorkian:master Oct 10, 2019
@CharlyF CharlyF deleted the add-header-options branch October 10, 2019 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants