github.com/google/go-github/v70@v70.0.0/github/rate_limit.go (about) 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 maximum number of requests that you can make per hour. 16 Limit int `json:"limit"` 17 18 // The number of requests remaining in the current rate limit window. 19 Remaining int `json:"remaining"` 20 21 // The number of requests you have made in the current rate limit window. 22 Used int `json:"used"` 23 24 // The time at which the current rate limit window resets, in UTC epoch seconds. 25 Reset Timestamp `json:"reset"` 26 27 // The rate limit resource that the request counted against. 28 // For more information about the different resources, see REST API endpoints for rate limits. 29 // GitHub API docs: https://docs.github.com/en/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user 30 Resource string `json:"resource,omitempty"` 31 } 32 33 func (r Rate) String() string { 34 return Stringify(r) 35 } 36 37 // RateLimits represents the rate limits for the current client. 38 type RateLimits struct { 39 // The rate limit for non-search API requests. Unauthenticated 40 // requests are limited to 60 per hour. Authenticated requests are 41 // limited to 5,000 per hour. 42 // 43 // GitHub API docs: https://docs.github.com/en/rest/overview/resources-in-the-rest-api#rate-limiting 44 Core *Rate `json:"core"` 45 46 // The rate limit for search API requests. Unauthenticated requests 47 // are limited to 10 requests per minutes. Authenticated requests are 48 // limited to 30 per minute. 49 // 50 // GitHub API docs: https://docs.github.com/en/rest/search#rate-limit 51 Search *Rate `json:"search"` 52 53 // GitHub API docs: https://docs.github.com/en/graphql/overview/resource-limitations#rate-limit 54 GraphQL *Rate `json:"graphql"` 55 56 // GitHub API dos: https://docs.github.com/en/rest/rate-limit 57 IntegrationManifest *Rate `json:"integration_manifest"` 58 59 SourceImport *Rate `json:"source_import"` 60 CodeScanningUpload *Rate `json:"code_scanning_upload"` 61 ActionsRunnerRegistration *Rate `json:"actions_runner_registration"` 62 SCIM *Rate `json:"scim"` 63 DependencySnapshots *Rate `json:"dependency_snapshots"` 64 CodeSearch *Rate `json:"code_search"` 65 AuditLog *Rate `json:"audit_log"` 66 } 67 68 func (r RateLimits) String() string { 69 return Stringify(r) 70 } 71 72 // Get returns the rate limits for the current client. 73 // 74 // GitHub API docs: https://docs.github.com/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user 75 // 76 //meta:operation GET /rate_limit 77 func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, error) { 78 req, err := s.client.NewRequest("GET", "rate_limit", nil) 79 if err != nil { 80 return nil, nil, err 81 } 82 83 response := new(struct { 84 Resources *RateLimits `json:"resources"` 85 }) 86 87 // This resource is not subject to rate limits. 88 ctx = context.WithValue(ctx, BypassRateLimitCheck, true) 89 resp, err := s.client.Do(ctx, req, response) 90 if err != nil { 91 return nil, resp, err 92 } 93 94 if response.Resources != nil { 95 s.client.rateMu.Lock() 96 if response.Resources.Core != nil { 97 s.client.rateLimits[CoreCategory] = *response.Resources.Core 98 } 99 if response.Resources.Search != nil { 100 s.client.rateLimits[SearchCategory] = *response.Resources.Search 101 } 102 if response.Resources.GraphQL != nil { 103 s.client.rateLimits[GraphqlCategory] = *response.Resources.GraphQL 104 } 105 if response.Resources.IntegrationManifest != nil { 106 s.client.rateLimits[IntegrationManifestCategory] = *response.Resources.IntegrationManifest 107 } 108 if response.Resources.SourceImport != nil { 109 s.client.rateLimits[SourceImportCategory] = *response.Resources.SourceImport 110 } 111 if response.Resources.CodeScanningUpload != nil { 112 s.client.rateLimits[CodeScanningUploadCategory] = *response.Resources.CodeScanningUpload 113 } 114 if response.Resources.ActionsRunnerRegistration != nil { 115 s.client.rateLimits[ActionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration 116 } 117 if response.Resources.SCIM != nil { 118 s.client.rateLimits[ScimCategory] = *response.Resources.SCIM 119 } 120 if response.Resources.DependencySnapshots != nil { 121 s.client.rateLimits[DependencySnapshotsCategory] = *response.Resources.DependencySnapshots 122 } 123 if response.Resources.CodeSearch != nil { 124 s.client.rateLimits[CodeSearchCategory] = *response.Resources.CodeSearch 125 } 126 if response.Resources.AuditLog != nil { 127 s.client.rateLimits[AuditLogCategory] = *response.Resources.AuditLog 128 } 129 s.client.rateMu.Unlock() 130 } 131 132 return response.Resources, resp, nil 133 }