diff --git a/lapsepy/__init__.py b/lapsepy/__init__.py index 8772da8..b448e4c 100644 --- a/lapsepy/__init__.py +++ b/lapsepy/__init__.py @@ -3,7 +3,7 @@ Date: 10/22/23 """ -__version__ = '1.1.1' +__version__ = '1.1.2' from .journal import Journal from .auth.refresher import refresh diff --git a/lapsepy/journal/journal.py b/lapsepy/journal/journal.py index ce248fb..d61ac02 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,22 +59,35 @@ 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) + + # Check for exceptions raised while making request, none of these are handled. 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: + # 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 diff --git a/setup.py b/setup.py index 17ee8d7..2be7ea1 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = "1.1.1" +VERSION = "1.1.2" DESCRIPTION = "A Python API wrapper for the social media app Lapse." with open("README.md", 'r') as f: