Skip to content
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

Feature: Add caching for HTTP responses #62

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
)
34 changes: 31 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,17 @@ 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()
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved

return httpx.Client(
**self._get_client_defaults(),
transport=transport,
)
yanyongyu marked this conversation as resolved.
Show resolved Hide resolved

# get or create sync client
@contextmanager
Expand All @@ -203,7 +224,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