2
2
from collections .abc import Awaitable , Callable , Iterable , Mapping
3
3
from contextlib import nullcontext
4
4
from http import HTTPStatus
5
- from typing import Any , Self , TYPE_CHECKING
5
+ from http .cookiejar import CookieJar
6
+ from itertools import chain
7
+ from typing import Any , cast , Self , TYPE_CHECKING
6
8
from urllib .parse import urlparse
7
9
8
10
from yarl import URL
24
26
from .utils import adestroying , destroying
25
27
26
28
if TYPE_CHECKING :
29
+ from http .client import HTTPResponse
30
+
27
31
from .types import Engine
28
32
29
33
INIT_MAX_RETRY_ATTEMPTS = 5
48
52
49
53
class Session :
50
54
def __init__ (self ) -> None :
55
+ self ._cookie_jar = CookieJar ()
51
56
self ._engine : Engine | None = None
52
57
53
58
async def __aenter__ (self ) -> Self :
@@ -128,6 +133,13 @@ async def request(
128
133
if params is not None :
129
134
url = str (URL (url ).update_query (params ))
130
135
136
+ request_params = RequestParameters (
137
+ method = method ,
138
+ url = url ,
139
+ ** kwargs ,
140
+ )
141
+ self ._cookie_jar .add_cookie_header (request_params )
142
+
131
143
async with (
132
144
adestroying (
133
145
lib .Cronet_UrlRequestParams_Create (),
@@ -138,19 +150,18 @@ async def request(
138
150
lib .Cronet_UrlRequest_Destroy ,
139
151
) as request ,
140
152
RequestCallbackManager (
141
- RequestParameters (
142
- method = method ,
143
- url = url ,
144
- ** kwargs ,
145
- ),
153
+ request_params ,
146
154
) as callback_manager ,
147
155
ExecutorManager () as executor_manager ,
148
156
):
149
157
lib .Cronet_UrlRequestParams_http_method_set (
150
158
parameters ,
151
159
method .encode (),
152
160
)
153
- for name , value in DEFAULT_HEADERS :
161
+ for name , value in chain (
162
+ DEFAULT_HEADERS ,
163
+ request_params .headers .items (),
164
+ ):
154
165
with destroying (
155
166
lib .Cronet_HttpHeader_Create (),
156
167
lib .Cronet_HttpHeader_Destroy ,
@@ -177,11 +188,18 @@ async def request(
177
188
_raise_for_error_result (lib .Cronet_UrlRequest_Start (request ))
178
189
179
190
try :
180
- return await callback_manager .response ()
191
+ response = await callback_manager .response ()
181
192
except :
182
193
lib .Cronet_UrlRequest_Cancel (request )
183
194
raise
184
195
196
+ self ._cookie_jar .extract_cookies (
197
+ cast ("HTTPResponse" , response ),
198
+ request_params ,
199
+ )
200
+
201
+ return response
202
+
185
203
186
204
class RetrySession (Session ):
187
205
def __init__ (
0 commit comments