github.com/google/go-github/v57@v57.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 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  }
    56  
    57  func (r RateLimits) String() string {
    58  	return Stringify(r)
    59  }
    60  
    61  // Get returns the rate limits for the current client.
    62  //
    63  // GitHub API docs: https://docs.github.com/rest/rate-limit/rate-limit#get-rate-limit-status-for-the-authenticated-user
    64  //
    65  //meta:operation GET /rate_limit
    66  func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, error) {
    67  	req, err := s.client.NewRequest("GET", "rate_limit", nil)
    68  	if err != nil {
    69  		return nil, nil, err
    70  	}
    71  
    72  	response := new(struct {
    73  		Resources *RateLimits `json:"resources"`
    74  	})
    75  
    76  	// This resource is not subject to rate limits.
    77  	ctx = context.WithValue(ctx, bypassRateLimitCheck, true)
    78  	resp, err := s.client.Do(ctx, req, response)
    79  	if err != nil {
    80  		return nil, resp, err
    81  	}
    82  
    83  	if response.Resources != nil {
    84  		s.client.rateMu.Lock()
    85  		if response.Resources.Core != nil {
    86  			s.client.rateLimits[coreCategory] = *response.Resources.Core
    87  		}
    88  		if response.Resources.Search != nil {
    89  			s.client.rateLimits[searchCategory] = *response.Resources.Search
    90  		}
    91  		if response.Resources.GraphQL != nil {
    92  			s.client.rateLimits[graphqlCategory] = *response.Resources.GraphQL
    93  		}
    94  		if response.Resources.IntegrationManifest != nil {
    95  			s.client.rateLimits[integrationManifestCategory] = *response.Resources.IntegrationManifest
    96  		}
    97  		if response.Resources.SourceImport != nil {
    98  			s.client.rateLimits[sourceImportCategory] = *response.Resources.SourceImport
    99  		}
   100  		if response.Resources.CodeScanningUpload != nil {
   101  			s.client.rateLimits[codeScanningUploadCategory] = *response.Resources.CodeScanningUpload
   102  		}
   103  		if response.Resources.ActionsRunnerRegistration != nil {
   104  			s.client.rateLimits[actionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration
   105  		}
   106  		if response.Resources.SCIM != nil {
   107  			s.client.rateLimits[scimCategory] = *response.Resources.SCIM
   108  		}
   109  		s.client.rateMu.Unlock()
   110  	}
   111  
   112  	return response.Resources, resp, nil
   113  }