Cattrs#

Utilities to break down CachedResponse objects into a dict of python builtin types using cattrs. This does the majority of the work needed for all serialization formats.

CattrStage

Base serializer class that does pre/post-processing with cattrs.

init_converter

Make a converter to structure and unstructure nested objects within a CachedResponse

make_decimal_timedelta_converter

Make a converter that uses Decimals instead of floats to represent timedelta objects

class requests_cache.serializers.cattrs.CattrStage(factory=None, decode_content=False, **kwargs)[source]#

Bases: Stage

Base serializer class that does pre/post-processing with cattrs. This can be used either on its own, or as a stage within a SerializerPipeline.

Parameters:
  • factory (Optional[Callable[..., Converter]]) – A callable that returns a cattrs converter to start from instead of a new Converter. Mainly useful for preconf converters.

  • decode_content (bool) – Save response body in human-readable format, if possible

Notes on decode_content option:

  • Response body will be decoded into a human-readable format (if possible) during serialization, and re-encoded during deserialization to reconstruct the original response.

  • Supported Content-Types are application/json and text/*. All other types will be saved as-is.

  • Decoded responses are saved in a separate _decoded_content attribute, to ensure that _content is always binary.

  • This is the default behavior for Filesystem, DynamoDB, and MongoDB backends.

dumps(value)[source]#
Parameters:

value (CachedResponse) –

Return type:

Dict

loads(value)[source]#
Parameters:

value (Dict) –

Return type:

CachedResponse

requests_cache.serializers.cattrs.init_converter(factory=None, convert_datetime=True, convert_timedelta=True)[source]#

Make a converter to structure and unstructure nested objects within a CachedResponse

Parameters:
  • factory (Optional[Callable[..., Converter]]) – An optional factory function that returns a cattrs converter

  • convert_datetime (bool) – May be set to False for pre-configured converters that already have datetime support

  • convert_timedelta (bool) –

Return type:

Converter

requests_cache.serializers.cattrs.make_decimal_timedelta_converter(**kwargs)[source]#

Make a converter that uses Decimals instead of floats to represent timedelta objects

Return type:

Converter