requests-cache / aiohttp-client-cache

An async persistent cache for aiohttp requests
MIT License
122 stars 22 forks source link

Feature request: Add support for async filtering functions on cache backends #121

Closed iliastsa closed 2 years ago

iliastsa commented 2 years ago

Feature description

Expand current support for cache backend filter functions to allow async filtering methods.

Use case

The current implementation only allows synchronous functions as filter functions on the cache backend. This has the limitation that any filtering decision can only make use of synchronous properties/methods of the aiohttp.ClientResponse object.

This is quite limiting, as any actual data of the response needs to be awaited before accessing it. For concrete example, consider that an API response returns a JSON object that contains a content and a success field:

response = {
    "content": "...",
    "success": True
}

It is really useful to be able to only cache successful (aka response.success == True) responses, and discard the rest. Ideally, one could implement a filter that looks like the following:

async def filter(resp: aiohttp.ClientResponse) -> bool:
    json_base = await resp.json()
    return json_base['success']

Unfortunately, this is not currently supported as the filtering function is not awaited by the internal implementation

JWCook commented 2 years ago

Good points. I'll get this added soon. I think we can support both sync and async filter functions by inspecting whether filter_fn is a coroutine.