From 21a1aa3ab02c229328d7035dae796a62a9c18675 Mon Sep 17 00:00:00 2001 From: karpetrosyan Date: Tue, 12 Dec 2023 15:09:59 +0400 Subject: [PATCH] Use InMemoryCache --- githubkit/config.py | 3 +++ githubkit/core.py | 9 +++++++-- githubkit/github.py | 3 +++ pyproject.toml | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/githubkit/config.py b/githubkit/config.py index 66d1d3c18..9385fe3e9 100644 --- a/githubkit/config.py +++ b/githubkit/config.py @@ -12,6 +12,7 @@ class Config: user_agent: str follow_redirects: bool timeout: httpx.Timeout + http_cache: bool def dict(self) -> Dict[str, Any]: return asdict(self) @@ -65,6 +66,7 @@ def get_config( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ) -> Config: return Config( build_base_url(base_url), @@ -72,4 +74,5 @@ def get_config( build_user_agent(user_agent), follow_redirects, build_timeout(timeout), + http_cache, ) diff --git a/githubkit/core.py b/githubkit/core.py index 4ae031361..0d3981a79 100644 --- a/githubkit/core.py +++ b/githubkit/core.py @@ -80,6 +80,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... @@ -95,6 +96,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... @@ -110,6 +112,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... @@ -124,6 +127,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): auth = auth or UnauthAuthStrategy() # type: ignore self.auth: A = ( # type: ignore @@ -140,6 +144,7 @@ def __init__( self.__async_client: ContextVar[Optional[httpx.AsyncClient]] = ContextVar( "async_client", default=None ) + self._http_cache = http_cache # sync context def __enter__(self): @@ -190,7 +195,7 @@ def _get_client_defaults(self): def _create_sync_client(self) -> httpx.Client: return httpx.Client( **self._get_client_defaults(), - transport=hishel.CacheTransport(httpx.HTTPTransport()), + transport=hishel.CacheTransport(httpx.HTTPTransport(), storage=hishel.InMemoryStorage()), ) # get or create sync client @@ -209,7 +214,7 @@ def get_sync_client(self) -> Generator[httpx.Client, None, None]: def _create_async_client(self) -> httpx.AsyncClient: return httpx.AsyncClient( **self._get_client_defaults(), - transport=hishel.AsyncCacheTransport(httpx.AsyncHTTPTransport()), + transport=hishel.AsyncCacheTransport(httpx.AsyncHTTPTransport(), storage=hishel.AsyncInMemoryStorage()), ) # get or create async client diff --git a/githubkit/github.py b/githubkit/github.py index 62effc632..6ff3e4edf 100644 --- a/githubkit/github.py +++ b/githubkit/github.py @@ -84,6 +84,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... @@ -99,6 +100,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... @@ -114,6 +116,7 @@ def __init__( user_agent: Optional[str] = None, follow_redirects: bool = True, timeout: Optional[Union[float, httpx.Timeout]] = None, + http_cache: bool = True, ): ... diff --git a/pyproject.toml b/pyproject.toml index 33dfb3862..92344d59b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ include = ["githubkit/py.typed"] python = "^3.8" pydantic = ">=2.0.0, <3.0.0, !=2.5.0, !=2.5.1" httpx = ">=0.23.0, <1.0.0" -hishel = ">=0.0.19" +hishel = ">=0.0.20" typing-extensions = "^4.3.0" anyio = { version = "^3.6.1", optional = true } PyJWT = { version = "^2.4.0", extras = ["crypto"], optional = true }