hishel 0.1.4__py3-none-any.whl → 1.0.0b1__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.
Files changed (56) hide show
  1. hishel/__init__.py +59 -52
  2. hishel/_async_cache.py +213 -0
  3. hishel/_async_httpx.py +236 -0
  4. hishel/_core/_headers.py +646 -0
  5. hishel/{beta/_core → _core}/_spec.py +270 -136
  6. hishel/_core/_storages/_async_base.py +71 -0
  7. hishel/_core/_storages/_async_sqlite.py +420 -0
  8. hishel/_core/_storages/_packing.py +144 -0
  9. hishel/_core/_storages/_sync_base.py +71 -0
  10. hishel/_core/_storages/_sync_sqlite.py +420 -0
  11. hishel/{beta/_core → _core}/models.py +100 -37
  12. hishel/_policies.py +49 -0
  13. hishel/_sync_cache.py +213 -0
  14. hishel/_sync_httpx.py +236 -0
  15. hishel/_utils.py +37 -366
  16. hishel/asgi.py +400 -0
  17. hishel/fastapi.py +263 -0
  18. hishel/httpx.py +12 -0
  19. hishel/{beta/requests.py → requests.py} +41 -30
  20. hishel-1.0.0b1.dist-info/METADATA +509 -0
  21. hishel-1.0.0b1.dist-info/RECORD +24 -0
  22. hishel/_async/__init__.py +0 -5
  23. hishel/_async/_client.py +0 -30
  24. hishel/_async/_mock.py +0 -43
  25. hishel/_async/_pool.py +0 -201
  26. hishel/_async/_storages.py +0 -768
  27. hishel/_async/_transports.py +0 -282
  28. hishel/_controller.py +0 -581
  29. hishel/_exceptions.py +0 -10
  30. hishel/_files.py +0 -54
  31. hishel/_headers.py +0 -215
  32. hishel/_lfu_cache.py +0 -71
  33. hishel/_lmdb_types_.pyi +0 -53
  34. hishel/_s3.py +0 -122
  35. hishel/_serializers.py +0 -329
  36. hishel/_sync/__init__.py +0 -5
  37. hishel/_sync/_client.py +0 -30
  38. hishel/_sync/_mock.py +0 -43
  39. hishel/_sync/_pool.py +0 -201
  40. hishel/_sync/_storages.py +0 -768
  41. hishel/_sync/_transports.py +0 -282
  42. hishel/_synchronization.py +0 -37
  43. hishel/beta/__init__.py +0 -59
  44. hishel/beta/_async_cache.py +0 -167
  45. hishel/beta/_core/__init__.py +0 -0
  46. hishel/beta/_core/_async/_storages/_sqlite.py +0 -411
  47. hishel/beta/_core/_base/_storages/_base.py +0 -260
  48. hishel/beta/_core/_base/_storages/_packing.py +0 -165
  49. hishel/beta/_core/_headers.py +0 -301
  50. hishel/beta/_core/_sync/_storages/_sqlite.py +0 -411
  51. hishel/beta/_sync_cache.py +0 -167
  52. hishel/beta/httpx.py +0 -317
  53. hishel-0.1.4.dist-info/METADATA +0 -404
  54. hishel-0.1.4.dist-info/RECORD +0 -41
  55. {hishel-0.1.4.dist-info → hishel-1.0.0b1.dist-info}/WHEEL +0 -0
  56. {hishel-0.1.4.dist-info → hishel-1.0.0b1.dist-info}/licenses/LICENSE +0 -0
