Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Added automatic refreshing of Auth token #72

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Moved AuthTokenExpired try/except to wrap around the exception router.
quintindunn committed Nov 23, 2023
commit f6c1d5f7f4b06e5b635af5e689afe09a31d3c640
24 changes: 16 additions & 8 deletions lapsepy/journal/journal.py
Original file line number Diff line number Diff line change
@@ -51,29 +51,37 @@ 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.
:return: dict of the HTTP response.
"""

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()