+
Skip to content

Add NewTokenClient #2637

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

Merged
merged 6 commits into from
Jan 25, 2023
Merged
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
6 changes: 6 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"time"

"github.com/google/go-querystring/query"
"golang.org/x/oauth2"
)

const (
Expand Down Expand Up @@ -346,6 +347,11 @@ func NewClient(httpClient *http.Client) *Client {
return c
}

// NewTokenClient returns a new GitHub API client authenticated with the provided token.
func NewTokenClient(ctx context.Context, token string) *Client {
return NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})))
}

// NewEnterpriseClient returns a new GitHub API client with provided
// base URL and upload URL (often is your GitHub Enterprise hostname).
// If the base URL does not have the suffix "/api/v3/", it will be added automatically.
Expand Down
14 changes: 14 additions & 0 deletions github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"golang.org/x/oauth2"
)

const (
Expand Down Expand Up @@ -266,6 +267,19 @@ func TestClient(t *testing.T) {
}
}

func TestNewTokenClient(t *testing.T) {
token := "gh_test_token"
ctx := context.Background()
c := NewTokenClient(ctx, token)
tr, ok := c.Client().Transport.(*oauth2.Transport)
if !ok {
t.Error("Client transport is not oauth2.Transport")
}
if tok, err := tr.Source.Token(); err != nil || tok.AccessToken != token {
t.Errorf("Client not using correct token")
}
}

func TestNewEnterpriseClient(t *testing.T) {
baseURL := "https://custom-url/api/v3/"
uploadURL := "https://custom-upload-url/api/uploads/"
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载