Skip to content

Instantly share code, notes, and snippets.

@michaeldorner
Last active March 8, 2025 20:49
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 time import sleep, time
from datetime import timedelta
import requests
from requests.adapters import HTTPAdapter, Retry
API_URL = 'https://api.github.com/' # or 'https://api.github.com/graphql' or others
API_TOKEN = '' # You will need that
TIMEOUT = 10
class GitHubRetry(Retry):
def increment(self, method, url, response=None, error=None, _pool=None, _stacktrace=None):
if response is not None and response.status == 403:
rate_limit_reset = int(response.headers['X-RateLimit-Reset'])
wait_until_reset = max(0, int(rate_limit_reset - time()) + 1)
print(f'wait {timedelta(seconds=wait_until_reset)} for reset')
sleep(wait_until_reset)
return super().increment(method, url, response, error, _pool, _stacktrace)
retries = GitHubRetry(total=10,
backoff_factor=2,
status_forcelist=(403, 500, 501, 502, 503, 504,),
raise_on_status=True)
http_session = requests.session()
http_session.headers.update({
'User-Agent': 'hamster_bth/1.0',
'Accept': 'application/vnd.github+json',
'Authorization': f'Bearer {API_TOKEN}',
'X-Github-Next-Global-ID': '1', # if you would like to use the GraphQL API
})
http_adapter = HTTPAdapter(max_retries=retries)
http_session.mount('https://', http_adapter)
http_session.mount('http://', http_adapter)
# example
print(http_session.get('https://api.github.com/repos/michaeldorner/michaeldorner.de').json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment