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

How to combine with https://github.com/inyutin/aiohttp_retry #16

Closed
foooooooooooooooobar opened this issue Feb 20, 2021 · 3 comments
Closed
Labels
question Question about the project

Comments

@foooooooooooooooobar
Copy link

Hey! Thanks for the library, I was wondering if it's possible to use the retry and cache libraries together?

@JWCook
Copy link
Member

JWCook commented Feb 20, 2021

Good question! Yes, there would be a few different ways to combine these. This library subclasses ClientSession, and it looks like aiohttp_retry wraps a ClientSession object. So, the quick and ugly way would be to monkey-patch RetryClient's inner ClientSession:

import aiohttp_retry
import aiohttp_client_cache

aiohttp_retry.ClientSession = aiohttp_client_cache.CachedSession
client = aiohttp_retry.RetryClient(...)

Or replace it after initialization:

client = aiohttp_retry.RetryClient(...)
client._client = CachedSession(...)

That should give you both caching and retry features, but I haven't tested it yet.

@JWCook
Copy link
Member

JWCook commented Feb 20, 2021

A better and more pythonic solution, I believe, would be to refactor both CachedSession and RetryClient to be mixin classes, instead of a subclass and wrapper class, respectively. Then you could do something like this:

class CustomSession(RetryMixin, CacheMixin, ClientSession):
    """Session class with retry + caching features"""

That might be worthwhile, because there are other features I'd like to be able to combine with aiohttp_client_cache without making them tightly coupled.

@JWCook
Copy link
Member

JWCook commented Mar 1, 2021

Fyi, aiohttp_client_cache.CacheMixin has been added for this purpose.

Closing this issue for now; let me know if the above answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Question about the project
Projects
None yet
Development

No branches or pull requests

2 participants