mirror of
https://git.juggalol.com/agatha/gitmon.git
synced 2024-11-22 23:46:42 +00:00
Sleep until rate limit refresh
This commit is contained in:
parent
9387706f61
commit
2e2756db07
@ -1,6 +1,8 @@
|
|||||||
"""GitHub API module"""
|
"""GitHub API module"""
|
||||||
import logging
|
import logging
|
||||||
import requests
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
from time import sleep
|
||||||
from requests.adapters import HTTPAdapter
|
from requests.adapters import HTTPAdapter
|
||||||
from requests.packages.urllib3.util.retry import Retry
|
from requests.packages.urllib3.util.retry import Retry
|
||||||
|
|
||||||
@ -26,6 +28,12 @@ class GitHubApi:
|
|||||||
"""GET an HTTP resource and return JSON."""
|
"""GET an HTTP resource and return JSON."""
|
||||||
url = self.base_url + endpoint
|
url = self.base_url + endpoint
|
||||||
|
|
||||||
|
# Check rate limits
|
||||||
|
if self.rate_remaining == 0 and self.rate_reset > datetime.now():
|
||||||
|
logging.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
|
||||||
|
delta = self.rate_reset - datetime.now()
|
||||||
|
sleep(delta.total_seconds())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = self.session.get(url, params=params)
|
response = self.session.get(url, params=params)
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
@ -41,7 +49,7 @@ class GitHubApi:
|
|||||||
|
|
||||||
def __parse_headers(self, headers):
|
def __parse_headers(self, headers):
|
||||||
"""Parse headers to update rate limits."""
|
"""Parse headers to update rate limits."""
|
||||||
self.rate_reset = int(headers['x-ratelimit-reset'])
|
self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset']))
|
||||||
self.rate_remaining = int(headers['x-ratelimit-remaining'])
|
self.rate_remaining = int(headers['x-ratelimit-remaining'])
|
||||||
|
|
||||||
def get_events(self, page=None):
|
def get_events(self, page=None):
|
||||||
|
Loading…
Reference in New Issue
Block a user