requests-cache / aiohttp-client-cache

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

AttributeError: 'CachedResponse' object has no attribute 'content_type' #117

Closed AndreyMZ closed 2 years ago

AndreyMZ commented 2 years ago

The problem

CachedResponse object has no property content_type.

Expected behavior

CachedResponse object has property content_type like ClientResponse does.

Steps to reproduce the behavior

Something like:

from aiohttp_client_cache import CachedSession

async with CachedSession() as session:
    async with session.get("https://example.com/") as response:
        content_type = response.content_type

Environment

simon-liebehenschel commented 2 years ago

Since you did not provide a reproducible code sample, I am trying to understand what is your use case. Are you trying to get a CachedResponse from a backend manually?

I mean that in the code that you shared

from aiohttp_client_cache import CachedSession

async with CachedSession() as session:
    async with session.get("https://example.com/") as response:
        content_type = response.content_type  # this will work

a ClientResponse is returned by the context manager and you can access a content_type.

So I am trying to get into details how your code may look so you access directly CachedResponse somehow.

JWCook commented 2 years ago

I'm able to reproduce it. CachedSession without a backend argument uses a non-persistent memory backend by default (since all the "real" backends require optional dependencies), so creating a new one for each request will always return the original response.

This will return a cached response and fail:

async with CachedSession() as session:
    async with session.get("https://example.com/") as response:
        content_type = response.content_type
    async with session.get("https://example.com/") as response:
        content_type = response.content_type

It looks like CachedResponse is missing the properties from HeadersMixin, including content_type. I'll fix that.

JWCook commented 2 years ago

Fixed in main. Thanks for the bug report!