From 291a41c9b0f5852dd8deb46fe8eb8e016e66fa67 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Thu, 4 Nov 2021 21:26:41 -0400 Subject: [PATCH] Pagination --- api.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/api.py b/api.py index 10817ad..79a11e1 100644 --- a/api.py +++ b/api.py @@ -14,6 +14,19 @@ class API: with httpx.Client(headers=self.headers) as client: 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: """Sets the user object""" return self._get(f"https://api.github.com/users/{user_name}") @@ -24,4 +37,4 @@ class API: def load_repos(self) -> dict: """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")