Compare commits

..

2 Commits

Author SHA1 Message Date
bf34654a46 Update docstrings 2023-11-11 14:03:33 -05:00
74288909ba Remove HTTP 304 checks 2023-11-11 13:55:31 -05:00

View File

@ -12,6 +12,14 @@ setup_logger()
class GitHubApi:
def __init__(self, token=None, headers=None, proxy=None):
"""Initialize the GitHubApi instance.
Args:
token (str, optional): GitHub API token.
headers (dict, optional): Additional headers to include in API requests.
proxy (dict, optional): Proxy information.
"""
self.base_url = 'https://api.github.com'
self.session = requests.Session()
@ -30,10 +38,19 @@ class GitHubApi:
self.rate_remaining = None
def __get(self, endpoint, params=None):
"""GET an HTTP resource and return JSON."""
"""Send a GET request to the GitHub API and return the JSON response.
Args:
endpoint (str): The API endpoint.
params (dict, optional): Query parameters for the request.
Returns:
dict: The JSON response from the API.
"""
url = self.base_url + endpoint
# Check rate limits
# Check rate limits and sleep if exceeded
if self.rate_remaining == 0 and self.rate_reset > datetime.now():
logger.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
delta = self.rate_reset - datetime.now()
@ -45,21 +62,30 @@ class GitHubApi:
logger.warning(f'Failed to execute GET request: {str(e)}')
return None
# 304 Not Modified
if response.status_code == '304':
return None
self.__parse_headers(response.headers)
return response.json()
def __parse_headers(self, headers):
"""Parse headers to update rate limits."""
"""Parse the response headers to update the rate limit information.
Args:
headers (dict): The response headers from the GitHub API.
"""
self.rate_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset']))
self.rate_remaining = int(headers['x-ratelimit-remaining'])
logger.info(f'{self.rate_remaining} calls remaining until {self.rate_reset} ({self.rate_reset - datetime.now()})')
def get_events(self, page=None):
"""Fetch GitHub events."""
"""Fetch GitHub events.
Args:
page (int, optional): The page number of events to retrieve.
Returns:
dict: The JSON response containing the events.
"""
endpoint = '/events'
params = None