1
1
from __future__ import annotations
2
2
3
3
import os
4
- import typing
5
- import warnings
6
4
import sys
7
5
import time
8
- from http .cookiejar import CookieJar
6
+ import typing
7
+ import warnings
9
8
from collections import OrderedDict
10
9
from datetime import timedelta
10
+ from http .cookiejar import CookieJar
11
11
from urllib .parse import urljoin , urlparse
12
+
12
13
from .status_codes import codes
13
14
14
15
if typing .TYPE_CHECKING :
22
23
from urllib3 .contrib .webextensions ._async import load_extension
23
24
else : # Defensive: tested in separate/isolated CI
24
25
from urllib3_future import ConnectionInfo # type: ignore[assignment]
25
- from urllib3_future .contrib .resolver ._async import AsyncBaseResolver # type: ignore[assignment]
26
- from urllib3_future .contrib .webextensions ._async import load_extension # type: ignore[assignment]
26
+ from urllib3_future .contrib .resolver ._async import ( # type: ignore[assignment]
27
+ AsyncBaseResolver ,
28
+ )
29
+ from urllib3_future .contrib .webextensions ._async import ( # type: ignore[assignment]
30
+ load_extension ,
31
+ )
27
32
28
33
from ._constant import (
34
+ DEFAULT_POOLSIZE ,
35
+ DEFAULT_RETRIES ,
29
36
READ_DEFAULT_TIMEOUT ,
30
37
WRITE_DEFAULT_TIMEOUT ,
31
- DEFAULT_RETRIES ,
32
- DEFAULT_POOLSIZE ,
33
38
)
34
39
from ._typing import (
40
+ AsyncBodyType ,
41
+ AsyncHookType ,
42
+ AsyncHttpAuthenticationType ,
43
+ AsyncResolverType ,
35
44
BodyType ,
45
+ CacheLayerAltSvcType ,
36
46
CookiesType ,
37
47
HeadersType ,
38
48
HookType ,
42
52
MultiPartFilesType ,
43
53
ProxyType ,
44
54
QueryParameterType ,
55
+ RetryType ,
45
56
TimeoutType ,
46
57
TLSClientCertType ,
47
58
TLSVerifyType ,
48
- AsyncResolverType ,
49
- CacheLayerAltSvcType ,
50
- RetryType ,
51
- AsyncHookType ,
52
- AsyncBodyType ,
53
- AsyncHttpAuthenticationType ,
59
+ )
60
+ from .adapters import AsyncBaseAdapter , AsyncHTTPAdapter
61
+ from .cookies import (
62
+ RequestsCookieJar ,
63
+ cookiejar_from_dict ,
64
+ extract_cookies_to_jar ,
65
+ merge_cookies ,
54
66
)
55
67
from .exceptions import (
56
68
ChunkedEncodingError ,
57
69
ContentDecodingError ,
58
- TooManyRedirects ,
59
70
InvalidSchema ,
71
+ TooManyRedirects ,
60
72
)
61
73
from .hooks import async_dispatch_hook , default_hooks
62
74
from .models import (
75
+ DEFAULT_REDIRECT_LIMIT ,
76
+ AsyncResponse ,
63
77
PreparedRequest ,
64
78
Request ,
65
79
Response ,
66
- DEFAULT_REDIRECT_LIMIT ,
67
80
TransferProgress ,
68
- AsyncResponse ,
69
81
)
70
82
from .sessions import Session
83
+ from .structures import AsyncQuicSharedCache
71
84
from .utils import (
85
+ _deepcopy_ci ,
86
+ _swap_context ,
72
87
create_async_resolver ,
73
88
default_headers ,
89
+ is_ocsp_capable ,
90
+ parse_scheme ,
91
+ requote_uri ,
74
92
resolve_proxies ,
75
93
rewind_body ,
76
- requote_uri ,
77
- _swap_context ,
78
- _deepcopy_ci ,
79
- parse_scheme ,
80
- is_ocsp_capable ,
81
- )
82
- from .cookies import (
83
- RequestsCookieJar ,
84
- cookiejar_from_dict ,
85
- extract_cookies_to_jar ,
86
- merge_cookies ,
87
94
)
88
- from .structures import AsyncQuicSharedCache
89
- from .adapters import AsyncBaseAdapter , AsyncHTTPAdapter
90
95
91
96
# Preferred clock, based on which one is more accurate on a given system.
92
97
if sys .platform == "win32" :
@@ -163,9 +168,7 @@ def __init__(
163
168
self .proxies : ProxyType = {}
164
169
165
170
#: Event-handling hooks.
166
- self .hooks : AsyncHookType [PreparedRequest | Response | AsyncResponse ] = (
167
- default_hooks () # type: ignore[assignment]
168
- )
171
+ self .hooks : AsyncHookType [PreparedRequest | Response | AsyncResponse ] = default_hooks () # type: ignore[assignment]
169
172
170
173
#: Dictionary of querystring data to attach to each
171
174
#: :class:`Request <Request>`. The dictionary values may be lists for
@@ -232,18 +235,12 @@ def __init__(
232
235
#: session. By default it is a
233
236
#: :class:`RequestsCookieJar <requests.cookies.RequestsCookieJar>`, but
234
237
#: may be any other ``cookielib.CookieJar`` compatible object.
235
- self .cookies : RequestsCookieJar | CookieJar = cookiejar_from_dict (
236
- {}, thread_free = True
237
- )
238
+ self .cookies : RequestsCookieJar | CookieJar = cookiejar_from_dict ({}, thread_free = True )
238
239
239
240
#: A simple dict that allows us to persist which server support QUIC
240
241
#: It is simply forwarded to urllib3.future that handle the caching logic.
241
242
#: Can be any mutable mapping.
242
- self .quic_cache_layer = (
243
- quic_cache_layer
244
- if quic_cache_layer is not None
245
- else AsyncQuicSharedCache (max_size = 12_288 )
246
- )
243
+ self .quic_cache_layer = quic_cache_layer if quic_cache_layer is not None else AsyncQuicSharedCache (max_size = 12_288 )
247
244
248
245
#: Don't try to manipulate this object.
249
246
#: It cannot be pickled and accessing this object may cause
@@ -291,9 +288,7 @@ def __init__(
291
288
)
292
289
293
290
def __enter__ (self ) -> typing .NoReturn :
294
- raise SyntaxError (
295
- 'You probably meant "async with". Did you forget to prepend the "async" keyword?'
296
- )
291
+ raise SyntaxError ('You probably meant "async with". Did you forget to prepend the "async" keyword?' )
297
292
298
293
async def __aenter__ (self ) -> AsyncSession :
299
294
return self
@@ -367,10 +362,7 @@ def get_adapter(self, url: str) -> AsyncBaseAdapter: # type: ignore[override]
367
362
try :
368
363
extension = load_extension (scheme , implementation = implementation )
369
364
for prefix , adapter in self .adapters .items ():
370
- if (
371
- scheme in extension .supported_schemes ()
372
- and extension .scheme_to_http_scheme (scheme ) == parse_scheme (prefix )
373
- ):
365
+ if scheme in extension .supported_schemes () and extension .scheme_to_http_scheme (scheme ) == parse_scheme (prefix ):
374
366
return adapter
375
367
except ImportError :
376
368
pass
@@ -383,9 +375,7 @@ def get_adapter(self, url: str) -> AsyncBaseAdapter: # type: ignore[override]
383
375
additional_hint = ""
384
376
385
377
# Nothing matches :-/
386
- raise InvalidSchema (
387
- f"No connection adapters were found for { url !r} { additional_hint } "
388
- )
378
+ raise InvalidSchema (f"No connection adapters were found for { url !r} { additional_hint } " )
389
379
390
380
async def send ( # type: ignore[override]
391
381
self , request : PreparedRequest , ** kwargs : typing .Any
@@ -427,21 +417,16 @@ async def on_post_connection(conn_info: ConnectionInfo) -> None:
427
417
nonlocal ptr_request , request , kwargs
428
418
ptr_request .conn_info = conn_info
429
419
430
- if (
431
- ptr_request .url
432
- and ptr_request .url .startswith ("https://" )
433
- and kwargs ["verify" ]
434
- and is_ocsp_capable (conn_info )
435
- ):
436
- strict_ocsp_enabled : bool = (
437
- os .environ .get ("NIQUESTS_STRICT_OCSP" , "0" ) != "0"
438
- )
420
+ if ptr_request .url and ptr_request .url .startswith ("https://" ) and kwargs ["verify" ] and is_ocsp_capable (conn_info ):
421
+ strict_ocsp_enabled : bool = os .environ .get ("NIQUESTS_STRICT_OCSP" , "0" ) != "0"
439
422
440
423
try :
441
424
from .extensions ._async_ocsp import (
442
- verify as ocsp_verify ,
443
425
InMemoryRevocationStatus ,
444
426
)
427
+ from .extensions ._async_ocsp import (
428
+ verify as ocsp_verify ,
429
+ )
445
430
except ImportError :
446
431
pass
447
432
else :
@@ -608,9 +593,7 @@ async def _redirect_method_ref(x, y):
608
593
# Resolve redirects if allowed.
609
594
if allow_redirects :
610
595
# Redirect resolving generator.
611
- gen = self .resolve_redirects (
612
- r , request , yield_requests_trail = True , ** kwargs
613
- )
596
+ gen = self .resolve_redirects (r , request , yield_requests_trail = True , ** kwargs )
614
597
history = []
615
598
616
599
async for resp_or_req in gen :
@@ -693,9 +676,7 @@ async def resolve_redirects( # type: ignore[override]
693
676
await resp .raw .read (decode_content = False )
694
677
695
678
if len (resp .history ) >= self .max_redirects :
696
- raise TooManyRedirects (
697
- f"Exceeded { self .max_redirects } redirects." , response = resp
698
- )
679
+ raise TooManyRedirects (f"Exceeded { self .max_redirects } redirects." , response = resp )
699
680
700
681
# Release the connection back into the pool.
701
682
if isinstance (resp , AsyncResponse ):
@@ -715,9 +696,7 @@ async def resolve_redirects( # type: ignore[override]
715
696
parsed = urlparse (url )
716
697
if parsed .fragment == "" and previous_fragment :
717
698
parsed = parsed ._replace (
718
- fragment = previous_fragment
719
- if isinstance (previous_fragment , str )
720
- else previous_fragment .decode ("utf-8" )
699
+ fragment = previous_fragment if isinstance (previous_fragment , str ) else previous_fragment .decode ("utf-8" )
721
700
)
722
701
elif parsed .fragment :
723
702
previous_fragment = parsed .fragment
@@ -728,9 +707,7 @@ async def resolve_redirects( # type: ignore[override]
728
707
# Compliant with RFC3986, we percent encode the url.
729
708
if not parsed .netloc :
730
709
url = urljoin (resp .url , requote_uri (url )) # type: ignore[type-var]
731
- assert isinstance (
732
- url , str
733
- ), f"urljoin produced { type (url )} instead of str"
710
+ assert isinstance (url , str ), f"urljoin produced { type (url )} instead of str"
734
711
else :
735
712
url = requote_uri (url )
736
713
@@ -910,9 +887,7 @@ async def request( # type: ignore[override]
910
887
911
888
proxies = proxies or {}
912
889
913
- settings = self .merge_environment_settings (
914
- prep .url , proxies , stream , verify , cert
915
- )
890
+ settings = self .merge_environment_settings (prep .url , proxies , stream , verify , cert )
916
891
917
892
# Send the request.
918
893
send_kwargs = {
0 commit comments