mirror of
https://git.juggalol.com/agatha/gitmon.git
synced 2024-11-24 16:36:42 +00:00
Compare commits
2 Commits
5a4960a087
...
bf34654a46
Author | SHA1 | Date | |
---|---|---|---|
bf34654a46 | |||
74288909ba |
@ -12,6 +12,14 @@ setup_logger()
|
|||||||
|
|
||||||
class GitHubApi:
|
class GitHubApi:
|
||||||
def __init__(self, token=None, headers=None, proxy=None):
|
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.base_url = 'https://api.github.com'
|
||||||
self.session = requests.Session()
|
self.session = requests.Session()
|
||||||
|
|
||||||
@ -30,10 +38,19 @@ class GitHubApi:
|
|||||||
self.rate_remaining = None
|
self.rate_remaining = None
|
||||||
|
|
||||||
def __get(self, endpoint, params=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
|
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():
|
if self.rate_remaining == 0 and self.rate_reset > datetime.now():
|
||||||
logger.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
|
logger.warning(f'Out of API calls. Sleeping until {self.rate_reset}.')
|
||||||
delta = self.rate_reset - datetime.now()
|
delta = self.rate_reset - datetime.now()
|
||||||
@ -45,21 +62,30 @@ class GitHubApi:
|
|||||||
logger.warning(f'Failed to execute GET request: {str(e)}')
|
logger.warning(f'Failed to execute GET request: {str(e)}')
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# 304 Not Modified
|
|
||||||
if response.status_code == '304':
|
|
||||||
return None
|
|
||||||
|
|
||||||
self.__parse_headers(response.headers)
|
self.__parse_headers(response.headers)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def __parse_headers(self, headers):
|
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_reset = datetime.fromtimestamp(int(headers['x-ratelimit-reset']))
|
||||||
self.rate_remaining = int(headers['x-ratelimit-remaining'])
|
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()})')
|
logger.info(f'{self.rate_remaining} calls remaining until {self.rate_reset} ({self.rate_reset - datetime.now()})')
|
||||||
|
|
||||||
def get_events(self, page=None):
|
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'
|
endpoint = '/events'
|
||||||
params = None
|
params = None
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user