Skip to content

Commit

Permalink
✨ Feature: Add caching for HTTP responses (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 14, 2023
1 parent c785b90 commit d005552
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions githubkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -65,11 +66,13 @@ 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),
build_accept(accept_format, previews),
build_user_agent(user_agent),
follow_redirects,
build_timeout(timeout),
http_cache,
)
31 changes: 28 additions & 3 deletions githubkit/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)

import httpx
import hishel

from .response import Response
from .utils import obj_to_jsonable
Expand Down Expand Up @@ -79,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,
):
...

Expand All @@ -94,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,
):
...

Expand All @@ -109,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,
):
...

Expand All @@ -123,14 +127,21 @@ 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
TokenAuthStrategy(auth) if isinstance(auth, str) else auth
)

self.config = config or get_config(
base_url, accept_format, previews, user_agent, follow_redirects, timeout
base_url,
accept_format,
previews,
user_agent,
follow_redirects,
timeout,
http_cache,
)

self.__sync_client: ContextVar[Optional[httpx.Client]] = ContextVar(
Expand Down Expand Up @@ -187,7 +198,14 @@ def _get_client_defaults(self):

# create sync client
def _create_sync_client(self) -> httpx.Client:
return httpx.Client(**self._get_client_defaults())
if self.config.http_cache:
transport = hishel.CacheTransport(
httpx.HTTPTransport(), storage=hishel.InMemoryStorage()
)
else:
transport = httpx.HTTPTransport()

return httpx.Client(**self._get_client_defaults(), transport=transport)

# get or create sync client
@contextmanager
Expand All @@ -203,7 +221,14 @@ def get_sync_client(self) -> Generator[httpx.Client, None, None]:

# create async client
def _create_async_client(self) -> httpx.AsyncClient:
return httpx.AsyncClient(**self._get_client_defaults())
if self.config.http_cache:
transport = hishel.AsyncCacheTransport(
httpx.AsyncHTTPTransport(), storage=hishel.AsyncInMemoryStorage()
)
else:
transport = httpx.AsyncHTTPTransport()

return httpx.AsyncClient(**self._get_client_defaults(), transport=transport)

# get or create async client
@asynccontextmanager
Expand Down
3 changes: 3 additions & 0 deletions githubkit/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
):
...

Expand All @@ -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,
):
...

Expand All @@ -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,
):
...

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +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.20"
typing-extensions = "^4.3.0"
anyio = { version = "^3.6.1", optional = true }
PyJWT = { version = "^2.4.0", extras = ["crypto"], optional = true }
Expand Down

0 comments on commit d005552

Please sign in to comment.