-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat!: Refactor client constructor to use options pattern #4201
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
base: master
Are you sure you want to change the base?
Changes from all commits
7e6779b
4ee0835
b6c4519
de722e1
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 |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| ignore: | ||
| # ignore examples | ||
| - "example" | ||
| # ignore auto-generated code | ||
| - "github/github-accessors.go" | ||
| # ignore experimental scrape package | ||
| - "scrape" | ||
| # ignore test | ||
| - "test" | ||
| # ignore tools | ||
| - "tools" |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,7 +20,7 @@ go-github will require the N-1 major release of Go by default. | |||||
|
|
||||||
| [support-policy]: https://golang.org/doc/devel/release.html#policy | ||||||
|
|
||||||
| ## Development | ||||||
| ## Development ## | ||||||
|
|
||||||
| If you're interested in using the [GraphQL API v4][], the recommended library is | ||||||
| [shurcooL/githubv4][]. | ||||||
|
|
@@ -66,7 +66,10 @@ Construct a new GitHub client, then use the various services on the client to | |||||
| access different parts of the GitHub API. For example: | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil) | ||||||
| client, err := github.NewClient() | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
|
|
||||||
| // list all organizations for user "willnorris" | ||||||
| orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil) | ||||||
|
|
@@ -75,7 +78,10 @@ orgs, _, err := client.Organizations.List(context.Background(), "willnorris", ni | |||||
| Some API methods have optional parameters that can be passed. For example: | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil) | ||||||
| client, err := github.NewClient() | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
|
|
||||||
| // list public repositories for org "github" | ||||||
| opt := &github.RepositoryListByOrgOptions{Type: "public"} | ||||||
|
|
@@ -95,12 +101,15 @@ For more sample code snippets, head over to the | |||||
|
|
||||||
| ### Authentication ### | ||||||
|
|
||||||
| Use the `WithAuthToken` method to configure your client to authenticate using an | ||||||
| Use the `github.WithAuthToken` options method to configure your client to authenticate using an | ||||||
| OAuth token (for example, a [personal access token][]). This is what is needed | ||||||
| for a majority of use cases aside from GitHub Apps. | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil).WithAuthToken("... your access token ...") | ||||||
| client, err := github.NewClient(github.WithAuthToken("... your access token ...")) | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Note that when using an authenticated Client, all calls made by the client will | ||||||
|
|
@@ -146,7 +155,10 @@ func main() { | |||||
| } | ||||||
|
|
||||||
| // Use installation transport with client. | ||||||
| client := github.NewClient(&http.Client{Transport: itr}) | ||||||
| client, err := github.NewClient(github.WithTransport(itr)) | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
|
|
||||||
| // Use client... | ||||||
| } | ||||||
|
|
@@ -186,11 +198,14 @@ func main() { | |||||
| // InstallationTokenSource has the mechanism to refresh the token when it expires. | ||||||
| httpClient := oauth2.NewClient(context.Background(), installationTokenSource) | ||||||
|
|
||||||
| client := github.NewClient(httpClient) | ||||||
| client, err := github.NewClient(github.WithHTTPClient(httpClient)) | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| *Note*: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token | ||||||
| _Note_: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token | ||||||
|
Contributor
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. Unrelated change:
Suggested change
Contributor
Author
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've modified the README and these lines weren't consistently formatted. @gmlewis has asked me to fixup files where I've made changes, so I suggest we keep this for the same reason. |
||||||
| using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples. | ||||||
|
|
||||||
| ### Rate Limiting ### | ||||||
|
|
@@ -296,9 +311,10 @@ import ( | |||||
| _ "github.com/bartventer/httpcache/store/memcache" // Register the in-memory backend | ||||||
| ) | ||||||
|
|
||||||
| client := github.NewClient( | ||||||
| httpcache.NewClient("memcache://"), | ||||||
| ).WithAuthToken(os.Getenv("GITHUB_TOKEN")) | ||||||
| client, err := github.NewClient(github.WithHTTPClient(httpcache.NewClient("memcache://")), github.WithAuthToken(os.Getenv("GITHUB_TOKEN"))) | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Alternatively, the [bored-engineer/github-conditional-http-transport](https://github.com/bored-engineer/github-conditional-http-transport) | ||||||
|
|
@@ -334,7 +350,10 @@ embedded type of a more specific list options struct (for example | |||||
| `github.Response` struct. | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil) | ||||||
| client, err := github.NewClient() | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
|
|
||||||
| opt := &github.RepositoryListByOrgOptions{ | ||||||
| ListOptions: github.ListOptions{PerPage: 10}, | ||||||
|
|
@@ -372,7 +391,10 @@ To handle rate limiting issues, make sure to use a rate-limiting transport. | |||||
| To use these methods, simply create an iterator and then range over it, for example: | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil) | ||||||
| client, err := github.NewClient() | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
| var allRepos []*github.Repository | ||||||
|
|
||||||
| // create an iterator and start looping through all the results | ||||||
|
|
@@ -389,7 +411,10 @@ Alternatively, if you wish to use an external package, there is `enrichman/gh-it | |||||
| Its iterator will handle pagination for you, looping through all the available results. | ||||||
|
|
||||||
| ```go | ||||||
| client := github.NewClient(nil) | ||||||
| client, err := github.NewClient() | ||||||
| if err != nil { | ||||||
| // Handle error. | ||||||
| } | ||||||
| var allRepos []*github.Repository | ||||||
|
|
||||||
| // create an iterator and start looping through all the results | ||||||
|
|
@@ -465,12 +490,14 @@ implementing preview features of the GitHub API, we've adopted the following | |||||
| versioning policy: | ||||||
|
|
||||||
| * We increment the **major version** with any incompatible change to | ||||||
| non-preview functionality, including changes to the exported Go API surface | ||||||
| or behavior of the API. | ||||||
| non-preview functionality, including changes to the exported Go API surface | ||||||
| or behavior of the API. | ||||||
|
|
||||||
| * We increment the **minor version** with any backwards-compatible changes to | ||||||
| functionality, as well as any changes to preview functionality in the GitHub | ||||||
| API. GitHub makes no guarantee about the stability of preview functionality, | ||||||
| so neither do we consider it a stable part of the go-github API. | ||||||
| functionality, as well as any changes to preview functionality in the GitHub | ||||||
| API. GitHub makes no guarantee about the stability of preview functionality, | ||||||
| so neither do we consider it a stable part of the go-github API. | ||||||
|
|
||||||
|
Comment on lines
-468
to
+500
Contributor
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. Why were these lines changed? It looks like they are not related to the PR's topic.
Contributor
Author
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've modified the README and these lines weren't consistently formatted. @gmlewis has asked me to fixup files where I've made changes, so I suggest we keep this for the same reason. |
||||||
| * We increment the **patch version** with any backwards-compatible bug fixes. | ||||||
|
|
||||||
| Preview functionality may take the form of entire methods or simply additional | ||||||
|
|
||||||
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.
Unrelated:
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've modified the README and these lines weren't consistently formatted. @gmlewis has asked me to fixup files where I've made changes, so I suggest we keep this for the same reason.