Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ access different parts of the GitHub API. For example:
client := github.NewClient(nil)

// list all organizations for user "willnorris"
orgs, _, err := client.Organizations.List("willnorris", nil)
orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)
```

Some API methods have optional parameters that can be passed. For example:
Expand All @@ -32,7 +32,7 @@ client := github.NewClient(nil)

// list public repositories for org "github"
opt := &github.RepositoryListByOrgOptions{Type: "public"}
repos, _, err := client.Repositories.ListByOrg("github", opt)
repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)
```

The services of a client divide the API into logical chunks and correspond to
Expand All @@ -52,15 +52,16 @@ API token][]), you can use it with the oauth2 library using:
import "golang.org/x/oauth2"

func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "... your access token ..."},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

// list all repositories for the authenticated user
repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
}
```

Expand Down Expand Up @@ -90,7 +91,7 @@ up-to-date rate limit data for the client.
To detect an API rate limit error, you can check if its type is `*github.RateLimitError`:

```go
repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
Expand All @@ -110,7 +111,7 @@ To detect this condition of error, you can check if its type is
`*github.AcceptedError`:

```go
stats, _, err := client.Repositories.ListContributorsStats(org, repo)
stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
if _, ok := err.(*github.AcceptedError); ok {
log.Println("scheduled on GitHub side")
}
Expand Down Expand Up @@ -140,7 +141,7 @@ repo := &github.Repository{
Name: github.String("foo"),
Private: github.Bool(true),
}
client.Repositories.Create("", repo)
client.Repositories.Create(ctx, "", repo)
```

Users who have worked with protocol buffers should find this pattern familiar.
Expand All @@ -163,7 +164,7 @@ opt := &github.RepositoryListByOrgOptions{
// get all pages of results
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg("github", opt)
repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
if err != nil {
return err
}
Expand Down
17 changes: 9 additions & 8 deletions github/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ access different parts of the GitHub API. For example:
client := github.NewClient(nil)

// list all organizations for user "willnorris"
orgs, _, err := client.Organizations.List("willnorris", nil)
orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)

Some API methods have optional parameters that can be passed. For example:

client := github.NewClient(nil)

// list public repositories for org "github"
opt := &github.RepositoryListByOrgOptions{Type: "public"}
repos, _, err := client.Repositories.ListByOrg("github", opt)
repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)

The services of a client divide the API into logical chunks and correspond to
the structure of the GitHub API documentation at
Expand All @@ -42,15 +42,16 @@ use it with the oauth2 library using:
import "golang.org/x/oauth2"

func main() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "... your access token ..."},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)

// list all repositories for the authenticated user
repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
}

Note that when using an authenticated Client, all calls made by the client will
Expand Down Expand Up @@ -78,7 +79,7 @@ up-to-date rate limit data for the client.

To detect an API rate limit error, you can check if its type is *github.RateLimitError:

repos, _, err := client.Repositories.List("", nil)
repos, _, err := client.Repositories.List(ctx, "", nil)
if _, ok := err.(*github.RateLimitError); ok {
log.Println("hit rate limit")
}
Expand All @@ -96,7 +97,7 @@ this behavior.
To detect this condition of error, you can check if its type is
*github.AcceptedError:

stats, _, err := client.Repositories.ListContributorsStats(org, repo)
stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
if _, ok := err.(*github.AcceptedError); ok {
log.Println("scheduled on GitHub side")
}
Expand Down Expand Up @@ -124,7 +125,7 @@ bool, and int values. For example:
Name: github.String("foo"),
Private: github.Bool(true),
}
client.Repositories.Create("", repo)
client.Repositories.Create(ctx, "", repo)

Users who have worked with protocol buffers should find this pattern familiar.

Expand All @@ -145,7 +146,7 @@ github.Response struct.
// get all pages of results
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.ListByOrg("github", opt)
repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package tests

import (
"context"
"fmt"
"math/rand"
"net/http"
Expand All @@ -31,7 +32,7 @@ func init() {
print("!!! No OAuth token. Some tests won't run. !!!\n\n")
client = github.NewClient(nil)
} else {
tc := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client = github.NewClient(tc)
Expand Down