You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wanted to use this library for an application I'm making that will allow customers to authenticate, and use the Moloni API. The issue I'm facing, since I will have multiple customers using this application, is that in the code I've noticed you use TTLCache with a token key, so once one of the customers would authenticate their token will be used for everyone.
Would you be open to a solution that will allow providing a custom class (that extends MyAuth) to clients constructor, this will allow using a custom caching client, for example Redis, rather than relaying on in memory cache as well as modify any other things like per client id key?
I would be willing to provide such solution, but I would like to know if you're willing to make changes or should I fork the repository?
thanks for raising this - it's actually an overview on my side, usually the cache key should be a hash of the refresh token, not a static "token"... This library is built upon my other one, https://github.com/saleweaver/python-amazon-sp-api, where this is implemented as this already.
If you want to port it over, or create your own, you're very welcome to do so - I'll happily merge it.
Thanks for that. I've only seen it on mobile, but I think now we're recreating the cache whenever a new client gets created (unless you explicitly pass an existing cache).
I like the cache service, nice one. Maybe we should make it a singleton?
Copilot recommended sth like this, I'll have a look tomorrow as well:
To address the caching limitations while leveraging the service provided in the pull request, here's an alternative approach that ensures shared caching across multiple client instances:
Alternative Approach
Singleton Cache Service:
Use a singleton pattern for the CacheService to ensure all client instances share the same cache instance. For example:
Modify Client Initialization:
Update the client to use this singleton CacheService by default if no explicit cache service is provided:
classClient:
def__init__(self, cache_service=None):
self.cache_service=cache_serviceorCacheServiceSingleton()
# Other initialization code...
Shared Tokens:
With the singleton approach, all clients will share the same cache, ensuring tokens are reused across clients, reducing redundant retrievals.
Custom Cache Configuration:
If needed, allow customizing the cache configuration for the singleton:
This way, the cache configuration can be set globally before any client instances are created.
This approach keeps the flexibility introduced by the abstract CacheService while maintaining the benefits of a shared, top-level cache for tokens. Would you like help implementing or refining this solution further?
I've done it this way due to having client id in constructor of the CacheService so we can build unique key for each client, I wasn't sure how would you feel about adding client_id to get_key contract.
If you think adding client_id to get_key contract (which IMO wouldn't be a bad idea overall) is okay, I would be happy to change it to singleton approach.
I have prepared changes like this, I just didn't push them yet in case you have another idea about client id. The contract looks like this now:
@abstractmethoddefget_key(self, client_id: str) ->str:
"""Generate a cache."""pass
While CacheServiceSingleton looks like this:
classCacheServiceSingleton(AbstractCacheService):
_instance=None_lock=Lock()
def__new__(cls):
# Check if an instance already exists without acquiring the lockifcls._instanceisNone:
withcls._lock:
ifcls._instanceisNone:
cls._instance=super().__new__(cls)
cls._instance._cache=TTLCache(maxsize=10, ttl=3600)
returncls._instance
@saleweaver Sorry to ping you again, any update on this and another PR I submitted? I would really appreciate if you could check them since I'm in a bit of a hurry, and this is blocking me, and I would rather contribute to your project, instead of forking it. Thank you!
Activity
saleweaver commentedon Jul 17, 2025
Hi @ZejdCicak ,
thanks for raising this - it's actually an overview on my side, usually the cache key should be a hash of the refresh token, not a static "token"... This library is built upon my other one, https://github.com/saleweaver/python-amazon-sp-api, where this is implemented as this already.
If you want to port it over, or create your own, you're very welcome to do so - I'll happily merge it.
Here's the implementation in the other lib
https://github.com/saleweaver/python-amazon-sp-api/blob/master/sp_api/auth/access_token_client.py
ZejdCicak commentedon Jul 17, 2025
Thank you, I've provided a solution here. Please let me know what you think!
saleweaver commentedon Jul 17, 2025
Thanks for that. I've only seen it on mobile, but I think now we're recreating the cache whenever a new client gets created (unless you explicitly pass an existing cache).
I like the cache service, nice one. Maybe we should make it a singleton?
Copilot recommended sth like this, I'll have a look tomorrow as well:
To address the caching limitations while leveraging the service provided in the pull request, here's an alternative approach that ensures shared caching across multiple client instances:
Alternative Approach
Singleton Cache Service:
Use a singleton pattern for the
CacheServiceto ensure all client instances share the same cache instance. For example:Modify Client Initialization:
Update the client to use this singleton
CacheServiceby default if no explicit cache service is provided:Shared Tokens:
With the singleton approach, all clients will share the same cache, ensuring tokens are reused across clients, reducing redundant retrievals.
Custom Cache Configuration:
If needed, allow customizing the cache configuration for the singleton:
This way, the cache configuration can be set globally before any client instances are created.
This approach keeps the flexibility introduced by the abstract
CacheServicewhile maintaining the benefits of a shared, top-level cache for tokens. Would you like help implementing or refining this solution further?ZejdCicak commentedon Jul 17, 2025
I've done it this way due to having client id in constructor of the
CacheServiceso we can build unique key for each client, I wasn't sure how would you feel about addingclient_idtoget_keycontract.If you think adding
client_idtoget_keycontract (which IMO wouldn't be a bad idea overall) is okay, I would be happy to change it to singleton approach.ZejdCicak commentedon Jul 17, 2025
I have prepared changes like this, I just didn't push them yet in case you have another idea about client id. The contract looks like this now:
While
CacheServiceSingletonlooks like this:saleweaver commentedon Jul 18, 2025
Let's try that..
ZejdCicak commentedon Jul 18, 2025
Gotcha! It's in the PR now.
ZejdCicak commentedon Jul 21, 2025
@saleweaver Sorry to ping you again, any update on this and another PR I submitted? I would really appreciate if you could check them since I'm in a bit of a hurry, and this is blocking me, and I would rather contribute to your project, instead of forking it. Thank you!