Skip to content

Commit d005552

Browse files
karpetrosyanpre-commit-ci[bot]yanyongyu
authored
✨ Feature: Add caching for HTTP responses (#62)
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>
1 parent c785b90 commit d005552

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

githubkit/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class Config:
1212
user_agent: str
1313
follow_redirects: bool
1414
timeout: httpx.Timeout
15+
http_cache: bool
1516

1617
def dict(self) -> Dict[str, Any]:
1718
return asdict(self)
@@ -65,11 +66,13 @@ def get_config(
6566
user_agent: Optional[str] = None,
6667
follow_redirects: bool = True,
6768
timeout: Optional[Union[float, httpx.Timeout]] = None,
69+
http_cache: bool = True,
6870
) -> Config:
6971
return Config(
7072
build_base_url(base_url),
7173
build_accept(accept_format, previews),
7274
build_user_agent(user_agent),
7375
follow_redirects,
7476
build_timeout(timeout),
77+
http_cache,
7578
)

githubkit/core.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
)
1818

1919
import httpx
20+
import hishel
2021

2122
from .response import Response
2223
from .utils import obj_to_jsonable
@@ -79,6 +80,7 @@ def __init__(
7980
user_agent: Optional[str] = None,
8081
follow_redirects: bool = True,
8182
timeout: Optional[Union[float, httpx.Timeout]] = None,
83+
http_cache: bool = True,
8284
):
8385
...
8486

@@ -94,6 +96,7 @@ def __init__(
9496
user_agent: Optional[str] = None,
9597
follow_redirects: bool = True,
9698
timeout: Optional[Union[float, httpx.Timeout]] = None,
99+
http_cache: bool = True,
97100
):
98101
...
99102

@@ -109,6 +112,7 @@ def __init__(
109112
user_agent: Optional[str] = None,
110113
follow_redirects: bool = True,
111114
timeout: Optional[Union[float, httpx.Timeout]] = None,
115+
http_cache: bool = True,
112116
):
113117
...
114118

@@ -123,14 +127,21 @@ def __init__(
123127
user_agent: Optional[str] = None,
124128
follow_redirects: bool = True,
125129
timeout: Optional[Union[float, httpx.Timeout]] = None,
130+
http_cache: bool = True,
126131
):
127132
auth = auth or UnauthAuthStrategy() # type: ignore
128133
self.auth: A = ( # type: ignore
129134
TokenAuthStrategy(auth) if isinstance(auth, str) else auth
130135
)
131136

132137
self.config = config or get_config(
133-
base_url, accept_format, previews, user_agent, follow_redirects, timeout
138+
base_url,
139+
accept_format,
140+
previews,
141+
user_agent,
142+
follow_redirects,
143+
timeout,
144+
http_cache,
134145
)
135146

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

188199
# create sync client
189200
def _create_sync_client(self) -> httpx.Client:
190-
return httpx.Client(**self._get_client_defaults())
201+
if self.config.http_cache:
202+
transport = hishel.CacheTransport(
203+
httpx.HTTPTransport(), storage=hishel.InMemoryStorage()
204+
)
205+
else:
206+
transport = httpx.HTTPTransport()
207+
208+
return httpx.Client(**self._get_client_defaults(), transport=transport)
191209

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

204222
# create async client
205223
def _create_async_client(self) -> httpx.AsyncClient:
206-
return httpx.AsyncClient(**self._get_client_defaults())
224+
if self.config.http_cache:
225+
transport = hishel.AsyncCacheTransport(
226+
httpx.AsyncHTTPTransport(), storage=hishel.AsyncInMemoryStorage()
227+
)
228+
else:
229+
transport = httpx.AsyncHTTPTransport()
230+
231+
return httpx.AsyncClient(**self._get_client_defaults(), transport=transport)
207232

208233
# get or create async client
209234
@asynccontextmanager

githubkit/github.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def __init__(
8484
user_agent: Optional[str] = None,
8585
follow_redirects: bool = True,
8686
timeout: Optional[Union[float, httpx.Timeout]] = None,
87+
http_cache: bool = True,
8788
):
8889
...
8990

@@ -99,6 +100,7 @@ def __init__(
99100
user_agent: Optional[str] = None,
100101
follow_redirects: bool = True,
101102
timeout: Optional[Union[float, httpx.Timeout]] = None,
103+
http_cache: bool = True,
102104
):
103105
...
104106

@@ -114,6 +116,7 @@ def __init__(
114116
user_agent: Optional[str] = None,
115117
follow_redirects: bool = True,
116118
timeout: Optional[Union[float, httpx.Timeout]] = None,
119+
http_cache: bool = True,
117120
):
118121
...
119122

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ include = ["githubkit/py.typed"]
1515
python = "^3.8"
1616
pydantic = ">=2.0.0, <3.0.0, !=2.5.0, !=2.5.1"
1717
httpx = ">=0.23.0, <1.0.0"
18+
hishel = ">=0.0.20"
1819
typing-extensions = "^4.3.0"
1920
anyio = { version = "^3.6.1", optional = true }
2021
PyJWT = { version = "^2.4.0", extras = ["crypto"], optional = true }

0 commit comments

Comments
 (0)