Skip to content
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

Move RateLimits method to a service #2969

Merged
merged 2 commits into from Oct 20, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
99 changes: 5 additions & 94 deletions github/github.go
Expand Up @@ -203,6 +203,7 @@
Organizations *OrganizationsService
Projects *ProjectsService
PullRequests *PullRequestsService
RateLimit *RateLimitService
Reactions *ReactionsService
Repositories *RepositoriesService
SCIM *SCIMService
Expand Down Expand Up @@ -424,6 +425,7 @@
c.Organizations = (*OrganizationsService)(&c.common)
c.Projects = (*ProjectsService)(&c.common)
c.PullRequests = (*PullRequestsService)(&c.common)
c.RateLimit = (*RateLimitService)(&c.common)
c.Reactions = (*ReactionsService)(&c.common)
c.Repositories = (*RepositoriesService)(&c.common)
c.SCIM = (*SCIMService)(&c.common)
Expand Down Expand Up @@ -1281,54 +1283,6 @@
return false, err
}

// Rate represents the rate limit for the current client.
type Rate struct {
// The number of requests per hour the client is currently limited to.
Limit int `json:"limit"`

// The number of remaining requests the client can make this hour.
Remaining int `json:"remaining"`

// The time at which the current rate limit will reset.
Reset Timestamp `json:"reset"`
}

func (r Rate) String() string {
return Stringify(r)
}

// RateLimits represents the rate limits for the current client.
type RateLimits struct {
// The rate limit for non-search API requests. Unauthenticated
// requests are limited to 60 per hour. Authenticated requests are
// limited to 5,000 per hour.
//
// GitHub API docs: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting
Core *Rate `json:"core"`

// The rate limit for search API requests. Unauthenticated requests
// are limited to 10 requests per minutes. Authenticated requests are
// limited to 30 per minute.
//
// GitHub API docs: https://docs.github.com/en/rest/search#rate-limit
Search *Rate `json:"search"`

// GitHub API docs: https://docs.github.com/en/graphql/overview/resource-limitations#rate-limit
GraphQL *Rate `json:"graphql"`

// GitHub API dos: https://docs.github.com/en/rest/rate-limit
IntegrationManifest *Rate `json:"integration_manifest"`

SourceImport *Rate `json:"source_import"`
CodeScanningUpload *Rate `json:"code_scanning_upload"`
ActionsRunnerRegistration *Rate `json:"actions_runner_registration"`
SCIM *Rate `json:"scim"`
}

func (r RateLimits) String() string {
return Stringify(r)
}

type rateLimitCategory uint8

const (
Expand Down Expand Up @@ -1378,53 +1332,10 @@
}

// RateLimits returns the rate limits for the current client.
//
// Deprecated: Use RateLimitService.Get instead.
func (c *Client) RateLimits(ctx context.Context) (*RateLimits, *Response, error) {
req, err := c.NewRequest("GET", "rate_limit", nil)
if err != nil {
return nil, nil, err
}

response := new(struct {
Resources *RateLimits `json:"resources"`
})

// This resource is not subject to rate limits.
ctx = context.WithValue(ctx, bypassRateLimitCheck, true)
resp, err := c.Do(ctx, req, response)
if err != nil {
return nil, resp, err
}

if response.Resources != nil {
c.rateMu.Lock()
if response.Resources.Core != nil {
c.rateLimits[coreCategory] = *response.Resources.Core
}
if response.Resources.Search != nil {
c.rateLimits[searchCategory] = *response.Resources.Search
}
if response.Resources.GraphQL != nil {
c.rateLimits[graphqlCategory] = *response.Resources.GraphQL
}
if response.Resources.IntegrationManifest != nil {
c.rateLimits[integrationManifestCategory] = *response.Resources.IntegrationManifest
}
if response.Resources.SourceImport != nil {
c.rateLimits[sourceImportCategory] = *response.Resources.SourceImport
}
if response.Resources.CodeScanningUpload != nil {
c.rateLimits[codeScanningUploadCategory] = *response.Resources.CodeScanningUpload
}
if response.Resources.ActionsRunnerRegistration != nil {
c.rateLimits[actionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration
}
if response.Resources.SCIM != nil {
c.rateLimits[scimCategory] = *response.Resources.SCIM
}
c.rateMu.Unlock()
}

return response.Resources, resp, nil
return c.RateLimit.Get(ctx)

Check warning on line 1338 in github/github.go

View check run for this annotation

Codecov / codecov/patch

github/github.go#L1338

Added line #L1338 was not covered by tests
}

func setCredentialsAsHeaders(req *http.Request, id, secret string) *http.Request {
Expand Down