Pagination

This commit is contained in:
Tyrel Souza 2021-11-04 21:26:41 -04:00
parent 40c1baa893
commit 291a41c9b0

15
api.py
View File

@ -14,6 +14,19 @@ class API:
with httpx.Client(headers=self.headers) as client: with httpx.Client(headers=self.headers) as client:
return client.get(url).json() return client.get(url).json()
def _get_paged(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) as client:
while True:
resp = client.get(url)
content.extend(resp.json())
if not resp.links.get('next'):
break
url = resp.links['next']['url']
return content
def load_user(self, user_name: str) -> dict: def load_user(self, user_name: str) -> dict:
"""Sets the user object""" """Sets the user object"""
return self._get(f"https://api.github.com/users/{user_name}") return self._get(f"https://api.github.com/users/{user_name}")
@ -24,4 +37,4 @@ class API:
def load_repos(self) -> dict: def load_repos(self) -> dict:
"""Set the users repos""" """Set the users repos"""
return self._get(f"https://api.github.com/users/{self.user_name}/repos") return self._get_paged(f"https://api.github.com/users/{self.user_name}/repos")