17
17
)
18
18
19
19
import httpx
20
+ import hishel
20
21
21
22
from .response import Response
22
23
from .utils import obj_to_jsonable
@@ -79,6 +80,7 @@ def __init__(
79
80
user_agent : Optional [str ] = None ,
80
81
follow_redirects : bool = True ,
81
82
timeout : Optional [Union [float , httpx .Timeout ]] = None ,
83
+ http_cache : bool = True ,
82
84
):
83
85
...
84
86
@@ -94,6 +96,7 @@ def __init__(
94
96
user_agent : Optional [str ] = None ,
95
97
follow_redirects : bool = True ,
96
98
timeout : Optional [Union [float , httpx .Timeout ]] = None ,
99
+ http_cache : bool = True ,
97
100
):
98
101
...
99
102
@@ -109,6 +112,7 @@ def __init__(
109
112
user_agent : Optional [str ] = None ,
110
113
follow_redirects : bool = True ,
111
114
timeout : Optional [Union [float , httpx .Timeout ]] = None ,
115
+ http_cache : bool = True ,
112
116
):
113
117
...
114
118
@@ -123,14 +127,21 @@ def __init__(
123
127
user_agent : Optional [str ] = None ,
124
128
follow_redirects : bool = True ,
125
129
timeout : Optional [Union [float , httpx .Timeout ]] = None ,
130
+ http_cache : bool = True ,
126
131
):
127
132
auth = auth or UnauthAuthStrategy () # type: ignore
128
133
self .auth : A = ( # type: ignore
129
134
TokenAuthStrategy (auth ) if isinstance (auth , str ) else auth
130
135
)
131
136
132
137
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 ,
134
145
)
135
146
136
147
self .__sync_client : ContextVar [Optional [httpx .Client ]] = ContextVar (
@@ -187,7 +198,14 @@ def _get_client_defaults(self):
187
198
188
199
# create sync client
189
200
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 )
191
209
192
210
# get or create sync client
193
211
@contextmanager
@@ -203,7 +221,14 @@ def get_sync_client(self) -> Generator[httpx.Client, None, None]:
203
221
204
222
# create async client
205
223
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 )
207
232
208
233
# get or create async client
209
234
@asynccontextmanager
0 commit comments