doc.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. /*
  6. Package github provides a client for using the GitHub API.
  7. Usage:
  8. import "github.com/google/go-github/github"
  9. Construct a new GitHub client, then use the various services on the client to
  10. access different parts of the GitHub API. For example:
  11. client := github.NewClient(nil)
  12. // list all organizations for user "willnorris"
  13. orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)
  14. Some API methods have optional parameters that can be passed. For example:
  15. client := github.NewClient(nil)
  16. // list public repositories for org "github"
  17. opt := &github.RepositoryListByOrgOptions{Type: "public"}
  18. repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)
  19. The services of a client divide the API into logical chunks and correspond to
  20. the structure of the GitHub API documentation at
  21. https://developer.github.com/v3/.
  22. NOTE: Using the https://godoc.org/context package, one can easily
  23. pass cancelation signals and deadlines to various services of the client for
  24. handling a request. In case there is no context available, then context.Background()
  25. can be used as a starting point.
  26. For more sample code snippets, head over to the https://github.com/google/go-github/tree/master/example directory.
  27. Authentication
  28. The go-github library does not directly handle authentication. Instead, when
  29. creating a new client, pass an http.Client that can handle authentication for
  30. you. The easiest and recommended way to do this is using the golang.org/x/oauth2
  31. library, but you can always use any other library that provides an http.Client.
  32. If you have an OAuth2 access token (for example, a personal API token), you can
  33. use it with the oauth2 library using:
  34. import "golang.org/x/oauth2"
  35. func main() {
  36. ctx := context.Background()
  37. ts := oauth2.StaticTokenSource(
  38. &oauth2.Token{AccessToken: "... your access token ..."},
  39. )
  40. tc := oauth2.NewClient(ctx, ts)
  41. client := github.NewClient(tc)
  42. // list all repositories for the authenticated user
  43. repos, _, err := client.Repositories.List(ctx, "", nil)
  44. }
  45. Note that when using an authenticated Client, all calls made by the client will
  46. include the specified OAuth token. Therefore, authenticated clients should
  47. almost never be shared between different users.
  48. See the oauth2 docs for complete instructions on using that library.
  49. For API methods that require HTTP Basic Authentication, use the
  50. BasicAuthTransport.
  51. GitHub Apps authentication can be provided by the
  52. https://github.com/bradleyfalzon/ghinstallation package.
  53. import "github.com/bradleyfalzon/ghinstallation"
  54. func main() {
  55. // Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
  56. itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem")
  57. if err != nil {
  58. // Handle error.
  59. }
  60. // Use installation transport with client
  61. client := github.NewClient(&http.Client{Transport: itr})
  62. // Use client...
  63. }
  64. Rate Limiting
  65. GitHub imposes a rate limit on all API clients. Unauthenticated clients are
  66. limited to 60 requests per hour, while authenticated clients can make up to
  67. 5,000 requests per hour. The Search API has a custom rate limit. Unauthenticated
  68. clients are limited to 10 requests per minute, while authenticated clients
  69. can make up to 30 requests per minute. To receive the higher rate limit when
  70. making calls that are not issued on behalf of a user,
  71. use UnauthenticatedRateLimitedTransport.
  72. The returned Response.Rate value contains the rate limit information
  73. from the most recent API call. If a recent enough response isn't
  74. available, you can use RateLimits to fetch the most up-to-date rate
  75. limit data for the client.
  76. To detect an API rate limit error, you can check if its type is *github.RateLimitError:
  77. repos, _, err := client.Repositories.List(ctx, "", nil)
  78. if _, ok := err.(*github.RateLimitError); ok {
  79. log.Println("hit rate limit")
  80. }
  81. Learn more about GitHub rate limiting at
  82. https://developer.github.com/v3/#rate-limiting.
  83. Accepted Status
  84. Some endpoints may return a 202 Accepted status code, meaning that the
  85. information required is not yet ready and was scheduled to be gathered on
  86. the GitHub side. Methods known to behave like this are documented specifying
  87. this behavior.
  88. To detect this condition of error, you can check if its type is
  89. *github.AcceptedError:
  90. stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
  91. if _, ok := err.(*github.AcceptedError); ok {
  92. log.Println("scheduled on GitHub side")
  93. }
  94. Conditional Requests
  95. The GitHub API has good support for conditional requests which will help
  96. prevent you from burning through your rate limit, as well as help speed up your
  97. application. go-github does not handle conditional requests directly, but is
  98. instead designed to work with a caching http.Transport. We recommend using
  99. https://github.com/gregjones/httpcache for that.
  100. Learn more about GitHub conditional requests at
  101. https://developer.github.com/v3/#conditional-requests.
  102. Creating and Updating Resources
  103. All structs for GitHub resources use pointer values for all non-repeated fields.
  104. This allows distinguishing between unset fields and those set to a zero-value.
  105. Helper functions have been provided to easily create these pointers for string,
  106. bool, and int values. For example:
  107. // create a new private repository named "foo"
  108. repo := &github.Repository{
  109. Name: github.String("foo"),
  110. Private: github.Bool(true),
  111. }
  112. client.Repositories.Create(ctx, "", repo)
  113. Users who have worked with protocol buffers should find this pattern familiar.
  114. Pagination
  115. All requests for resource collections (repos, pull requests, issues, etc.)
  116. support pagination. Pagination options are described in the
  117. github.ListOptions struct and passed to the list methods directly or as an
  118. embedded type of a more specific list options struct (for example
  119. github.PullRequestListOptions). Pages information is available via the
  120. github.Response struct.
  121. client := github.NewClient(nil)
  122. opt := &github.RepositoryListByOrgOptions{
  123. ListOptions: github.ListOptions{PerPage: 10},
  124. }
  125. // get all pages of results
  126. var allRepos []*github.Repository
  127. for {
  128. repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
  129. if err != nil {
  130. return err
  131. }
  132. allRepos = append(allRepos, repos...)
  133. if resp.NextPage == 0 {
  134. break
  135. }
  136. opt.Page = resp.NextPage
  137. }
  138. */
  139. package github