Skip to content

Conversation

@NRHelmi
Copy link
Contributor

@NRHelmi NRHelmi commented Oct 31, 2022

This PR adds support to oauth token caching

Copy link
Contributor

@geokollias geokollias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Helmi! Have added some (mostly minor) comments/questions.

railib/rest.py Outdated
Comment on lines 133 to 134
except Exception:
return None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see we swallow these exceptions in these cache functions (the go sdk does sth similar as well). Is that on purpose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I do understand, if the cache is not accessible for some reason we should not block the user and instead we request a new token

else:
cache = {creds.client_id: creds.access_token}
with open(_cache_file(), 'w') as f:
f.write(json.dumps(cache, default=vars))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's default=vars needed for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is one way to make python class json serializable

railib/rest.py Outdated
if cache:
cache[creds.client_id] = creds.access_token
else:
cache = {creds.client_id: creds.access_token}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could avoid this branch by returning {} from _read_cache's exception path

def _read_token_cache(creds: ClientCredentials) -> AccessToken:
try:
cache = _read_cache()
return AccessToken(**cache[creds.client_id])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume if the cache doesn't contain that client id an exception is thrown?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep that's true and we just by-pass it, if _read_token_cache function return None we will request a new token anyway

@NRHelmi NRHelmi merged commit 918f11d into main Nov 2, 2022
@NRHelmi NRHelmi deleted the hnr-token-caching branch November 2, 2022 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants