summaryrefslogtreecommitdiff
path: root/requests_cache
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-05-10 19:23:32 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-05-26 20:55:33 -0500
commit7c7126475d741ab32b14637343dc604dae9cd65c (patch)
treed3bf0d3ebe1b543de9ed02a5d0766040015fcda3 /requests_cache
parent2f3cfbf926e2c99919e7a5b02f4926d5dbd67f24 (diff)
downloadrequests-cache-7c7126475d741ab32b14637343dc604dae9cd65c.tar.gz
Fix broken unit tests and add more coverage
Diffstat (limited to 'requests_cache')
-rw-r--r--requests_cache/models/raw_response.py23
-rwxr-xr-xrequests_cache/models/response.py8
-rw-r--r--requests_cache/serializers/base.py42
-rw-r--r--requests_cache/serializers/pickle.py16
4 files changed, 58 insertions, 31 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"""
diff --git a/requests_cache/serializers/base.py b/requests_cache/serializers/base.py
index bc2b732..144f075 100644
--- a/requests_cache/serializers/base.py
+++ b/requests_cache/serializers/base.py
@@ -1,10 +1,11 @@
from abc import abstractmethod
from datetime import datetime, timedelta
-from typing import Dict
+from typing import Any
import cattr
from requests.cookies import RequestsCookieJar, cookiejar_from_dict
from requests.structures import CaseInsensitiveDict
+from urllib3.response import HTTPHeaderDict
from ..models import CachedResponse
@@ -20,25 +21,28 @@ class BaseSerializer:
def __init__(self, *args, **kwargs):
"""Make a converter to structure and unstructure some of the nested objects within a response"""
super().__init__(*args, **kwargs)
- converter = cattr.Converter()
+ try:
+ # raise AttributeError
+ converter = cattr.GenConverter(omit_if_default=True)
+ # Python 3.6 compatibility
+ except AttributeError:
+ converter = cattr.Converter()
# Convert datetimes to and from iso-formatted strings
converter.register_unstructure_hook(datetime, lambda obj: obj.isoformat() if obj else None)
- converter.register_structure_hook(
- datetime, lambda obj, cls: datetime.fromisoformat(obj) if obj else None
- )
+ converter.register_structure_hook(datetime, to_datetime)
# Convert timedeltas to and from float values in seconds
converter.register_unstructure_hook(timedelta, lambda obj: obj.total_seconds() if obj else None)
- converter.register_structure_hook(
- timedelta, lambda obj, cls: timedelta(seconds=obj) if obj else None
- )
+ converter.register_structure_hook(timedelta, to_timedelta)
# Convert dict-like objects to and from plain dicts
converter.register_unstructure_hook(RequestsCookieJar, lambda obj: dict(obj.items()))
converter.register_structure_hook(RequestsCookieJar, lambda obj, cls: cookiejar_from_dict(obj))
converter.register_unstructure_hook(CaseInsensitiveDict, dict)
converter.register_structure_hook(CaseInsensitiveDict, lambda obj, cls: CaseInsensitiveDict(obj))
+ converter.register_unstructure_hook(HTTPHeaderDict, dict)
+ converter.register_structure_hook(HTTPHeaderDict, lambda obj, cls: HTTPHeaderDict(obj))
# Not sure yet if this will be needed
# converter.register_unstructure_hook(PreparedRequest, CachedRequest.from_request)
@@ -49,10 +53,14 @@ class BaseSerializer:
self.converter = converter
- def unstructure(self, response: CachedResponse) -> Dict:
- return self.converter.unstructure(response)
+ def unstructure(self, obj: Any) -> Any:
+ if not isinstance(obj, CachedResponse):
+ return obj
+ return self.converter.unstructure(obj)
- def structure(self, obj: Dict) -> CachedResponse:
+ def structure(self, obj: Any) -> Any:
+ if not isinstance(obj, dict):
+ return obj
return self.converter.structure(obj, CachedResponse)
@abstractmethod
@@ -62,3 +70,15 @@ class BaseSerializer:
@abstractmethod
def loads(self, obj) -> CachedResponse:
pass
+
+
+def to_datetime(obj, cls) -> datetime:
+ if isinstance(obj, str):
+ obj = datetime.fromisoformat(obj)
+ return obj
+
+
+def to_timedelta(obj, cls) -> timedelta:
+ if isinstance(obj, (int, float)):
+ obj = timedelta(seconds=obj)
+ return obj
diff --git a/requests_cache/serializers/pickle.py b/requests_cache/serializers/pickle.py
index c1a5726..86a4e59 100644
--- a/requests_cache/serializers/pickle.py
+++ b/requests_cache/serializers/pickle.py
@@ -16,19 +16,15 @@ class PickleSerializer(BaseSerializer):
return super().structure(pickle.loads(obj))
-class SafePickleSerializer(BaseSerializer, SafeSerializer):
+class SafePickleSerializer(SafeSerializer, BaseSerializer):
"""Wrapper for itsdangerous + pickle that pre/post-processes with cattrs"""
def __init__(self, *args, **kwargs):
+ # super().__init__(*args, **kwargs, serializer=pickle)
super().__init__(*args, **kwargs, serializer=PickleSerializer())
- def dumps(self, response: CachedResponse) -> bytes:
- x = super().unstructure(response)
- # breakpoint()
- return SafeSerializer.dumps(self, x)
+ # def dumps(self, response: CachedResponse) -> bytes:
+ # return SafeSerializer.dumps(self, super().unstructure(response))
- # TODO: Something weird is going on here
- def loads(self, obj: bytes) -> CachedResponse:
- return SafeSerializer.loads(self, obj)
- # breakpoint()
- return super().structure(SafeSerializer.loads(self, obj))
+ # def loads(self, obj: bytes) -> CachedResponse:
+ # return super().structure(SafeSerializer.loads(self, obj))