@@ -20,7 +20,7 @@ go-github will require the N-1 major release of Go by default.
2020
2121[ support-policy ] : https://golang.org/doc/devel/release.html#policy
2222
23- ## Development
23+ ## Development ##
2424
2525If you're interested in using the [ GraphQL API v4] [ ] , the recommended library is
2626[ shurcooL/githubv4] [ ] .
@@ -66,7 +66,10 @@ Construct a new GitHub client, then use the various services on the client to
6666access different parts of the GitHub API. For example:
6767
6868``` go
69- client := github.NewClient (nil )
69+ client , err := github.NewClient ()
70+ if err != nil {
71+ // Handle error.
72+ }
7073
7174// list all organizations for user "willnorris"
7275orgs , _ , err := client.Organizations .List (context.Background (), " willnorris" , nil )
@@ -75,7 +78,10 @@ orgs, _, err := client.Organizations.List(context.Background(), "willnorris", ni
7578Some API methods have optional parameters that can be passed. For example:
7679
7780``` go
78- client := github.NewClient (nil )
81+ client , err := github.NewClient ()
82+ if err != nil {
83+ // Handle error.
84+ }
7985
8086// list public repositories for org "github"
8187opt := &github.RepositoryListByOrgOptions {Type: " public" }
@@ -95,14 +101,23 @@ For more sample code snippets, head over to the
95101
96102### Authentication ###
97103
98- Use the ` WithAuthToken ` method to configure your client to authenticate using an
104+ Use the ` github. WithAuthToken` options method to configure your client to authenticate using an
99105OAuth token (for example, a [ personal access token] [ ] ). This is what is needed
100106for a majority of use cases aside from GitHub Apps.
101107
102108``` go
103- client := github.NewClient (nil ).WithAuthToken (" ... your access token ..." )
109+ client , err := github.NewClient (github.WithAuthToken (" ... your access token ..." ))
110+ if err != nil {
111+ // Handle error.
112+ }
104113```
105114
115+ To support more advanced use cases; you can use the ` github.WithTransport ` option to provide a
116+ custom ` http.RoundTripper ` that handles authentication for you, or the ` github.WithHTTPClient `
117+ option to provide a custom ` http.Client ` . As an example; you can use the ` oauth2.Transport `
118+ from the [ golang.org/x/oauth2] ( https://pkg.go.dev/golang.org/x/oauth2 ) package to handle OAuth
119+ token refreshing for you.
120+
106121Note that when using an authenticated Client, all calls made by the client will
107122include the specified OAuth token. Therefore, authenticated clients should
108123almost never be shared between different users.
@@ -146,7 +161,10 @@ func main() {
146161 }
147162
148163 // Use installation transport with client.
149- client := github.NewClient (&http.Client {Transport: itr})
164+ client , err := github.NewClient (github.WithTransport (itr))
165+ if err != nil {
166+ // Handle error.
167+ }
150168
151169 // Use client...
152170}
@@ -186,11 +204,14 @@ func main() {
186204 // InstallationTokenSource has the mechanism to refresh the token when it expires.
187205 httpClient := oauth2.NewClient (context.Background (), installationTokenSource)
188206
189- client := github.NewClient (httpClient)
207+ client , err := github.NewClient (github.WithHTTPClient (httpClient))
208+ if err != nil {
209+ // Handle error.
210+ }
190211}
191212```
192213
193- * Note * : In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
214+ _ Note _ : In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
194215using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples.
195216
196217### Rate Limiting ###
@@ -296,9 +317,10 @@ import (
296317 _ " github.com/bartventer/httpcache/store/memcache" // Register the in-memory backend
297318)
298319
299- client := github.NewClient (
300- httpcache.NewClient (" memcache://" ),
301- ).WithAuthToken (os.Getenv (" GITHUB_TOKEN" ))
320+ client , err := github.NewClient (github.WithHTTPClient (httpcache.NewClient (" memcache://" )), github.WithAuthToken (os.Getenv (" GITHUB_TOKEN" )))
321+ if err != nil {
322+ // Handle error.
323+ }
302324```
303325
304326Alternatively, 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
334356` github.Response ` struct.
335357
336358``` go
337- client := github.NewClient (nil )
359+ client , err := github.NewClient ()
360+ if err != nil {
361+ // Handle error.
362+ }
338363
339364opt := &github.RepositoryListByOrgOptions {
340365 ListOptions : github.ListOptions {PerPage: 10 },
@@ -372,7 +397,10 @@ To handle rate limiting issues, make sure to use a rate-limiting transport.
372397To use these methods, simply create an iterator and then range over it, for example:
373398
374399``` go
375- client := github.NewClient (nil )
400+ client , err := github.NewClient ()
401+ if err != nil {
402+ // Handle error.
403+ }
376404var allRepos []*github.Repository
377405
378406// 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
389417Its iterator will handle pagination for you, looping through all the available results.
390418
391419``` go
392- client := github.NewClient (nil )
420+ client , err := github.NewClient ()
421+ if err != nil {
422+ // Handle error.
423+ }
393424var allRepos []*github.Repository
394425
395426// 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
465496versioning policy:
466497
467498* We increment the ** major version** with any incompatible change to
468- non-preview functionality, including changes to the exported Go API surface
469- or behavior of the API.
499+ non-preview functionality, including changes to the exported Go API surface
500+ or behavior of the API.
501+
470502* We increment the ** minor version** with any backwards-compatible changes to
471- functionality, as well as any changes to preview functionality in the GitHub
472- API. GitHub makes no guarantee about the stability of preview functionality,
473- so neither do we consider it a stable part of the go-github API.
503+ functionality, as well as any changes to preview functionality in the GitHub
504+ API. GitHub makes no guarantee about the stability of preview functionality,
505+ so neither do we consider it a stable part of the go-github API.
506+
474507* We increment the ** patch version** with any backwards-compatible bug fixes.
475508
476509Preview functionality may take the form of entire methods or simply additional
0 commit comments