MCPcopy Create free account
hub / github.com/psf/cachecontrol / dumps

Method dumps

cachecontrol/serialize.py:20–60  ·  view source on GitHub ↗
(
        self,
        request: PreparedRequest,
        response: HTTPResponse,
        body: bytes | None = None,
    )

Source from the content-addressed store, hash-verified

18 serde_version = "4"
19
20 def dumps(
21 self,
22 request: PreparedRequest,
23 response: HTTPResponse,
24 body: bytes | None = None,
25 ) -> bytes:
26 response_headers: CaseInsensitiveDict[str] = CaseInsensitiveDict(
27 response.headers
28 )
29
30 if body is None:
31 # When a body isn't passed in, we'll read the response. We
32 # also update the response with a new file handler to be
33 # sure it acts as though it was never read.
34 body = response.read(decode_content=False)
35 response._fp = io.BytesIO(body) # type: ignore[assignment]
36 response.length_remaining = len(body)
37
38 data = {
39 "response": {
40 "body": body, # Empty bytestring if body is stored separately
41 "headers": {str(k): str(v) for k, v in response.headers.items()},
42 "status": response.status,
43 "version": response.version,
44 "reason": str(response.reason),
45 "decode_content": response.decode_content,
46 }
47 }
48
49 # Construct our vary headers
50 data["vary"] = {}
51 if "vary" in response_headers:
52 varied_headers = response_headers["vary"].split(",")
53 for header in varied_headers:
54 header = str(header).strip()
55 header_value = request.headers.get(header, None)
56 if header_value is not None:
57 header_value = str(header_value)
58 data["vary"][header] = header_value
59
60 return b",".join([f"cc={self.serde_version}".encode(), self.serialize(data)])
61
62 def serialize(self, data: dict[str, Any]) -> bytes:
63 return cast(bytes, msgpack.dumps(data, use_bin_type=True))

Callers 2

_cache_setMethod · 0.45
serializeMethod · 0.45

Calls 4

serializeMethod · 0.95
readMethod · 0.80
encodeMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected