Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
7e6779b4ee0835b6c4519de722e144408a359fff326e675ae5c361f0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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,14 +101,23 @@ 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. | ||
| } | ||
| ``` | ||
| To support more advanced use cases; you can use the `github.WithTransport` option to provide a | ||
| custom `http.RoundTripper` that handles authentication for you, or the `github.WithHTTPClient` | ||
| option to provide a custom `http.Client`. As an example; you can use the `oauth2.Transport` | ||
| from the [golang.org/x/oauth2](https://pkg.go.dev/golang.org/x/oauth2) package to handle OAuth | ||
| token refreshing for you. | ||
| Note that when using an authenticated Client, all calls made by the client will | ||
| include the specified OAuth token. Therefore, authenticated clients should | ||
| almost never be shared between different users. | ||
| @@ -146,7 +161,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 +204,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 | ||
stevehipwell marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples. | ||
| ### Rate Limiting ### | ||
| @@ -296,9 +317,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 +356,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 +397,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 +417,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 +496,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. | ||
stevehipwell marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| * We increment the **patch version** with any backwards-compatible bug fixes. | ||
| Preview functionality may take the form of entire methods or simply additional | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.