2021-11-04 05:01:35 +00:00
|
|
|
import httpx
|
2021-11-05 02:59:03 +00:00
|
|
|
import os
|
|
|
|
from urllib.parse import urljoin
|
2021-11-04 05:01:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class API:
|
2021-11-05 02:59:03 +00:00
|
|
|
URLBASE = "https://api.github.com" # note: no slash
|
|
|
|
headers = {
|
|
|
|
"Accept": "application/vnd.github.v3+json",
|
|
|
|
"Authorization": f"token {os.environ['GITHUB_TOKEN']}",
|
|
|
|
}
|
2021-11-04 05:01:35 +00:00
|
|
|
|
2021-11-05 01:48:38 +00:00
|
|
|
def raise_on_4xx_5xx(self, response):
|
2021-11-05 02:59:03 +00:00
|
|
|
response.raise_for_status()
|
2021-11-04 05:01:35 +00:00
|
|
|
|
2021-11-05 01:48:38 +00:00
|
|
|
def get(self, url: str) -> list:
|
2021-11-05 01:26:41 +00:00
|
|
|
"""Uses httpx.Client().get to send the appropriate github headers, and return the url"""
|
|
|
|
content = []
|
2021-11-05 02:59:03 +00:00
|
|
|
with httpx.Client(
|
|
|
|
headers=self.headers, event_hooks={"response": [self.raise_on_4xx_5xx]}
|
|
|
|
) as client:
|
|
|
|
return client.get(urljoin(self.URLBASE, url)).json()
|
|
|
|
|
|
|
|
def get_with_pagination(self, url: str) -> list:
|
|
|
|
"""Uses httpx.Client().get to send the appropriate github headers, and return the url"""
|
|
|
|
content = []
|
|
|
|
with httpx.Client(
|
|
|
|
headers=self.headers, event_hooks={"response": [self.raise_on_4xx_5xx]}
|
|
|
|
) as client:
|
2021-11-05 01:26:41 +00:00
|
|
|
while True:
|
2021-11-05 02:59:03 +00:00
|
|
|
resp = client.get(urljoin(self.URLBASE, url))
|
2021-11-05 01:48:38 +00:00
|
|
|
body = resp.json()
|
|
|
|
if isinstance(body, dict):
|
|
|
|
# short circuit if not list
|
|
|
|
return body
|
|
|
|
content.extend(body)
|
2021-11-05 02:59:03 +00:00
|
|
|
if not resp.links.get("next"):
|
2021-11-05 01:26:41 +00:00
|
|
|
break
|
2021-11-05 02:59:03 +00:00
|
|
|
url = resp.links["next"]["url"]
|
2021-11-05 01:26:41 +00:00
|
|
|
return content
|