client.py
This file
Path
httpcore/client.py
743 lines · 20.7 KiB · Python
Last changed by
query_params -> paramsTom Christiebefore adoptionbb70377
History of this fileTake it
Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 1why, one step back1import asyncioMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 2–4why, one step back2import typing3from types import TracebackType4Auth (#65)Tom Christiebefore adoption5afa7dd
line 5why, one step back5from .auth import HTTPBasicAuthMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 6–15why, one step back6from .config import (7 DEFAULT_MAX_REDIRECTS,8 DEFAULT_POOL_LIMITS,9 DEFAULT_SSL_CONFIG,10 DEFAULT_TIMEOUT_CONFIG,11 PoolLimits,12 SSLConfig,13 TimeoutConfig,14)15from .dispatch.connection_pool import ConnectionPoolMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 16why, one step back16from .exceptions import RedirectBodyUnavailable, RedirectLoop, TooManyRedirectsMerge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 17why, one step back17from .interfaces import ConcurrencyBackend, DispatcherMerge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 18–19why, one step back18from .models import (19 URL,Auth (#65)Tom Christiebefore adoption5afa7dd
line 20why, one step back20 AuthTypes,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 21–22why, one step back21 Cookies,22 CookieTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 23why, one step back23 Headers,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 24–26why, one step back24 HeaderTypes,25 QueryParamTypes,26 Request,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 27why, one step back27 RequestData,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 28why, one step back28 Response,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 29why, one step back29 SyncResponse,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 30–31why, one step back30 URLTypes,31)Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 32why, one step back32from .status_codes import codesMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 33–34why, one step back3334Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 35why, one step back35class AsyncClient:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 36–37why, one step back36 def __init__(37 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 38why, one step back38 auth: AuthTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 39why, one step back39 cookies: CookieTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 40–41why, one step back40 ssl: SSLConfig = DEFAULT_SSL_CONFIG,41 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,Truckin' onTom Christiebefore adoption6b60d95
line 42why, one step back42 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 43why, one step back43 max_redirects: int = DEFAULT_MAX_REDIRECTS,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 44why, one step back44 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 45why, one step back45 backend: ConcurrencyBackend = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 46why, one step back46 ):Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 47why, one step back47 if dispatch is None:Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
lines 48–50why, one step back48 dispatch = ConnectionPool(49 ssl=ssl, timeout=timeout, pool_limits=pool_limits, backend=backend50 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 51why, one step back51Auth (#65)Tom Christiebefore adoption5afa7dd
line 52why, one step back52 self.auth = authCookie support (#73)Tom Christiebefore adoptionfeae0a8
line 53why, one step back53 self.cookies = Cookies(cookies)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 54–55why, one step back54 self.max_redirects = max_redirects55 self.dispatch = dispatchMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 56–58why, one step back5657 async def get(58 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 59why, one step back59 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 60why, one step back60 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 61why, one step back61 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 62why, one step back62 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 63why, one step back63 cookies: CookieTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 64why, one step back64 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 65why, one step back65 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 66–68why, one step back66 allow_redirects: bool = True,67 ssl: SSLConfig = None,68 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 69–70why, one step back69 ) -> Response:70 return await self.request(Finessing interfaceTom Christiebefore adoptiond7619a9
lines 71–72why, one step back71 "GET",72 url,query_params -> paramsTom Christiebefore adoptionbb70377
line 73why, one step back73 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 74why, one step back74 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 75why, one step back75 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 76why, one step back76 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 77why, one step back77 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 78–80why, one step back78 allow_redirects=allow_redirects,79 ssl=ssl,80 timeout=timeout,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 81–82why, one step back81 )82Finessing interfaceTom Christiebefore adoptiond7619a9
lines 83–86why, one step back83 async def options(84 self,85 url: URLTypes,86 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 87why, one step back87 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 88why, one step back88 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 89why, one step back89 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 90why, one step back90 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 91why, one step back91 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 92–98why, one step back92 allow_redirects: bool = True,93 ssl: SSLConfig = None,94 timeout: TimeoutConfig = None,95 ) -> Response:96 return await self.request(97 "OPTIONS",98 url,query_params -> paramsTom Christiebefore adoptionbb70377
line 99why, one step back99 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 100why, one step back100 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 101why, one step back101 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 102why, one step back102 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 103why, one step back103 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 104–112why, one step back104 allow_redirects=allow_redirects,105 ssl=ssl,106 timeout=timeout,107 )108109 async def head(110 self,111 url: URLTypes,112 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 113why, one step back113 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 114why, one step back114 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 115why, one step back115 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 116why, one step back116 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 117why, one step back117 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 118–124why, one step back118 allow_redirects: bool = False, # Note: Differs to usual default.119 ssl: SSLConfig = None,120 timeout: TimeoutConfig = None,121 ) -> Response:122 return await self.request(123 "HEAD",124 url,query_params -> paramsTom Christiebefore adoptionbb70377
line 125why, one step back125 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 126why, one step back126 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 127why, one step back127 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 128why, one step back128 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 129why, one step back129 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 130–134why, one step back130 allow_redirects=allow_redirects,131 ssl=ssl,132 timeout=timeout,133 )134Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 135–136why, one step back135 async def post(136 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 137why, one step back137 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 138why, one step back138 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 139why, one step back139 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 140why, one step back140 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 141why, one step back141 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 142why, one step back142 cookies: CookieTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 143why, one step back143 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 144why, one step back144 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 145–147why, one step back145 allow_redirects: bool = True,146 ssl: SSLConfig = None,147 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 148–151why, one step back148 ) -> Response:149 return await self.request(150 "POST",151 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 152why, one step back152 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 153why, one step back153 params=params,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 154why, one step back154 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 155why, one step back155 cookies=cookies,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 156why, one step back156 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 157why, one step back157 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
line 158why, one step back158 allow_redirects=allow_redirects,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 159–162why, one step back159 ssl=ssl,160 timeout=timeout,161 )162Finessing interfaceTom Christiebefore adoptiond7619a9
lines 163–166why, one step back163 async def put(164 self,165 url: URLTypes,166 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 167why, one step back167 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 168why, one step back168 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 169why, one step back169 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 170why, one step back170 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 171why, one step back171 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 172why, one step back172 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 173–179why, one step back173 allow_redirects: bool = True,174 ssl: SSLConfig = None,175 timeout: TimeoutConfig = None,176 ) -> Response:177 return await self.request(178 "PUT",179 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 180why, one step back180 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 181why, one step back181 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 182why, one step back182 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 183why, one step back183 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 184why, one step back184 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 185why, one step back185 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 186–194why, one step back186 allow_redirects=allow_redirects,187 ssl=ssl,188 timeout=timeout,189 )190191 async def patch(192 self,193 url: URLTypes,194 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 195why, one step back195 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 196why, one step back196 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 197why, one step back197 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 198why, one step back198 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 199why, one step back199 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 200why, one step back200 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 201–207why, one step back201 allow_redirects: bool = True,202 ssl: SSLConfig = None,203 timeout: TimeoutConfig = None,204 ) -> Response:205 return await self.request(206 "PATCH",207 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 208why, one step back208 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 209why, one step back209 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 210why, one step back210 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 211why, one step back211 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 212why, one step back212 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 213why, one step back213 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 214–222why, one step back214 allow_redirects=allow_redirects,215 ssl=ssl,216 timeout=timeout,217 )218219 async def delete(220 self,221 url: URLTypes,222 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 223why, one step back223 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 224why, one step back224 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 225why, one step back225 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 226why, one step back226 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 227why, one step back227 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 228why, one step back228 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 229–235why, one step back229 allow_redirects: bool = True,230 ssl: SSLConfig = None,231 timeout: TimeoutConfig = None,232 ) -> Response:233 return await self.request(234 "DELETE",235 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 236why, one step back236 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 237why, one step back237 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 238why, one step back238 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 239why, one step back239 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 240why, one step back240 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 241why, one step back241 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 242–246why, one step back242 allow_redirects=allow_redirects,243 ssl=ssl,244 timeout=timeout,245 )246Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 247–252why, one step back247 async def request(248 self,249 method: str,250 url: URLTypes,251 *,252 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 253why, one step back253 params: QueryParamTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 254why, one step back254 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 255why, one step back255 cookies: CookieTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 256why, one step back256 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 257why, one step back257 auth: AuthTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 258–262why, one step back258 allow_redirects: bool = True,259 ssl: SSLConfig = None,260 timeout: TimeoutConfig = None,261 ) -> Response:262 request = Request(Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 263–265why, one step back263 method,264 url,265 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 266why, one step back266 params=params,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 267–268why, one step back267 headers=headers,268 cookies=self.merge_cookies(cookies),Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 269–273why, one step back269 )270 self.prepare_request(request)271 response = await self.send(272 request,273 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 274why, one step back274 auth=auth,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 275–280why, one step back275 allow_redirects=allow_redirects,276 ssl=ssl,277 timeout=timeout,278 )279 return response280Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 281why, one step back281 def prepare_request(self, request: Request) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 282why, one step back282 request.prepare()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 283why, one step back283query_params -> paramsTom Christiebefore adoptionbb70377
lines 284–286why, one step back284 def merge_cookies(285 self, cookies: CookieTypes = None286 ) -> typing.Optional[CookieTypes]:Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 287–292why, one step back287 if cookies or self.cookies:288 merged_cookies = Cookies(self.cookies)289 merged_cookies.update(cookies)290 return merged_cookies291 return cookies292Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 293–297why, one step back293 async def send(294 self,295 request: Request,296 *,297 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 298why, one step back298 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 299–300why, one step back299 ssl: SSLConfig = None,300 timeout: TimeoutConfig = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 301why, one step back301 allow_redirects: bool = True,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 302–331why, one step back302 ) -> Response:303 if auth is None:304 auth = self.auth305306 url = request.url307 if auth is None and (url.username or url.password):308 auth = HTTPBasicAuth(username=url.username, password=url.password)309310 if auth is not None:311 if isinstance(auth, tuple):312 auth = HTTPBasicAuth(username=auth[0], password=auth[1])313 request = auth(request)314315 response = await self.send_handling_redirects(316 request,317 stream=stream,318 ssl=ssl,319 timeout=timeout,320 allow_redirects=allow_redirects,321 )322 return response323324 async def send_handling_redirects(325 self,326 request: Request,327 *,328 stream: bool = False,329 ssl: SSLConfig = None,330 timeout: TimeoutConfig = None,331 allow_redirects: bool = True,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 332why, one step back332 history: typing.List[Response] = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 333why, one step back333 ) -> Response:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 334–335why, one step back334 if history is None:335 history = []Finessing interfaceTom Christiebefore adoptiond7619a9
line 336why, one step back336Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 337–343why, one step back337 while True:338 # We perform these checks here, so that calls to `response.next()`339 # will raise redirect errors if appropriate.340 if len(history) > self.max_redirects:341 raise TooManyRedirects()342 if request.url in [response.url for response in history]:343 raise RedirectLoop()Finessing interfaceTom Christiebefore adoptiond7619a9
line 344why, one step back344Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 345–348why, one step back345 response = await self.dispatch.send(346 request, stream=stream, ssl=ssl, timeout=timeout347 )348 response.history = list(history)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 349why, one step back349 self.cookies.extract_cookies(response)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 350–352why, one step back350 history = [response] + history351 if not response.is_redirect:352 breakMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 353why, one step back353Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 354–360why, one step back354 if allow_redirects:355 request = self.build_redirect_request(request, response)356 else:357358 async def send_next() -> Response:359 nonlocal request, response, ssl, allow_redirects, timeout, history360 request = self.build_redirect_request(request, response)Auth (#65)Tom Christiebefore adoption5afa7dd
line 361why, one step back361 response = await self.send_handling_redirects(Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 362–380why, one step back362 request,363 stream=stream,364 allow_redirects=allow_redirects,365 ssl=ssl,366 timeout=timeout,367 history=history,368 )369 return response370371 response.next = send_next # type: ignore372 break373374 return response375376 def build_redirect_request(self, request: Request, response: Response) -> Request:377 method = self.redirect_method(request, response)378 url = self.redirect_url(request, response)379 headers = self.redirect_headers(request, url)380 content = self.redirect_content(request, method)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 381why, one step back381 cookies = self.merge_cookies(request.cookies)query_params -> paramsTom Christiebefore adoptionbb70377
lines 382–384why, one step back382 return Request(383 method=method, url=url, headers=headers, data=content, cookies=cookies384 )Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 385–393why, one step back385386 def redirect_method(self, request: Request, response: Response) -> str:387 """388 When being redirected we may want to change the method of the request389 based on certain specs or browser behavior.390 """391 method = request.method392393 # https://tools.ietf.org/html/rfc7231#section-6.4.4Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 394why, one step back394 if response.status_code == codes.SEE_OTHER and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 395–398why, one step back395 method = "GET"396397 # Do what the browsers do, despite standards...398 # Turn 302s into GETs.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 399why, one step back399 if response.status_code == codes.FOUND and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 400–403why, one step back400 method = "GET"401402 # If a POST is responded to with a 301, turn it into a GET.403 # This bizarre behaviour is explained in 'requests' issue 1704.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 404why, one step back404 if response.status_code == codes.MOVED_PERMANENTLY and method == "POST":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 405–447why, one step back405 method = "GET"406407 return method408409 def redirect_url(self, request: Request, response: Response) -> URL:410 """411 Return the URL for the redirect to follow.412 """413 location = response.headers["Location"]414415 url = URL(location, allow_relative=True)416417 # Facilitate relative 'Location' headers, as allowed by RFC 7231.418 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')419 if url.is_relative_url:420 url = url.resolve_with(request.url)421422 # Attach previous fragment if needed (RFC 7231 7.1.2)423 if request.url.fragment and not url.fragment:424 url = url.copy_with(fragment=request.url.fragment)425426 return url427428 def redirect_headers(self, request: Request, url: URL) -> Headers:429 """430 Strip Authorization headers when responses are redirected away from431 the origin.432 """433 headers = Headers(request.headers)434 if url.origin != request.url.origin:435 del headers["Authorization"]436 return headers437438 def redirect_content(self, request: Request, method: str) -> bytes:439 """440 Return the body that should be used for the redirect request.441 """442 if method != request.method and method == "GET":443 return b""444 if request.is_streaming:445 raise RedirectBodyUnavailable()446 return request.content447Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 448why, one step back448 async def close(self) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 449why, one step back449 await self.dispatch.close()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 450why, one step back450Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 451why, one step back451 async def __aenter__(self) -> "AsyncClient":Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 452–460why, one step back452 return self453454 async def __aexit__(455 self,456 exc_type: typing.Type[BaseException] = None,457 exc_value: BaseException = None,458 traceback: TracebackType = None,459 ) -> None:460 await self.close()Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 461–465why, one step back461462463class Client:464 def __init__(465 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 466why, one step back466 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 467–471why, one step back467 ssl: SSLConfig = DEFAULT_SSL_CONFIG,468 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,469 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,470 max_redirects: int = DEFAULT_MAX_REDIRECTS,471 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 472why, one step back472 backend: ConcurrencyBackend = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 473–474why, one step back473 ) -> None:474 self._client = AsyncClient(Auth (#65)Tom Christiebefore adoption5afa7dd
line 475why, one step back475 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 476–480why, one step back476 ssl=ssl,477 timeout=timeout,478 pool_limits=pool_limits,479 max_redirects=max_redirects,480 dispatch=dispatch,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 481why, one step back481 backend=backend,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 482–484why, one step back482 )483 self._loop = asyncio.new_event_loop()484Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 485–488why, one step back485 @property486 def cookies(self) -> Cookies:487 return self._client.cookies488Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 489–494why, one step back489 def request(490 self,491 method: str,492 url: URLTypes,493 *,494 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 495why, one step back495 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 496why, one step back496 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 497why, one step back497 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 498why, one step back498 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 499why, one step back499 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 500–504why, one step back500 allow_redirects: bool = True,501 ssl: SSLConfig = None,502 timeout: TimeoutConfig = None,503 ) -> SyncResponse:504 request = Request(Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 505–507why, one step back505 method,506 url,507 data=data,query_params -> paramsTom Christiebefore adoptionbb70377
line 508why, one step back508 params=params,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 509–510why, one step back509 headers=headers,510 cookies=self._client.merge_cookies(cookies),Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 511–515why, one step back511 )512 self.prepare_request(request)513 response = self.send(514 request,515 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 516why, one step back516 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 517–526why, one step back517 allow_redirects=allow_redirects,518 ssl=ssl,519 timeout=timeout,520 )521 return response522523 def get(524 self,525 url: URLTypes,526 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 527why, one step back527 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 528why, one step back528 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 529why, one step back529 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 530why, one step back530 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 531why, one step back531 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 532–539why, one step back532 allow_redirects: bool = True,533 ssl: SSLConfig = None,534 timeout: TimeoutConfig = None,535 ) -> SyncResponse:536 return self.request(537 "GET",538 url,539 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 540why, one step back540 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 541why, one step back541 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 542why, one step back542 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 543–551why, one step back543 allow_redirects=allow_redirects,544 ssl=ssl,545 timeout=timeout,546 )547548 def options(549 self,550 url: URLTypes,551 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 552why, one step back552 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 553why, one step back553 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 554why, one step back554 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 555why, one step back555 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 556why, one step back556 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 557–564why, one step back557 allow_redirects: bool = True,558 ssl: SSLConfig = None,559 timeout: TimeoutConfig = None,560 ) -> SyncResponse:561 return self.request(562 "OPTIONS",563 url,564 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 565why, one step back565 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 566why, one step back566 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 567why, one step back567 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 568–576why, one step back568 allow_redirects=allow_redirects,569 ssl=ssl,570 timeout=timeout,571 )572573 def head(574 self,575 url: URLTypes,576 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 577why, one step back577 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 578why, one step back578 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 579why, one step back579 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 580why, one step back580 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 581why, one step back581 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 582–589why, one step back582 allow_redirects: bool = False, # Note: Differs to usual default.583 ssl: SSLConfig = None,584 timeout: TimeoutConfig = None,585 ) -> SyncResponse:586 return self.request(587 "HEAD",588 url,589 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 590why, one step back590 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 591why, one step back591 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 592why, one step back592 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 593–602why, one step back593 allow_redirects=allow_redirects,594 ssl=ssl,595 timeout=timeout,596 )597598 def post(599 self,600 url: URLTypes,601 *,602 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 603why, one step back603 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 604why, one step back604 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 605why, one step back605 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 606why, one step back606 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 607why, one step back607 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 608–616why, one step back608 allow_redirects: bool = True,609 ssl: SSLConfig = None,610 timeout: TimeoutConfig = None,611 ) -> SyncResponse:612 return self.request(613 "POST",614 url,615 data=data,616 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 617why, one step back617 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 618why, one step back618 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 619why, one step back619 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 620–629why, one step back620 allow_redirects=allow_redirects,621 ssl=ssl,622 timeout=timeout,623 )624625 def put(626 self,627 url: URLTypes,628 *,629 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 630why, one step back630 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 631why, one step back631 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 632why, one step back632 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 633why, one step back633 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 634why, one step back634 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 635–643why, one step back635 allow_redirects: bool = True,636 ssl: SSLConfig = None,637 timeout: TimeoutConfig = None,638 ) -> SyncResponse:639 return self.request(640 "PUT",641 url,642 data=data,643 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 644why, one step back644 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 645why, one step back645 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 646why, one step back646 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 647–656why, one step back647 allow_redirects=allow_redirects,648 ssl=ssl,649 timeout=timeout,650 )651652 def patch(653 self,654 url: URLTypes,655 *,656 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 657why, one step back657 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 658why, one step back658 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 659why, one step back659 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 660why, one step back660 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 661why, one step back661 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 662–670why, one step back662 allow_redirects: bool = True,663 ssl: SSLConfig = None,664 timeout: TimeoutConfig = None,665 ) -> SyncResponse:666 return self.request(667 "PATCH",668 url,669 data=data,670 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 671why, one step back671 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 672why, one step back672 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 673why, one step back673 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 674–683why, one step back674 allow_redirects=allow_redirects,675 ssl=ssl,676 timeout=timeout,677 )678679 def delete(680 self,681 url: URLTypes,682 *,683 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 684why, one step back684 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 685why, one step back685 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 686why, one step back686 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 687why, one step back687 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 688why, one step back688 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 689–697why, one step back689 allow_redirects: bool = True,690 ssl: SSLConfig = None,691 timeout: TimeoutConfig = None,692 ) -> SyncResponse:693 return self.request(694 "DELETE",695 url,696 data=data,697 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 698why, one step back698 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 699why, one step back699 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 700why, one step back700 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 701–713why, one step back701 allow_redirects=allow_redirects,702 ssl=ssl,703 timeout=timeout,704 )705706 def prepare_request(self, request: Request) -> None:707 self._client.prepare_request(request)708709 def send(710 self,711 request: Request,712 *,713 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 714why, one step back714 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 715–722why, one step back715 allow_redirects: bool = True,716 ssl: SSLConfig = None,717 timeout: TimeoutConfig = None,718 ) -> SyncResponse:719 response = self._loop.run_until_complete(720 self._client.send(721 request,722 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 723why, one step back723 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 724–743why, one step back724 allow_redirects=allow_redirects,725 ssl=ssl,726 timeout=timeout,727 )728 )729 return SyncResponse(response, self._loop)730731 def close(self) -> None:732 self._loop.run_until_complete(self._client.close())733734 def __enter__(self) -> "Client":735 return self736737 def __exit__(738 self,739 exc_type: typing.Type[BaseException] = None,740 exc_value: BaseException = None,741 traceback: TracebackType = None,742 ) -> None:743 self.close()