Skip to content

Instantly share code, notes, and snippets.

@jasonbot
Created September 7, 2017 19:27
Show Gist options
  • Select an option

  • Save jasonbot/d2824b975467a7743dfff4f4049330c1 to your computer and use it in GitHub Desktop.

Select an option

Save jasonbot/d2824b975467a7743dfff4f4049330c1 to your computer and use it in GitHub Desktop.
import time
from github3.models import GitHubCore
def throttle_github(github_like, requests_needed):
"""Takes any github3 objects (repo, org, etc) and will throttle if there are
not at least requests_needed requests remianing before the API starts
throttling. If there are not enough requests left, sleep until the window
resets. Note this uses up one API call to get the rate limit headers, so be
judicious in its use."""
if not isinstance(github_like, GitHubCore):
raise TypeError("Expected a GitHubCore object, got a {0}".format(type(github_like)))
response = github_like._get(github_like._github_url + '/rate_limit')
rate_limit = int(response.headers['X-RateLimit-Limit'])
if requests_needed > rate_limit:
raise ValueError("Maximum window for requests is {0}, given {1}".format(rate_limit, requests_needed))
remaining_requests = int(response.headers['X-RateLimit-Remaining'])
if remaining_requests < requests_needed:
reset_window = int(response.headers['X-RateLimit-Reset'])
throttle_time = reset_window - time.time()
log.info("Throtting Github for %s seconds", throttle_time)
time.sleep(throttle_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment