Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid multiple Auth0 calls #497

Merged
Changes from all commits
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
9 changes: 8 additions & 1 deletion lean/components/api/auth0_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def __init__(self, api_client: 'APIClient') -> None:
:param api_client: the APIClient instance to use when making requests
"""
self._api = api_client
self._cache = {}

def read(self, brokerage_id: str) -> QCAuth0Authorization:
"""Reads the authorization data for a brokerage.
Expand All @@ -35,12 +36,18 @@ def read(self, brokerage_id: str) -> QCAuth0Authorization:
:return: the authorization data for the specified brokerage
"""
try:
# First check cache
if brokerage_id in self._cache.keys():
return self._cache[brokerage_id]
payload = {
"brokerage": brokerage_id
}

data = self._api.post("live/auth0/read", payload)
return QCAuth0Authorization(**data)
# Store in cache
result = QCAuth0Authorization(**data)
self._cache[brokerage_id] = result
return result
except RequestFailedError as e:
return QCAuth0Authorization(authorization=None)

Expand Down
Loading