| 1 |
// Copyright 2023 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 |
| 7 |
|
| 8 |
import "context" |
| 9 |
|
| 10 |
// RateLimitService provides access to rate limit functions in the GitHub API. |
| 11 |
type RateLimitService service |
| 12 |
|
| 13 |
// Rate represents the rate limit for the current client. |
| 14 |
type Rate struct { |
| 15 |
// The number of requests per hour the client is currently limited to. |
| 16 |
Limit int `json:"limit"` |
| 17 |
|
| 18 |
// The number of remaining requests the client can make this hour. |
| 19 |
Remaining int `json:"remaining"` |
| 20 |
|
| 21 |
// The time at which the current rate limit will reset. |
| 22 |
Reset Timestamp `json:"reset"` |
| 23 |
} |
| 24 |
|
| 25 |
func (r Rate) String() string { |
| 26 |
return Stringify(r) |
| 27 |
} |
| 28 |
|
| 29 |
// RateLimits represents the rate limits for the current client. |
| 30 |
type RateLimits struct { |
| 31 |
// The rate limit for non-search API requests. Unauthenticated |
| 32 |
// requests are limited to 60 per hour. Authenticated requests are |
| 33 |
// limited to 5,000 per hour. |
| 34 |
// |
| 35 |
// GitHub API docs: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting |
| 36 |
Core *Rate `json:"core"` |
| 37 |
|
| 38 |
// The rate limit for search API requests. Unauthenticated requests |
| 39 |
// are limited to 10 requests per minutes. Authenticated requests are |
| 40 |
// limited to 30 per minute. |
| 41 |
// |
| 42 |
// GitHub API docs: https://docs.github.com/en/rest/search#rate-limit |
| 43 |
Search *Rate `json:"search"` |
| 44 |
|
| 45 |
// GitHub API docs: https://docs.github.com/en/graphql/overview/resource-limitations#rate-limit |
| 46 |
GraphQL *Rate `json:"graphql"` |
| 47 |
|
| 48 |
// GitHub API dos: https://docs.github.com/en/rest/rate-limit |
| 49 |
IntegrationManifest *Rate `json:"integration_manifest"` |
| 50 |
|
| 51 |
SourceImport *Rate `json:"source_import"` |
| 52 |
CodeScanningUpload *Rate `json:"code_scanning_upload"` |
| 53 |
ActionsRunnerRegistration *Rate `json:"actions_runner_registration"` |
| 54 |
SCIM *Rate `json:"scim"` |
| 55 |
DependencySnapshots *Rate `json:"dependency_snapshots"` |
| 56 |
CodeSearch *Rate `json:"code_search"` |
| 57 |
} |
| 58 |
|
| 59 |
func (r RateLimits) String() string { |
| 60 |
return Stringify(r) |
| 61 |
} |
| 62 |
|
| 63 |
// Get returns the rate limits for the current client. |
| 64 |
// |
| 65 |
// GitHub API docs: https://docs.github.com/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user |
| 66 |
// |
| 67 |
//meta:operation GET /rate_limit |
| 68 |
func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, error) { |
| 69 |
req, err := s.client.NewRequest("GET", "rate_limit", nil) |
| 70 |
if err != nil { |
| 71 |
return nil, nil, err |
| 72 |
} |
| 73 |
|
| 74 |
response := new(struct { |
| 75 |
Resources *RateLimits `json:"resources"` |
| 76 |
}) |
| 77 |
|
| 78 |
// This resource is not subject to rate limits. |
| 79 |
ctx = context.WithValue(ctx, bypassRateLimitCheck, true) |
| 80 |
resp, err := s.client.Do(ctx, req, response) |
| 81 |
if err != nil { |
| 82 |
return nil, resp, err |
| 83 |
} |
| 84 |
|
| 85 |
if response.Resources != nil { |
| 86 |
s.client.rateMu.Lock() |
| 87 |
if response.Resources.Core != nil { |
| 88 |
s.client.rateLimits[coreCategory] = *response.Resources.Core |
| 89 |
} |
| 90 |
if response.Resources.Search != nil { |
| 91 |
s.client.rateLimits[searchCategory] = *response.Resources.Search |
| 92 |
} |
| 93 |
if response.Resources.GraphQL != nil { |
| 94 |
s.client.rateLimits[graphqlCategory] = *response.Resources.GraphQL |
| 95 |
} |
| 96 |
if response.Resources.IntegrationManifest != nil { |
| 97 |
s.client.rateLimits[integrationManifestCategory] = *response.Resources.IntegrationManifest |
| 98 |
} |
| 99 |
if response.Resources.SourceImport != nil { |
| 100 |
s.client.rateLimits[sourceImportCategory] = *response.Resources.SourceImport |
| 101 |
} |
| 102 |
if response.Resources.CodeScanningUpload != nil { |
| 103 |
s.client.rateLimits[codeScanningUploadCategory] = *response.Resources.CodeScanningUpload |
| 104 |
} |
| 105 |
if response.Resources.ActionsRunnerRegistration != nil { |
| 106 |
s.client.rateLimits[actionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration |
| 107 |
} |
| 108 |
if response.Resources.SCIM != nil { |
| 109 |
s.client.rateLimits[scimCategory] = *response.Resources.SCIM |
| 110 |
} |
| 111 |
if response.Resources.DependencySnapshots != nil { |
| 112 |
s.client.rateLimits[dependencySnapshotsCategory] = *response.Resources.DependencySnapshots |
| 113 |
} |
| 114 |
if response.Resources.CodeSearch != nil { |
| 115 |
s.client.rateLimits[codeSearchCategory] = *response.Resources.CodeSearch |
| 116 |
} |
| 117 |
s.client.rateMu.Unlock() |
| 118 |
} |
| 119 |
|
| 120 |
return response.Resources, resp, nil |
| 121 |
} |