Repository navigation
Feature: support hishel v2 #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,8 @@ | |
|
|
||
| from hishel import ( | ||
| AsyncBaseStorage, | ||
| BaseStorage, | ||
| CacheOptions, | ||
| CachePolicy, | ||
| SpecificationPolicy, | ||
| SyncBaseStorage, | ||
| ) | ||
|
|
||
|
|
||
|
|
@@ -47,12 +45,12 @@ async def get_async_cache_storage(self) -> AsyncBaseCache: | |
| """ | ||
| raise NotImplementedError | ||
|
|
||
| def get_hishel_policy(self) -> CachePolicy: | ||
| def get_hishel_policy(self) -> CacheOptions: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method is still called policy. We may add a new method |
||
| """Get the hishel cache policy instance""" | ||
| return SpecificationPolicy(cache_options=CacheOptions(shared=False)) | ||
| return CacheOptions(shared=False) | ||
|
|
||
| @abc.abstractmethod | ||
| def get_hishel_storage(self) -> SyncBaseStorage: | ||
| def get_hishel_storage(self) -> BaseStorage: | ||
| """Get the hishel storage instance used in sync context""" | ||
| raise NotImplementedError | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,62 +6,74 @@ | |
| from hishel import ( | ||
| AsyncBaseStorage, | ||
| AsyncSqliteStorage, | ||
| CachePolicy, | ||
| SyncBaseStorage, | ||
| SyncSqliteStorage, | ||
| BaseStorage, | ||
| CacheOptions, | ||
| SqliteStorage, | ||
| ) | ||
|
|
||
| from ._httpx import AsyncCacheTransport, SyncCacheTransport, httpx | ||
| from ._httpx import AsyncCacheTransport, CacheTransport, httpx | ||
|
|
||
|
|
||
| def _check_policy(policy: Any) -> None: | ||
| if not isinstance(policy, (bool, dict)): | ||
| raise TypeError( | ||
| "policy must be a hishel CacheOptions instance or a bool, " | ||
| f"got {type(policy).__name__}. hishel 2.0 removed CachePolicy " | ||
| "(e.g. SpecificationPolicy); pass CacheOptions(...) instead." | ||
| ) | ||
|
|
||
|
|
||
| class SyncCacheClient(httpx.Client): | ||
| def __init__(self, *args: Any, **kwargs: Any): | ||
| self.storage: SyncBaseStorage | None = kwargs.pop("storage", None) | ||
| self.policy: CachePolicy | None = kwargs.pop("policy", None) | ||
| self.storage: BaseStorage | None = kwargs.pop("storage", None) | ||
| policy = kwargs.pop("policy", True) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the arg name |
||
| _check_policy(policy) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this may be unnecessary. |
||
| self.policy: CacheOptions | bool = policy | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def _init_transport(self, *args, **kwargs) -> httpx.BaseTransport: | ||
| _transport = super()._init_transport(*args, **kwargs) | ||
| return SyncCacheTransport( | ||
| next_transport=_transport, storage=self.storage, policy=self.policy | ||
| return CacheTransport( | ||
| next_transport=_transport, storage=self.storage, rfc=self.policy | ||
| ) | ||
|
|
||
| def _init_proxy_transport(self, *args, **kwargs) -> httpx.BaseTransport: | ||
| _transport = super()._init_proxy_transport(*args, **kwargs) | ||
| return SyncCacheTransport( | ||
| next_transport=_transport, storage=self.storage, policy=self.policy | ||
| return CacheTransport( | ||
| next_transport=_transport, storage=self.storage, rfc=self.policy | ||
| ) | ||
|
|
||
|
|
||
| class AsyncCacheClient(httpx.AsyncClient): | ||
| def __init__(self, *args: Any, **kwargs: Any): | ||
| self.storage: AsyncBaseStorage | None = kwargs.pop("storage", None) | ||
| self.policy: CachePolicy | None = kwargs.pop("policy", None) | ||
| policy = kwargs.pop("policy", True) | ||
| _check_policy(policy) | ||
| self.policy: CacheOptions | bool = policy | ||
| super().__init__(*args, **kwargs) | ||
|
|
||
| def _init_transport(self, *args, **kwargs) -> httpx.AsyncBaseTransport: | ||
| _transport = super()._init_transport(*args, **kwargs) | ||
| return AsyncCacheTransport( | ||
| next_transport=_transport, storage=self.storage, policy=self.policy | ||
| next_transport=_transport, storage=self.storage, rfc=self.policy | ||
| ) | ||
|
|
||
| def _init_proxy_transport(self, *args, **kwargs) -> httpx.AsyncBaseTransport: | ||
| _transport = super()._init_proxy_transport(*args, **kwargs) | ||
| return AsyncCacheTransport( | ||
| next_transport=_transport, storage=self.storage, policy=self.policy | ||
| next_transport=_transport, storage=self.storage, rfc=self.policy | ||
| ) | ||
|
|
||
|
|
||
| class SyncMemoryStorage(SyncSqliteStorage): | ||
| class SyncMemoryStorage(SqliteStorage): | ||
| def __init__( | ||
| self, | ||
| *, | ||
| default_ttl: float | None = None, | ||
| refresh_ttl_on_access: bool | None = None, | ||
| keep_unclosed: bool = False, | ||
| ) -> None: | ||
| super().__init__( | ||
| default_ttl=default_ttl, refresh_ttl_on_access=refresh_ttl_on_access | ||
| default_ttl=default_ttl, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove comma here |
||
| ) | ||
| # NOTE: hishel close the storage when the client transport is closed. | ||
| # but for in-memory sqlite, | ||
|
|
@@ -91,11 +103,10 @@ def __init__( | |
| self, | ||
| *, | ||
| default_ttl: float | None = None, | ||
| refresh_ttl_on_access: bool | None = None, | ||
| keep_unclosed: bool = False, | ||
| ) -> None: | ||
| super().__init__( | ||
| default_ttl=default_ttl, refresh_ttl_on_access=refresh_ttl_on_access | ||
| default_ttl=default_ttl, | ||
| ) | ||
| # NOTE: hishel close the storage when the client transport is closed. | ||
| # but for in-memory sqlite, | ||
|
|
@@ -117,7 +128,7 @@ async def _ensure_connection(self) -> anysqlite.Connection: | |
| async def close(self) -> None: | ||
| if self.keep_unclosed: | ||
| # Do not close the connection, but drain all pending writes | ||
| async with self._write_lock: | ||
| async with self._init_lock: | ||
| return | ||
| return await super().close() | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems need reformat, remove the comma