Closed sangaline closed 4 years ago
Note that these new init arguments and attributes broke the pylint checking. I added exceptions to the too-many-arguments
and too-many-instance-attributes
rules in BaseClient
to get tests to pass, not sure if you want to handle this in a different way.
Awesome! I'm out at AWS re:Invent right now, but I'll try to get around to reviewing and merging ASAP!
This is now live on pip as version 3.2.40
Thanks again for your contributions! 😄
This implements support for using an external storage mechanism for oauth2 tokens to prevent contention issues when using multiple clients in different processes. The proposal was written out in #72, but I made some slight changes that felt natural when writing the code. This implementation replaces
BaseClient.access_token
andBaseClient.refresh_token
with properties that defer toBaseClient.__access_token
andBaseClient.__refresh_token
when no external storage is configured. This behavior should be fully backwards compatible with how the library worked before this PR.There are two new options that you can specify when initializing the base client that will change this default behavior:
oauth2_token_getter: Optional[Callable[[Literal['access_token', 'refresh_token'], str], str]] = None
andoauth2_token_setter: Optional[Callable[[Literal['access_token', 'refresh_token'], str, str], None]] = None
. They both take the token type as the first argument and the client ID as the second argument. The setter has one additional argument that is the value of the token. These methods both default toNone
, but are used instead of deferring toBaseClient.__access_token
andBaseClient.__refresh_token
when the are provided. This allows using whichever shared storage mechanism you see fit to manage the tokens. If no tokens are retrieved from the getter method, then the properties will fall back to the internal variables. Even if theaccess_token
is expired, this allows the client to get new tokens when the external storage is expired or otherwise unavailable.Here's an example of how you can use the new functionality to store your oauth2 tokens in redis:
Closes #72