summaryrefslogtreecommitdiff
path: root/requests_cache/serializers
diff options
context:
space:
mode:
authorJordan Cook <jordan.cook@pioneer.com>2021-06-11 16:49:34 -0500
committerJordan Cook <jordan.cook@pioneer.com>2021-06-11 16:49:34 -0500
commit218b3260a140f8e3c207c1bb3fc28dfa8deec274 (patch)
tree088209713c15208888d4cbec26b65dda3734935c /requests_cache/serializers
parent727304595bba14dae6d765282490978f061f71de (diff)
downloadrequests-cache-218b3260a140f8e3c207c1bb3fc28dfa8deec274.tar.gz
Rename BaseSerializer methods to dumps/loads
Diffstat (limited to 'requests_cache/serializers')
-rw-r--r--requests_cache/serializers/base.py20
-rw-r--r--requests_cache/serializers/bson_serializer.py4
-rw-r--r--requests_cache/serializers/json_serializer.py4
-rw-r--r--requests_cache/serializers/pickle_serializer.py4
4 files changed, 9 insertions, 23 deletions
diff --git a/requests_cache/serializers/base.py b/requests_cache/serializers/base.py
index 4e7ab9a..ee2546e 100644
--- a/requests_cache/serializers/base.py
+++ b/requests_cache/serializers/base.py
@@ -1,4 +1,3 @@
-from abc import abstractmethod
from datetime import datetime, timedelta
from typing import Any, Callable
@@ -14,38 +13,25 @@ class BaseSerializer:
pre/post-processing with cattrs. This provides an easy starting point for alternative
serialization formats, and potential for some backend-specific optimizations.
- Subclasses must provide ``dumps`` and ``loads`` methods.
+ Subclasses must call and override ``dumps`` and ``loads`` methods.
"""
# Flag to indicate to backends that content should be stored as a binary object
is_binary = True
def __init__(self, *args, converter_factory=None, **kwargs):
- from ..backends import get_valid_kwargs
-
- # If used as a mixin and the superclass is custom serializer, pass along any valid kwargs
- kwargs = get_valid_kwargs(super().__init__, kwargs)
- super().__init__(**kwargs)
self.converter = init_converter(factory=converter_factory)
- def unstructure(self, obj: Any) -> Any:
+ def dumps(self, obj: Any) -> Any:
if not isinstance(obj, CachedResponse) or not self.converter:
return obj
return self.converter.unstructure(obj)
- def structure(self, obj: Any) -> Any:
+ def loads(self, obj: Any) -> Any:
if not isinstance(obj, dict) or not self.converter:
return obj
return self.converter.structure(obj, CachedResponse)
- @abstractmethod
- def dumps(self, response: CachedResponse):
- pass
-
- @abstractmethod
- def loads(self, obj) -> CachedResponse:
- pass
-
def init_converter(factory: Callable = None):
"""Make a converter to structure and unstructure some of the nested objects within a response,
diff --git a/requests_cache/serializers/bson_serializer.py b/requests_cache/serializers/bson_serializer.py
index 70ca967..4ac3220 100644
--- a/requests_cache/serializers/bson_serializer.py
+++ b/requests_cache/serializers/bson_serializer.py
@@ -13,7 +13,7 @@ class BSONSerializer(BaseSerializer):
super().__init__(*args, converter_factory=make_converter, **kwargs)
def dumps(self, response: CachedResponse) -> bytes:
- return bson.encode(super().unstructure(response))
+ return bson.encode(super().dumps(response))
def loads(self, obj: bytes) -> CachedResponse:
- return super().structure(bson.decode(obj))
+ return super().loads(bson.decode(obj))
diff --git a/requests_cache/serializers/json_serializer.py b/requests_cache/serializers/json_serializer.py
index 8d00ded..777f40f 100644
--- a/requests_cache/serializers/json_serializer.py
+++ b/requests_cache/serializers/json_serializer.py
@@ -19,7 +19,7 @@ class JSONSerializer(BaseSerializer):
super().__init__(*args, converter_factory=make_converter, **kwargs)
def dumps(self, response: CachedResponse) -> str:
- return json.dumps(super().unstructure(response), indent=2)
+ return json.dumps(super().dumps(response), indent=2)
def loads(self, obj: str) -> CachedResponse:
- return super().structure(json.loads(obj))
+ return super().loads(json.loads(obj))
diff --git a/requests_cache/serializers/pickle_serializer.py b/requests_cache/serializers/pickle_serializer.py
index cd07e3d..af43c20 100644
--- a/requests_cache/serializers/pickle_serializer.py
+++ b/requests_cache/serializers/pickle_serializer.py
@@ -11,10 +11,10 @@ class PickleSerializer(BaseSerializer):
"""Wrapper for pickle that pre/post-processes with cattrs"""
def dumps(self, response: CachedResponse) -> bytes:
- return pickle.dumps(super().unstructure(response))
+ return pickle.dumps(super().dumps(response))
def loads(self, obj: bytes) -> CachedResponse:
- return super().structure(pickle.loads(obj))
+ return super().loads(pickle.loads(obj))
class SafePickleSerializer(SafeSerializer, BaseSerializer):