本文整理匯總了Golang中github.com/google/go-github/github.NewClient函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewClient函數的具體用法?Golang NewClient怎麽用?Golang NewClient使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewClient函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
flag.Parse()
token := os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
println("!!! No OAuth token. Some tests won't run. !!!\n")
client = github.NewClient(nil)
} else {
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
client = github.NewClient(t.Client())
auth = true
}
for _, tt := range []struct {
url string
typ interface{}
}{
//{"rate_limit", &github.RateLimits{}},
{"users/octocat", &github.User{}},
{"user", &github.User{}},
{"users/willnorris/keys", &[]github.Key{}},
{"orgs/google-test", &github.Organization{}},
{"repos/google/go-github", &github.Repository{}},
{"/gists/9257657", &github.Gist{}},
} {
err := testType(tt.url, tt.typ)
if err != nil {
fmt.Printf("error: %v\n", err)
}
}
}
示例2: createGithubClient
func (grp githubRegistryProvider) createGithubClient(credentialName string) (*http.Client, *github.Client, error) {
if credentialName == "" {
return http.DefaultClient, github.NewClient(nil), nil
}
c, err := grp.cp.GetCredential(credentialName)
if err != nil {
log.Printf("Failed to fetch credential %s: %v", credentialName, err)
log.Print("Trying to use unauthenticated client")
return http.DefaultClient, github.NewClient(nil), nil
}
if c != nil {
if c.APIToken != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: string(c.APIToken)},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
return tc, github.NewClient(tc), nil
}
if c.BasicAuth.Username != "" && c.BasicAuth.Password != "" {
tp := github.BasicAuthTransport{
Username: c.BasicAuth.Username,
Password: c.BasicAuth.Password,
}
return tp.Client(), github.NewClient(tp.Client()), nil
}
}
return nil, nil, fmt.Errorf("No suitable credential found for %s", credentialName)
}
示例3: Before
// Before gets called before any action on every execution.
func Before() cli.BeforeFunc {
return func(c *cli.Context) error {
logrus.SetOutput(os.Stdout)
logrus.SetLevel(logrus.DebugLevel)
if c.BoolT("update") {
Update()
}
if config.Token != "" {
config.Client = github.NewClient(
oauth2.NewClient(
oauth2.NoContext,
oauth2.StaticTokenSource(
&oauth2.Token{
AccessToken: config.Token,
},
),
),
)
} else {
config.Client = github.NewClient(nil)
}
return nil
}
}
示例4: TestAsciiCatRespRecorder
// TestAsciiCatRespRecorder uses net/http/httptest ResponseRecorder
// (https://godoc.org/net/http/httptest#ResponseRecorder) to test the AsciiCat
// handler directly.
//
// ResponseRecorder is useful for direct testing of handlers,
// but doesn't provide a complete solution when the router itself handles complex logic.
// See TestGetIssuesTestSrv in get_issues_test.go for an example of testing complex router logic
func TestAsciiCatRespRecorder(t *testing.T) {
// create a ResponseRecorder, which implements http.ResponseWriter. it will be passed into the handler
w := httptest.NewRecorder()
// create a fake request to be passed into the handler
r, err := http.NewRequest("GET", "/ascii_cat", nil)
if err != nil {
t.Fatalf("error constructing test HTTP request [%s]", err)
}
// create and execute the handler, passing the ResponseRecorder and fake request
handler := AsciiCat(github.NewClient(nil))
handler.ServeHTTP(w, r)
// now that the request has been 'served' by the handler, check the response that it would
// have returned
if w.Code != http.StatusOK {
t.Fatalf("expected code %d, got %d", http.StatusOK, w.Code)
}
bodyStr := string(w.Body.Bytes())
if len(bodyStr) <= 0 {
t.Fatalf("expected non-empty response body")
}
ghClient := github.NewClient(nil)
expectedCat, _, err := ghClient.Octocat("Hello, Go In 5 Minutes Viewer!")
if err != nil {
t.Fatalf("error getting expected octocat string [%s]", err)
}
if bodyStr != expectedCat {
t.Fatalf("got unexpected octocat string [%s]", bodyStr)
}
// ResponseRecorder records more useful data about the response.
// see http://godoc.org/net/http/httptest#ResponseRecorder for details
}
示例5: downloadReleaseFromGithub
func downloadReleaseFromGithub(r repo, artifact string, alias string) {
client := github.NewClient(nil)
config, err := loadConfig()
if err != nil {
DieWithError(err, "Could not load user config")
}
if token, ok := config.Config["token"]; ok {
log.Debug("Using Github token from config.")
// if the user has a token stored, use it
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = github.NewClient(tc)
} else {
log.Debug("No Github token found in config. Add it with `mup config` if you need it")
}
// with a provided token, this will also get repos for the authenticated user
// TODO: support pagination
releases, resp, err := client.Repositories.ListReleases(r.Owner, r.Repo, nil)
if resp.StatusCode == 404 {
Die("Could not find repository %s/%s. Maybe it doesn't exist, or you need to add your token for μpdater to have access.", r.Owner, r.Repo)
} else if err != nil {
DieWithError(err, "Error reaching Github")
}
updateFromGithubReleases(client, r, releases, "", artifact, alias)
}
示例6: MakeClient
func MakeClient(token string) *github.Client {
if len(token) > 0 {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(oauth2.NoContext, ts)
return github.NewClient(tc)
}
return github.NewClient(nil)
}
示例7: makeGithubClient
func makeGithubClient(token string) *github.Client {
if token != "" {
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: token},
}
return github.NewClient(t.Client())
}
return github.NewClient(nil)
}
示例8: buildGithubClient
func buildGithubClient() *github.Client {
if token != "" {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
httpClient := oauth2.NewClient(oauth2.NoContext, tokenSource)
return github.NewClient(httpClient)
}
return github.NewClient(nil)
}
示例9: newGithubClient
func newGithubClient() githubClient {
gt := os.Getenv(gitHubAPITokenEnvVar)
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: gt},
}
github.NewClient(t.Client())
c := github.NewClient(t.Client())
return githubClientProd{
org: c.Organizations,
repo: c.Repositories,
}
}
示例10: newClient
func newClient() *github.Client {
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken != "" {
oauthTransport := &oauth.Transport{
Token: &oauth.Token{AccessToken: githubToken},
}
return github.NewClient(oauthTransport.Client())
}
return github.NewClient(nil)
}
示例11: getGithubClient
func getGithubClient(db *gorp.DbMap) *github.Client {
token := getConfig(db).GithubToken
if token != "" {
ts := &tokenSource{
&oauth2.Token{AccessToken: token},
}
tc := oauth2.NewClient(oauth2.NoContext, ts)
return github.NewClient(tc)
} else {
return github.NewClient(nil)
}
}
示例12: GetGithubClient
func GetGithubClient(owner string, repository string) (*github.Client, error) {
accessToken, getTokenError := loadAccessToken(owner, repository)
if getTokenError != nil {
return github.NewClient(nil), getTokenError
}
t := &oauth.Transport{
Token: &oauth.Token{AccessToken: accessToken},
}
return github.NewClient(t.Client()), nil
}
示例13: newClient
func newClient(accessToken string) (client *github.Client) {
if accessToken == "" {
client = github.NewClient(nil)
} else {
transport := &oauth.Transport{
Token: &oauth.Token{AccessToken: accessToken},
}
client = github.NewClient(transport.Client())
}
return
}
示例14: init
func init() {
token := os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
print("!!! No OAuth token. Some tests won't run. !!!\n\n")
client = github.NewClient(nil)
} else {
tc := oauth2.NewClient(oauth2.NoContext, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client = github.NewClient(tc)
auth = true
}
}
示例15: CreateClientConnection
// CreateClientConnection is a helper function for creating a connection to the
// Github API based on whether or not an auth token is supplied.
func CreateClientConnection() *github.Client {
var client *github.Client
config := new(Configuration)
if config.Github.Token == "empty" {
client = github.NewClient(nil)
return client
} else {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: TOKEN})
tc := oauth2.NewClient(oauth2.NoContext, ts)
client = github.NewClient(tc)
return client
}
}