Skip to content

Instantly share code, notes, and snippets.

@michaeldorner
Created October 28, 2023 17:14
Show Gist options
  • Save michaeldorner/9a7c852c1ae2bdfcd088157c7e5d8045 to your computer and use it in GitHub Desktop.
Save michaeldorner/9a7c852c1ae2bdfcd088157c7e5d8045 to your computer and use it in GitHub Desktop.
urllib3 and requests retry for REST and GraphQL GitHub API
from urllib3 import BaseHTTPResponse, Retry
import time
class GitHubRetry(Retry):
def get_retry_after(self, response: BaseHTTPResponse) -> float | None:
if 'X-RateLimit-Remaining' in response.headers and 'X-RateLimit-Reset' in response.headers:
remaining_requests = int(response.headers['X-RateLimit-Remaining'])
if remaining_requests > 0:
return None
else:
reset_time = int(response.headers['X-RateLimit-Reset'])
current_time = int(time.time())
return float(reset_time - current_time)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment