Make make_cache_key pluggable
Make esipy.utils.make_cache_key pluggable in EsiClient.
The implementation right now uses all of the headers as the cache key, including the Authorization Bearer Token. Since tokens only last about 20 minutes, any authenticated request isn't able to be found in the cache after a token refresh, making the cache a little useless for responses that are cached for over 20 minutes.
Ideally the default would see if security was provided and use an identifier from security in place of the Authorization header so that changes to the actual access token across refreshes is maintained.
So far I've monkey patched it to implement the behavior I want, but being able to supply a make_cache_key kwarg to EsiClient.__init__ would be ideal.
def make_cache_key(request: Request):
url = request.url
# I've been using sorted tuples instead of frozenset
# since I need to turn the key into a string for redis, etc.
path = tuple(sorted(request._p["path"].items()))
query = tuple(sorted(request._p["query"]))
# I store my token on the request when I add the Authorization header,
# but changing the args of make_cache_key to take the `security` object would work too
token = getattr(request, "token", None)
if token is None:
headers = tuple(sorted(request._p["header"].items()))
key = (url, headers, path, query)
else:
headers = tuple(
sorted(
[
(h, v)
for h, v in request._p["header"].items()
if h != "Authorization"
]
)
)
# in my case the token.id is a good identifier, but security.token_identifier would work too
key = (url, headers, path, query, token.id)
return key