hishel/beta/httpx.py DELETED
@@ -1,317 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import ssl
4
- import typing as t
5
- from typing import AsyncIterator, Iterable, Iterator, Union, overload
6
-
7
- import httpx
8
-
9
- from hishel.beta import Headers, Request, Response
10
- from hishel.beta._async_cache import AsyncCacheProxy
11
- from hishel.beta._core._base._storages._base import AsyncBaseStorage, SyncBaseStorage
12
- from hishel.beta._core._spec import (
13
- CacheOptions,
14
- )
15
- from hishel.beta._core.models import AnyIterable
16
- from hishel.beta._sync_cache import SyncCacheProxy
17
-
18
- SOCKET_OPTION = t.Union[
19
- t.Tuple[int, int, int],
20
- t.Tuple[int, int, t.Union[bytes, bytearray]],
21
- t.Tuple[int, int, None, int],
22
- ]
23
-
24
-
25
- class IteratorStream(httpx.SyncByteStream, httpx.AsyncByteStream):
26
- def __init__(self, iterator: Iterator[bytes] | AsyncIterator[bytes]) -> None:
27
- self.iterator = iterator
28
-
29
- def __iter__(self) -> Iterator[bytes]:
30
- assert isinstance(self.iterator, (Iterator))
31
- yield from self.iterator
32
-
33
- async def __aiter__(self) -> AsyncIterator[bytes]:
34
- assert isinstance(self.iterator, (AsyncIterator))
35
- async for chunk in self.iterator:
36
- yield chunk
37
-
38
-
39
- @overload
40
- def internal_to_httpx(
41
- value: Request,
42
- ) -> httpx.Request: ...
43
- @overload
44
- def internal_to_httpx(
45
- value: Response,
46
- ) -> httpx.Response: ...
47
- def internal_to_httpx(
48
- value: Union[Request, Response],
49
- ) -> Union[httpx.Request, httpx.Response]:
50
- """
51
- Convert internal Request/Response to httpx.Request/httpx.Response.
52
- """
53
- if isinstance(value, Request):
54
- return httpx.Request(
55
- method=value.method,
56
- url=value.url,
57
- headers=value.headers,
58
- stream=IteratorStream(value.stream),
59
- extensions=value.metadata,
60
- )
61
- elif isinstance(value, Response):
62
- return httpx.Response(
63
- status_code=value.status_code,
64
- headers=value.headers,
65
- stream=IteratorStream(value.stream),
66
- extensions=value.metadata,
67
- )
68
-
69
-
70
- @overload
71
- def httpx_to_internal(
72
- value: httpx.Request,
73
- ) -> Request: ...
74
- @overload
75
- def httpx_to_internal(
76
- value: httpx.Response,
77
- ) -> Response: ...
78
- def httpx_to_internal(
79
- value: Union[httpx.Request, httpx.Response],
80
- ) -> Union[Request, Response]:
81
- """
82
- Convert httpx.Request/httpx.Response to internal Request/Response.
83
- """
84
- stream: Union[Iterator[bytes], AsyncIterator[bytes]]
85
- try:
86
- stream = AnyIterable(value.content)
87
- except (httpx.RequestNotRead, httpx.ResponseNotRead):
88
- if isinstance(value, httpx.Response):
89
- stream = value.iter_raw() if isinstance(value.stream, Iterable) else value.aiter_raw()
90
- else:
91
- stream = value.stream # type: ignore
92
- if isinstance(value, httpx.Request):
93
- return Request(
94
- method=value.method,
95
- url=str(value.url),
96
- headers=Headers({key: value for key, value in value.headers.items()}),
97
- stream=stream,
98
- metadata={
99
- "hishel_refresh_ttl_on_access": value.extensions.get("hishel_refresh_ttl_on_access"),
100
- "hishel_ttl": value.extensions.get("hishel_ttl"),
101
- "hishel_spec_ignore": value.extensions.get("hishel_spec_ignore"),
102
- },
103
- )
104
- elif isinstance(value, httpx.Response):
105
- return Response(
106
- status_code=value.status_code,
107
- headers=Headers({key: value for key, value in value.headers.items()}),
108
- stream=stream,
109
- metadata={},
110
- )
111
-
112
-
113
- class SyncCacheTransport(httpx.BaseTransport):
114
- def __init__(
115
- self,
116
- next_transport: httpx.BaseTransport,
117
- storage: SyncBaseStorage | None = None,
118
- cache_options: CacheOptions | None = None,
119
- ignore_specification: bool = False,
120
- ) -> None:
121
- self.next_transport = next_transport
122
- self._cache_proxy: SyncCacheProxy = SyncCacheProxy(
123
- send_request=self.sync_send_request,
124
- storage=storage,
125
- cache_options=cache_options,
126
- ignore_specification=ignore_specification,
127
- )
128
-
129
- def handle_request(
130
- self,
131
- request: httpx.Request,
132
- ) -> httpx.Response:
133
- internal_request = httpx_to_internal(request)
134
- internal_response = self._cache_proxy.handle_request(internal_request)
135
- response = internal_to_httpx(internal_response)
136
- return response
137
-
138
- def close(self) -> None:
139
- self.next_transport.close()
140
- super().close()
141
-
142
- def sync_send_request(self, request: Request) -> Response:
143
- httpx_request = internal_to_httpx(request)
144
- httpx_response = self.next_transport.handle_request(httpx_request)
145
- return httpx_to_internal(httpx_response)
146
-
147
-
148
- class SyncCacheClient(httpx.Client):
149
- @overload
150
- def __init__(
151
- self,
152
- *,
153
- storage: SyncBaseStorage | None = None,
154
- cache_options: CacheOptions | None = None,
155
- **kwargs: t.Any,
156
- ) -> None: ...
157
- @overload
158
- def __init__(
159
- self,
160
- *args: t.Any,
161
- **kwargs: t.Any,
162
- ) -> None: ...
163
- def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
164
- self.storage: SyncBaseStorage | None = kwargs.pop("storage", None)
165
- self.cache_options: CacheOptions | None = kwargs.pop("cache_options", None)
166
- super().__init__(*args, **kwargs)
167
-
168
- def _init_transport(
169
- self,
170
- verify: ssl.SSLContext | str | bool = True,
171
- cert: t.Union[str, t.Tuple[str, str], t.Tuple[str, str, str], None] = None,
172
- trust_env: bool = True,
173
- http1: bool = True,
174
- http2: bool = False,
175
- limits: httpx.Limits = httpx.Limits(max_connections=100, max_keepalive_connections=20),
176
- transport: httpx.BaseTransport | None = None,
177
- **kwargs: t.Any,
178
- ) -> httpx.BaseTransport:
179
- if transport is not None:
180
- return transport
181
-
182
- return SyncCacheTransport(
183
- next_transport=httpx.HTTPTransport(
184
- verify=verify,
185
- cert=cert,
186
- trust_env=trust_env,
187
- http1=http1,
188
- http2=http2,
189
- limits=limits,
190
- ),
191
- storage=self.storage,
192
- cache_options=self.cache_options,
193
- ignore_specification=False,
194
- )
195
-
196
- def _init_proxy_transport(
197
- self,
198
- proxy: httpx.Proxy,
199
- verify: ssl.SSLContext | str | bool = True,
200
- cert: t.Union[str, t.Tuple[str, str], t.Tuple[str, str, str], None] = None,
201
- trust_env: bool = True,
202
- http1: bool = True,
203
- http2: bool = False,
204
- limits: httpx.Limits = httpx.Limits(max_connections=100, max_keepalive_connections=20),
205
- **kwargs: t.Any,
206
- ) -> httpx.BaseTransport:
207
- return SyncCacheTransport(
208
- next_transport=httpx.HTTPTransport(
209
- verify=verify,
210
- cert=cert,
211
- trust_env=trust_env,
212
- http1=http1,
213
- http2=http2,
214
- limits=limits,
215
- proxy=proxy,
216
- ),
217
- storage=self.storage,
218
- cache_options=self.cache_options,
219
- ignore_specification=False,
220
- )
221
-
222
-
223
- class AsyncCacheTransport(httpx.AsyncBaseTransport):
224
- def __init__(
225
- self,
226
- next_transport: httpx.AsyncBaseTransport,
227
- storage: AsyncBaseStorage | None = None,
228
- cache_options: CacheOptions | None = None,
229
- ignore_specification: bool = False,
230
- ) -> None:
231
- self.next_transport = next_transport
232
- self._cache_proxy: AsyncCacheProxy = AsyncCacheProxy(
233
- send_request=self.async_send_request,
234
- storage=storage,
235
- cache_options=cache_options,
236
- ignore_specification=ignore_specification,
237
- )
238
-
239
- async def handle_async_request(
240
- self,
241
- request: httpx.Request,
242
- ) -> httpx.Response:
243
- internal_request = httpx_to_internal(request)
244
- internal_response = await self._cache_proxy.handle_request(internal_request)
245
- response = internal_to_httpx(internal_response)
246
- return response
247
-
248
- async def aclose(self) -> None:
249
- await self.next_transport.aclose()
250
- await super().aclose()
251
-
252
- async def async_send_request(self, request: Request) -> Response:
253
- httpx_request = internal_to_httpx(request)
254
- httpx_response = await self.next_transport.handle_async_request(httpx_request)
255
- return httpx_to_internal(httpx_response)
256
-
257
-
258
- class AsyncCacheClient(httpx.AsyncClient):
259
- def __init__(self, *args: t.Any, **kwargs: t.Any) -> None:
260
- self.storage: AsyncBaseStorage | None = kwargs.pop("storage", None)
261
- self.cache_options: CacheOptions | None = kwargs.pop("cache_options", None)
262
- self.ignore_specification: bool = kwargs.pop("ignore_specification", False)
263
- super().__init__(*args, **kwargs)
264
-
265
- def _init_transport(
266
- self,
267
- verify: ssl.SSLContext | str | bool = True,
268
- cert: t.Union[str, t.Tuple[str, str], t.Tuple[str, str, str], None] = None,
269
- trust_env: bool = True,
270
- http1: bool = True,
271
- http2: bool = False,
272
- limits: httpx.Limits = httpx.Limits(max_connections=100, max_keepalive_connections=20),
273
- transport: httpx.AsyncBaseTransport | None = None,
274
- **kwargs: t.Any,
275
- ) -> httpx.AsyncBaseTransport:
276
- if transport is not None:
277
- return transport
278
-
279
- return AsyncCacheTransport(
280
- next_transport=httpx.AsyncHTTPTransport(
281
- verify=verify,
282
- cert=cert,
283
- trust_env=trust_env,
284
- http1=http1,
285
- http2=http2,
286
- limits=limits,
287
- ),
288
- storage=self.storage,
289
- cache_options=self.cache_options,
290
- ignore_specification=False,
291
- )
292
-
293
- def _init_proxy_transport(
294
- self,
295
- proxy: httpx.Proxy,
296
- verify: ssl.SSLContext | str | bool = True,
297
- cert: t.Union[str, t.Tuple[str, str], t.Tuple[str, str, str], None] = None,
298
- trust_env: bool = True,
299
- http1: bool = True,
300
- http2: bool = False,
301
- limits: httpx.Limits = httpx.Limits(max_connections=100, max_keepalive_connections=20),
302
- **kwargs: t.Any,
303
- ) -> httpx.AsyncBaseTransport:
304
- return AsyncCacheTransport(
305
- next_transport=httpx.AsyncHTTPTransport(
306
- verify=verify,
307
- cert=cert,
308
- trust_env=trust_env,
309
- http1=http1,
310
- http2=http2,
311
- limits=limits,
312
- proxy=proxy,
313
- ),
314
- storage=self.storage,
315
- cache_options=self.cache_options,
316
- ignore_specification=self.ignore_specification,
317
- )