-
Notifications
You must be signed in to change notification settings - Fork 93
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
Robust Header Check #13
Changes from 8 commits
844754d
2ee8446
8a5d177
7c7f7b3
52eae4e
6531cce
a58e813
9e23b94
677e3d3
b18aaba
3169c9a
e9f06a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
@@ -90,7 +91,7 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) { | |
} | ||
|
||
req.Header.Set("Authorization", "token "+token) | ||
req.Header.Add("Accept", acceptHeader) // We add to "Accept" header to avoid overwriting existing req headers. | ||
addAcceptHeader(req.Header) | ||
resp, err := t.tr.RoundTrip(req) | ||
return resp, err | ||
} | ||
|
@@ -134,3 +135,19 @@ func (t *Transport) refreshToken() error { | |
|
||
return nil | ||
} | ||
|
||
func addAcceptHeader(headers http.Header) { | ||
if headers.Get("Accept") == "" { | ||
headers.Set("Accept", acceptHeader) | ||
return | ||
} | ||
|
||
//Need to loop through all Accept headers incase there is more than one. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor style issue. Since this is a normal comment for humans rather than for the compiler, it should have a space after
See https://dmitri.shuralyov.com/idiomatic-go#comments-for-humans-always-have-a-single-space-after-the-slashes for rationale. Also, “incase” should be two words I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cpheps Did you see this comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missed the second one, just pushed a fix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about the first part? I still see some comments that don't have a space after |
||
for _, header := range headers["Accept"] { | ||
//Looks as though all media types (https://developer.github.com/v3/media/) that can accept json end with "json". Only doing a suffix check to see if a json header already exists. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here as well:
You might also want to wrap the line at around 80-100 columns, so it’s more comfortable to read. |
||
if strings.HasSuffix(header, "json") { | ||
headers.Add("Accept", acceptHeader) // We add to "Accept" header to avoid overwriting existing req headers. | ||
return | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to document what the function does. Perhaps something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copied it directly (seemed like a good description) and pushed.