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
Include the version of go-github
in User-Agent
headers sent to the GitHub API
#2403
Changes from 2 commits
7152610
3157af0
75465e1
8495ae5
f65d26e
c963102
7279ad7
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 |
---|---|---|
|
@@ -28,9 +28,11 @@ import ( | |
) | ||
|
||
const ( | ||
packageVersion = "45.2.0" | ||
|
||
defaultBaseURL = "https://api.github.com/" | ||
uploadBaseURL = "https://uploads.github.com/" | ||
userAgent = "go-github" | ||
userAgent = "go-github" + "/" + packageVersion | ||
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 do you think of renaming this to 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. Sure, that sounds good, @timrogers - let's also please sort these contant names within this group of 4 if you don't mind. |
||
|
||
headerRateLimit = "X-RateLimit-Limit" | ||
headerRateRemaining = "X-RateLimit-Remaining" | ||
|
@@ -167,6 +169,8 @@ type Client struct { | |
// User agent used when communicating with the GitHub API. | ||
UserAgent string | ||
|
||
PackageVersion string | ||
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. I thought that exposing this on the 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. Actually, you can kill two birds with one stone just by exporting the |
||
|
||
rateMu sync.Mutex | ||
rateLimits [categories]Rate // Rate limits for the client as determined by the most recent API calls. | ||
|
||
|
@@ -301,7 +305,7 @@ func NewClient(httpClient *http.Client) *Client { | |
baseURL, _ := url.Parse(defaultBaseURL) | ||
uploadURL, _ := url.Parse(uploadBaseURL) | ||
|
||
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL} | ||
c := &Client{client: httpClient, BaseURL: baseURL, PackageVersion: packageVersion, UserAgent: userAgent, UploadURL: uploadURL} | ||
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. So this would no longer be needed since the |
||
c.common.client = c | ||
c.Actions = (*ActionsService)(&c.common) | ||
c.Activity = (*ActivityService)(&c.common) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -507,10 +507,16 @@ func TestNewRequest(t *testing.T) { | |
t.Errorf("NewRequest(%q) Body is %v, want %v", inBody, got, want) | ||
} | ||
|
||
userAgent := req.Header.Get("User-Agent") | ||
|
||
// test that default user-agent is attached to the request | ||
if got, want := req.Header.Get("User-Agent"), c.UserAgent; got != want { | ||
if got, want := userAgent, c.UserAgent; got != want { | ||
t.Errorf("NewRequest() User-Agent is %v, want %v", got, want) | ||
} | ||
|
||
if !strings.Contains(userAgent, c.PackageVersion) { | ||
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. Is there somewhere else I should add a test to help confirm that this is being added to all requests? Or do you think this is sufficient? 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. I think this is sufficient, thanks. |
||
t.Errorf("NewRequest() User-Agent should contain %v, found %v", c.PackageVersion, userAgent) | ||
} | ||
} | ||
|
||
func TestNewRequest_invalidJSON(t *testing.T) { | ||
|
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.
I couldn't find this defined anywhere else in the package code, so I added it here. I'm happy to move it elsewhere, or access it in some other way if there are good options out there.