hishel 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,165 @@
1
+ from __future__ import annotations
2
+
3
+ import uuid
4
+ from typing import TYPE_CHECKING, Any, Mapping, Optional, Union, overload
5
+
6
+ import msgpack
7
+ from typing_extensions import Literal, cast
8
+
9
+ from hishel.beta._core._headers import Headers
10
+ from hishel.beta._core.models import PairMeta, Request, Response
11
+
12
+
13
+ def filter_out_hishel_metadata(data: Mapping[str, Any]) -> dict[str, Any]:
14
+ return {k: v for k, v in data.items() if not k.startswith("hishel_")}
15
+
16
+
17
+ if TYPE_CHECKING:
18
+ from hishel.beta import CompletePair, IncompletePair
19
+
20
+
21
+ @overload
22
+ def pack(
23
+ value: "CompletePair" | "IncompletePair",
24
+ /,
25
+ kind: Literal["pair"],
26
+ ) -> bytes: ...
27
+
28
+
29
+ @overload
30
+ def pack(
31
+ value: uuid.UUID,
32
+ /,
33
+ kind: Literal["entry_db_key_index"],
34
+ ) -> bytes: ...
35
+
36
+
37
+ def pack(
38
+ value: Union["CompletePair", "IncompletePair", uuid.UUID],
39
+ /,
40
+ kind: Literal["pair", "entry_db_key_index"],
41
+ ) -> bytes:
42
+ from hishel.beta import CompletePair, IncompletePair
43
+
44
+ if kind == "entry_db_key_index":
45
+ assert isinstance(value, uuid.UUID)
46
+ return value.bytes
47
+ elif kind == "pair":
48
+ assert isinstance(value, (CompletePair, IncompletePair))
49
+ cache_key_dict = {"cache_key": value.cache_key} if isinstance(value, CompletePair) else {}
50
+ return cast(
51
+ bytes,
52
+ msgpack.packb(
53
+ {
54
+ "id": value.id.bytes,
55
+ "request": {
56
+ "method": value.request.method,
57
+ "url": value.request.url,
58
+ "headers": value.request.headers._headers,
59
+ "extra": filter_out_hishel_metadata(value.request.metadata),
60
+ },
61
+ "response": (
62
+ {
63
+ "status_code": value.response.status_code,
64
+ "headers": value.response.headers._headers,
65
+ "extra": filter_out_hishel_metadata(value.response.metadata),
66
+ }
67
+ )
68
+ if isinstance(value, CompletePair)
69
+ else None,
70
+ "meta": {
71
+ "created_at": value.meta.created_at,
72
+ "deleted_at": value.meta.deleted_at,
73
+ },
74
+ **cache_key_dict,
75
+ }
76
+ ),
77
+ )
78
+ assert False, f"Unexpected kind: {kind}"
79
+
80
+
81
+ @overload
82
+ def unpack(
83
+ value: bytes,
84
+ /,
85
+ kind: Literal["pair"],
86
+ ) -> Union["CompletePair", "IncompletePair"]: ...
87
+
88
+
89
+ @overload
90
+ def unpack(
91
+ value: bytes,
92
+ /,
93
+ kind: Literal["entry_db_key_index"],
94
+ ) -> uuid.UUID: ...
95
+
96
+
97
+ @overload
98
+ def unpack(
99
+ value: Optional[bytes],
100
+ /,
101
+ kind: Literal["pair"],
102
+ ) -> Optional[Union["CompletePair", "IncompletePair"]]: ...
103
+
104
+
105
+ @overload
106
+ def unpack(
107
+ value: Optional[bytes],
108
+ /,
109
+ kind: Literal["entry_db_key_index"],
110
+ ) -> Optional[uuid.UUID]: ...
111
+
112
+
113
+ def unpack(
114
+ value: Optional[bytes],
115
+ /,
116
+ kind: Literal["pair", "entry_db_key_index"],
117
+ ) -> Union["CompletePair", "IncompletePair", uuid.UUID, None]:
118
+ from hishel.beta import CompletePair, IncompletePair
119
+
120
+ if value is None:
121
+ return None
122
+ if kind == "entry_db_key_index":
123
+ return uuid.UUID(bytes=value)
124
+ elif kind == "pair":
125
+ data = msgpack.unpackb(value)
126
+ id = uuid.UUID(bytes=data["id"])
127
+ if data.get("response"):
128
+ return CompletePair(
129
+ id=id,
130
+ request=Request(
131
+ method=data["request"]["method"],
132
+ url=data["request"]["url"],
133
+ headers=Headers(data["request"]["headers"]),
134
+ metadata=data["request"]["extra"],
135
+ stream=iter([]),
136
+ ),
137
+ response=(
138
+ Response(
139
+ status_code=data["response"]["status_code"],
140
+ headers=Headers(data["response"]["headers"]),
141
+ metadata=data["response"]["extra"],
142
+ stream=iter([]),
143
+ )
144
+ ),
145
+ meta=PairMeta(
146
+ created_at=data["meta"]["created_at"],
147
+ deleted_at=data["meta"]["deleted_at"],
148
+ ),
149
+ cache_key=data["cache_key"],
150
+ )
151
+ else:
152
+ return IncompletePair(
153
+ id=id,
154
+ request=Request(
155
+ method=data["request"]["method"],
156
+ url=data["request"]["url"],
157
+ headers=Headers(data["request"]["headers"]),
158
+ metadata=data["request"]["extra"],
159
+ stream=iter([]),
160
+ ),
161
+ meta=PairMeta(
162
+ created_at=data["meta"]["created_at"],
163
+ deleted_at=data["meta"]["deleted_at"],
164
+ ),
165
+ )