diff options
| author | Jordan Cook <jordan.cook@pioneer.com> | 2021-05-10 19:23:32 -0500 |
|---|---|---|
| committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-05-26 20:55:33 -0500 |
| commit | 7c7126475d741ab32b14637343dc604dae9cd65c (patch) | |
| tree | d3bf0d3ebe1b543de9ed02a5d0766040015fcda3 /requests_cache/models | |
| parent | 2f3cfbf926e2c99919e7a5b02f4926d5dbd67f24 (diff) | |
| download | requests-cache-7c7126475d741ab32b14637343dc604dae9cd65c.tar.gz | |
Fix broken unit tests and add more coverage
Diffstat (limited to 'requests_cache/models')
| -rw-r--r-- | requests_cache/models/raw_response.py | 23 | ||||
| -rwxr-xr-x | requests_cache/models/response.py | 8 |
2 files changed, 21 insertions, 10 deletions
diff --git a/requests_cache/models/raw_response.py b/requests_cache/models/raw_response.py index fd1875f..63c6d8b 100644 --- a/requests_cache/models/raw_response.py +++ b/requests_cache/models/raw_response.py @@ -3,15 +3,14 @@ from logging import getLogger import attr from requests import Response -from requests.structures import CaseInsensitiveDict -from urllib3.response import HTTPResponse, is_fp_closed +from urllib3.response import HTTPHeaderDict, HTTPResponse, is_fp_closed logger = getLogger(__name__) -@attr.s(auto_attribs=False, auto_detect=True, init=False, kw_only=True) +@attr.s(auto_attribs=False, auto_detect=True, kw_only=True) class CachedHTTPResponse(HTTPResponse): - """A serializable dataclass that emulates :py:class:`~urllib3.response.HTTPResponse`. + """A serializable dataclass that extends/emulates :py:class:`~urllib3.response.HTTPResponse`. Supports streaming requests and generator usage. The only action this doesn't support is explicitly calling :py:meth:`.read` with @@ -19,9 +18,9 @@ class CachedHTTPResponse(HTTPResponse): """ decode_content: bool = attr.ib(default=None) - headers: CaseInsensitiveDict = attr.ib(factory=dict) + headers: HTTPHeaderDict = attr.ib(factory=dict) reason: str = attr.ib(default=None) - request_url: str = attr.ib(default=None) + request_url: str = attr.ib(default=None) # TODO: Not available in urllib <=1.21. Is this needed? status: int = attr.ib(default=0) strict: int = attr.ib(default=0) version: int = attr.ib(default=0) @@ -71,9 +70,15 @@ class CachedHTTPResponse(HTTPResponse): self._fp.close() return data - def reset(self): - """Reset raw response file pointer""" - self._fp = BytesIO(self._body) + def reset(self, body: bytes = None): + """Reset raw response file pointer, and optionally update content""" + if body is not None: + self._body = body + self._fp = BytesIO(self._body or b'') + + def set_content(self, body: bytes): + self._body = body + self.reset() def stream(self, amt=None, **kwargs): """Simplified generator over cached content that emulates diff --git a/requests_cache/models/response.py b/requests_cache/models/response.py index eddffeb..86b21b9 100755 --- a/requests_cache/models/response.py +++ b/requests_cache/models/response.py @@ -34,7 +34,8 @@ class CachedResponse(Response): saves a bit of memory and deserialization steps when those objects aren't accessed. """ - _content: bytes = attr.ib(default=b'', repr=False, converter=lambda x: x or b'') + # _content: bytes = attr.ib(default=b'', repr=False, converter=lambda x: x or b'') + _content: bytes = attr.ib(default=None) url: str = attr.ib(default=None) status_code: int = attr.ib(default=0) cookies: RequestsCookieJar = attr.ib(factory=dict) @@ -48,6 +49,11 @@ class CachedResponse(Response): request: CachedRequest = attr.ib(factory=CachedRequest) raw: CachedHTTPResponse = attr.ib(factory=CachedHTTPResponse, repr=False) + def __attrs_post_init__(self): + """Re-initialize raw response body after deserialization""" + if self.raw._body is None and self._content is not None: + self.raw.reset(self._content) + @classmethod def from_response(cls, original_response: Response, **kwargs): """Create a CachedResponse based on an original response object""" |
