From f6c1d5f7f4b06e5b635af5e689afe09a31d3c640 Mon Sep 17 00:00:00 2001 From: Quintin Dunn <93884113+quintindunn@users.noreply.github.com> Date: Thu, 23 Nov 2023 15:56:03 -0500 Subject: [PATCH 1/3] Moved AuthTokenExpired try/except to wrap around the exception router. --- lapsepy/journal/journal.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/lapsepy/journal/journal.py b/lapsepy/journal/journal.py index 03dc26c..884953e 100644 --- a/lapsepy/journal/journal.py +++ b/lapsepy/journal/journal.py @@ -51,7 +51,7 @@ def __init__(self, authorization: str, refresher): } self.refresher = refresher - def _sync_journal_call(self, query: dict) -> dict: + def _sync_journal_call(self, query: dict, reauth=True) -> dict: """ Makes an API call to "https://sync-service.production.journal-api.lapse.app/graphql" with an arbitrary query. :param query: The query to send to the API. @@ -59,21 +59,29 @@ def _sync_journal_call(self, query: dict) -> dict: """ logger.debug(f"Making request to {self.request_url}") - try: - request = requests.post(self.request_url, headers=self.base_headers, json=query) - except AuthTokenExpired: - self.refresher() - logger.debug("Auth token expired, retrying.") - return self._sync_journal_call(query=query) + request = requests.post(self.request_url, headers=self.base_headers, json=query) + try: request.raise_for_status() except requests.exceptions.HTTPError: raise requests.exceptions.HTTPError(request.text) + # Check for errors in response errors = request.json().get("errors", []) if len(errors) > 0: + # There is an error, route it and raise the appropriate error. logger.error(f"Got error from request to {self.request_url} with query {query}.") - raise sync_journal_exception_router(error=errors[0]) + + try: + raise sync_journal_exception_router(error=errors[0]) + except AuthTokenExpired: + # If the error is related to the AuthToken being expired, retry once. + if reauth: + self.refresher() + logger.debug("Auth token expired, retrying.") + return self._sync_journal_call(query=query, reauth=False) + else: + raise sync_journal_exception_router(error=errors[0]) return request.json() From 0c4c1f4d12d65899c84d416b9287c8fd19026619 Mon Sep 17 00:00:00 2001 From: Quintin Dunn <93884113+quintindunn@users.noreply.github.com> Date: Thu, 23 Nov 2023 15:58:37 -0500 Subject: [PATCH 2/3] Refactored Journal._sync_journal_call --- lapsepy/journal/journal.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lapsepy/journal/journal.py b/lapsepy/journal/journal.py index 884953e..555c5e3 100644 --- a/lapsepy/journal/journal.py +++ b/lapsepy/journal/journal.py @@ -61,6 +61,7 @@ def _sync_journal_call(self, query: dict, reauth=True) -> dict: logger.debug(f"Making request to {self.request_url}") request = requests.post(self.request_url, headers=self.base_headers, json=query) + # Check for exceptions raised while making request, none of these are handled. try: request.raise_for_status() except requests.exceptions.HTTPError: @@ -68,6 +69,7 @@ def _sync_journal_call(self, query: dict, reauth=True) -> dict: # Check for errors in response errors = request.json().get("errors", []) + if len(errors) > 0: # There is an error, route it and raise the appropriate error. logger.error(f"Got error from request to {self.request_url} with query {query}.") @@ -78,11 +80,14 @@ def _sync_journal_call(self, query: dict, reauth=True) -> dict: # If the error is related to the AuthToken being expired, retry once. if reauth: self.refresher() + logger.debug("Auth token expired, retrying.") return self._sync_journal_call(query=query, reauth=False) else: + # Already retried it once, raise a real exception that won't be automatically caught. raise sync_journal_exception_router(error=errors[0]) + # Return the data from the API call. return request.json() @staticmethod From 5d00e2cb86bbb4cb09334d99afcde4ab47fe45d1 Mon Sep 17 00:00:00 2001 From: Quintin Dunn <93884113+quintindunn@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:26:03 -0500 Subject: [PATCH 3/3] Bumped version to 1.1.2 --- lapsepy/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lapsepy/__init__.py b/lapsepy/__init__.py index 4d950ac..b448e4c 100644 --- a/lapsepy/__init__.py +++ b/lapsepy/__init__.py @@ -3,7 +3,7 @@ Date: 10/22/23 """ -__version__ = '1.0.3' +__version__ = '1.1.2' from .journal import Journal from .auth.refresher import refresh diff --git a/setup.py b/setup.py index 80f53c6..2be7ea1 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = "1.0.3" +VERSION = "1.1.2" DESCRIPTION = "A Python API wrapper for the social media app Lapse." with open("README.md", 'r') as f: