Skip to content

Commit ede5c42

Browse files
committed
bookmarkmgr: cronet: Handle cookies
This is supposed to help with some rate limiting.
1 parent 971d5a2 commit ede5c42

File tree

2 files changed

+45
-13
lines changed

2 files changed

+45
-13
lines changed

bookmarkmgr/bookmarkmgr/cronet/models.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from dataclasses import dataclass, field
22
from email.message import Message
33
from http import HTTPStatus
4+
from typing import Any
5+
from urllib.request import Request
46

57

68
@dataclass(slots=True)
@@ -16,11 +18,20 @@ def ok(self) -> bool:
1618
)
1719

1820

19-
@dataclass(slots=True)
20-
class RequestParameters:
21-
method: str
22-
url: str
23-
allow_redirects: bool = True
21+
class RequestParameters(Request):
22+
def __init__(
23+
self,
24+
*args: Any,
25+
allow_redirects: bool = True,
26+
**kwargs: Any,
27+
) -> None:
28+
super().__init__(*args, **kwargs)
29+
30+
self.allow_redirects = allow_redirects
31+
32+
@property
33+
def url(self) -> str:
34+
return self.full_url
2435

2536

2637
@dataclass(slots=True)
@@ -32,6 +43,9 @@ class Response(ResponseStatus):
3243
headers: Message = field(default_factory=Message)
3344
redirect_url: str | None = None
3445

46+
def info(self) -> Message:
47+
return self.headers
48+
3549
@property
3650
def text(self) -> str:
3751
return self.content.decode(self.charset, "replace")

bookmarkmgr/bookmarkmgr/cronet/session.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
from collections.abc import Awaitable, Callable, Iterable, Mapping
33
from contextlib import nullcontext
44
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
68
from urllib.parse import urlparse
79

810
from yarl import URL
@@ -24,6 +26,8 @@
2426
from .utils import adestroying, destroying
2527

2628
if TYPE_CHECKING:
29+
from http.client import HTTPResponse
30+
2731
from .types import Engine
2832

2933
INIT_MAX_RETRY_ATTEMPTS = 5
@@ -48,6 +52,7 @@
4852

4953
class Session:
5054
def __init__(self) -> None:
55+
self._cookie_jar = CookieJar()
5156
self._engine: Engine | None = None
5257

5358
async def __aenter__(self) -> Self:
@@ -128,6 +133,13 @@ async def request(
128133
if params is not None:
129134
url = str(URL(url).update_query(params))
130135

136+
request_params = RequestParameters(
137+
method=method,
138+
url=url,
139+
**kwargs,
140+
)
141+
self._cookie_jar.add_cookie_header(request_params)
142+
131143
async with (
132144
adestroying(
133145
lib.Cronet_UrlRequestParams_Create(),
@@ -138,19 +150,18 @@ async def request(
138150
lib.Cronet_UrlRequest_Destroy,
139151
) as request,
140152
RequestCallbackManager(
141-
RequestParameters(
142-
method=method,
143-
url=url,
144-
**kwargs,
145-
),
153+
request_params,
146154
) as callback_manager,
147155
ExecutorManager() as executor_manager,
148156
):
149157
lib.Cronet_UrlRequestParams_http_method_set(
150158
parameters,
151159
method.encode(),
152160
)
153-
for name, value in DEFAULT_HEADERS:
161+
for name, value in chain(
162+
DEFAULT_HEADERS,
163+
request_params.headers.items(),
164+
):
154165
with destroying(
155166
lib.Cronet_HttpHeader_Create(),
156167
lib.Cronet_HttpHeader_Destroy,
@@ -177,11 +188,18 @@ async def request(
177188
_raise_for_error_result(lib.Cronet_UrlRequest_Start(request))
178189

179190
try:
180-
return await callback_manager.response()
191+
response = await callback_manager.response()
181192
except:
182193
lib.Cronet_UrlRequest_Cancel(request)
183194
raise
184195

196+
self._cookie_jar.extract_cookies(
197+
cast("HTTPResponse", response),
198+
request_params,
199+
)
200+
201+
return response
202+
185203

186204
class RetrySession(Session):
187205
def __init__(

0 commit comments

Comments
 (0)