client.py
This file
Path
httpcore/client.py
739 lines · 20.8 KiB · Python
Last changed by
Cookie support (#73)Tom Christiebefore adoptionfeae0a8
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 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 61why, one step back61 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 73why, one step back73 query_params=query_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 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 87why, one step back87 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 99why, one step back99 query_params=query_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 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 113why, one step back113 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 125why, one step back125 query_params=query_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"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 140why, one step back140 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 153why, one step back153 query_params=query_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"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 168why, one step back168 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 181why, one step back181 query_params=query_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"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 196why, one step back196 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 209why, one step back209 query_params=query_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"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 224why, one step back224 query_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,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 237why, one step back237 query_params=query_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–254why, one step back247 async def request(248 self,249 method: str,250 url: URLTypes,251 *,252 data: RequestData = b"",253 query_params: QueryParamTypes = None,254 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–268why, one step back263 method,264 url,265 data=data,266 query_params=query_params,267 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 back283Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 284–290why, one step back284 def merge_cookies(self, cookies: CookieTypes = None) -> typing.Optional[CookieTypes]:285 if cookies or self.cookies:286 merged_cookies = Cookies(self.cookies)287 merged_cookies.update(cookies)288 return merged_cookies289 return cookies290Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 291–295why, one step back291 async def send(292 self,293 request: Request,294 *,295 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 296why, one step back296 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 297–298why, one step back297 ssl: SSLConfig = None,298 timeout: TimeoutConfig = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 299why, one step back299 allow_redirects: bool = True,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 300–329why, one step back300 ) -> Response:301 if auth is None:302 auth = self.auth303304 url = request.url305 if auth is None and (url.username or url.password):306 auth = HTTPBasicAuth(username=url.username, password=url.password)307308 if auth is not None:309 if isinstance(auth, tuple):310 auth = HTTPBasicAuth(username=auth[0], password=auth[1])311 request = auth(request)312313 response = await self.send_handling_redirects(314 request,315 stream=stream,316 ssl=ssl,317 timeout=timeout,318 allow_redirects=allow_redirects,319 )320 return response321322 async def send_handling_redirects(323 self,324 request: Request,325 *,326 stream: bool = False,327 ssl: SSLConfig = None,328 timeout: TimeoutConfig = None,329 allow_redirects: bool = True,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 330why, one step back330 history: typing.List[Response] = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 331why, one step back331 ) -> Response:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 332–333why, one step back332 if history is None:333 history = []Finessing interfaceTom Christiebefore adoptiond7619a9
line 334why, one step back334Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 335–341why, one step back335 while True:336 # We perform these checks here, so that calls to `response.next()`337 # will raise redirect errors if appropriate.338 if len(history) > self.max_redirects:339 raise TooManyRedirects()340 if request.url in [response.url for response in history]:341 raise RedirectLoop()Finessing interfaceTom Christiebefore adoptiond7619a9
line 342why, one step back342Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 343–346why, one step back343 response = await self.dispatch.send(344 request, stream=stream, ssl=ssl, timeout=timeout345 )346 response.history = list(history)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 347why, one step back347 self.cookies.extract_cookies(response)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 348–350why, one step back348 history = [response] + history349 if not response.is_redirect:350 breakMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 351why, one step back351Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 352–358why, one step back352 if allow_redirects:353 request = self.build_redirect_request(request, response)354 else:355356 async def send_next() -> Response:357 nonlocal request, response, ssl, allow_redirects, timeout, history358 request = self.build_redirect_request(request, response)Auth (#65)Tom Christiebefore adoption5afa7dd
line 359why, one step back359 response = await self.send_handling_redirects(Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 360–378why, one step back360 request,361 stream=stream,362 allow_redirects=allow_redirects,363 ssl=ssl,364 timeout=timeout,365 history=history,366 )367 return response368369 response.next = send_next # type: ignore370 break371372 return response373374 def build_redirect_request(self, request: Request, response: Response) -> Request:375 method = self.redirect_method(request, response)376 url = self.redirect_url(request, response)377 headers = self.redirect_headers(request, url)378 content = self.redirect_content(request, method)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 379–380why, one step back379 cookies = self.merge_cookies(request.cookies)380 return Request(method=method, url=url, headers=headers, data=content, cookies=cookies)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 381–389why, one step back381382 def redirect_method(self, request: Request, response: Response) -> str:383 """384 When being redirected we may want to change the method of the request385 based on certain specs or browser behavior.386 """387 method = request.method388389 # https://tools.ietf.org/html/rfc7231#section-6.4.4Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 390why, one step back390 if response.status_code == codes.SEE_OTHER and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 391–394why, one step back391 method = "GET"392393 # Do what the browsers do, despite standards...394 # Turn 302s into GETs.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 395why, one step back395 if response.status_code == codes.FOUND and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 396–399why, one step back396 method = "GET"397398 # If a POST is responded to with a 301, turn it into a GET.399 # This bizarre behaviour is explained in 'requests' issue 1704.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 400why, one step back400 if response.status_code == codes.MOVED_PERMANENTLY and method == "POST":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 401–443why, one step back401 method = "GET"402403 return method404405 def redirect_url(self, request: Request, response: Response) -> URL:406 """407 Return the URL for the redirect to follow.408 """409 location = response.headers["Location"]410411 url = URL(location, allow_relative=True)412413 # Facilitate relative 'Location' headers, as allowed by RFC 7231.414 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')415 if url.is_relative_url:416 url = url.resolve_with(request.url)417418 # Attach previous fragment if needed (RFC 7231 7.1.2)419 if request.url.fragment and not url.fragment:420 url = url.copy_with(fragment=request.url.fragment)421422 return url423424 def redirect_headers(self, request: Request, url: URL) -> Headers:425 """426 Strip Authorization headers when responses are redirected away from427 the origin.428 """429 headers = Headers(request.headers)430 if url.origin != request.url.origin:431 del headers["Authorization"]432 return headers433434 def redirect_content(self, request: Request, method: str) -> bytes:435 """436 Return the body that should be used for the redirect request.437 """438 if method != request.method and method == "GET":439 return b""440 if request.is_streaming:441 raise RedirectBodyUnavailable()442 return request.content443Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 444why, one step back444 async def close(self) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 445why, one step back445 await self.dispatch.close()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 446why, one step back446Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 447why, one step back447 async def __aenter__(self) -> "AsyncClient":Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 448–456why, one step back448 return self449450 async def __aexit__(451 self,452 exc_type: typing.Type[BaseException] = None,453 exc_value: BaseException = None,454 traceback: TracebackType = None,455 ) -> None:456 await self.close()Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 457–461why, one step back457458459class Client:460 def __init__(461 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 462why, one step back462 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 463–467why, one step back463 ssl: SSLConfig = DEFAULT_SSL_CONFIG,464 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,465 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,466 max_redirects: int = DEFAULT_MAX_REDIRECTS,467 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 468why, one step back468 backend: ConcurrencyBackend = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 469–470why, one step back469 ) -> None:470 self._client = AsyncClient(Auth (#65)Tom Christiebefore adoption5afa7dd
line 471why, one step back471 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 472–476why, one step back472 ssl=ssl,473 timeout=timeout,474 pool_limits=pool_limits,475 max_redirects=max_redirects,476 dispatch=dispatch,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 477why, one step back477 backend=backend,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 478–480why, one step back478 )479 self._loop = asyncio.new_event_loop()480Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 481–484why, one step back481 @property482 def cookies(self) -> Cookies:483 return self._client.cookies484Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 485–492why, one step back485 def request(486 self,487 method: str,488 url: URLTypes,489 *,490 data: RequestData = b"",491 query_params: QueryParamTypes = None,492 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 493why, one step back493 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 494why, one step back494 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 495why, one step back495 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 496–500why, one step back496 allow_redirects: bool = True,497 ssl: SSLConfig = None,498 timeout: TimeoutConfig = None,499 ) -> SyncResponse:500 request = Request(Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 501–506why, one step back501 method,502 url,503 data=data,504 query_params=query_params,505 headers=headers,506 cookies=self._client.merge_cookies(cookies),Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 507–511why, one step back507 )508 self.prepare_request(request)509 response = self.send(510 request,511 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 512why, one step back512 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 513–524why, one step back513 allow_redirects=allow_redirects,514 ssl=ssl,515 timeout=timeout,516 )517 return response518519 def get(520 self,521 url: URLTypes,522 *,523 query_params: QueryParamTypes = None,524 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 525why, one step back525 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 526why, one step back526 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 527why, one step back527 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 528–535why, one step back528 allow_redirects: bool = True,529 ssl: SSLConfig = None,530 timeout: TimeoutConfig = None,531 ) -> SyncResponse:532 return self.request(533 "GET",534 url,535 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 536why, one step back536 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 537why, one step back537 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 538why, one step back538 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 539–549why, one step back539 allow_redirects=allow_redirects,540 ssl=ssl,541 timeout=timeout,542 )543544 def options(545 self,546 url: URLTypes,547 *,548 query_params: QueryParamTypes = None,549 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 550why, one step back550 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 551why, one step back551 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 552why, one step back552 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 553–560why, one step back553 allow_redirects: bool = True,554 ssl: SSLConfig = None,555 timeout: TimeoutConfig = None,556 ) -> SyncResponse:557 return self.request(558 "OPTIONS",559 url,560 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 561why, one step back561 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 562why, one step back562 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 563why, one step back563 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 564–574why, one step back564 allow_redirects=allow_redirects,565 ssl=ssl,566 timeout=timeout,567 )568569 def head(570 self,571 url: URLTypes,572 *,573 query_params: QueryParamTypes = None,574 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 575why, one step back575 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 576why, one step back576 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 577why, one step back577 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 578–585why, one step back578 allow_redirects: bool = False, # Note: Differs to usual default.579 ssl: SSLConfig = None,580 timeout: TimeoutConfig = None,581 ) -> SyncResponse:582 return self.request(583 "HEAD",584 url,585 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 586why, one step back586 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 587why, one step back587 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 588why, one step back588 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 589–600why, one step back589 allow_redirects=allow_redirects,590 ssl=ssl,591 timeout=timeout,592 )593594 def post(595 self,596 url: URLTypes,597 *,598 data: RequestData = b"",599 query_params: QueryParamTypes = None,600 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 601why, one step back601 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 602why, one step back602 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 603why, one step back603 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 604–612why, one step back604 allow_redirects: bool = True,605 ssl: SSLConfig = None,606 timeout: TimeoutConfig = None,607 ) -> SyncResponse:608 return self.request(609 "POST",610 url,611 data=data,612 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 613why, one step back613 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 614why, one step back614 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 615why, one step back615 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 616–627why, one step back616 allow_redirects=allow_redirects,617 ssl=ssl,618 timeout=timeout,619 )620621 def put(622 self,623 url: URLTypes,624 *,625 data: RequestData = b"",626 query_params: QueryParamTypes = None,627 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 628why, one step back628 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 629why, one step back629 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 630why, one step back630 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 631–639why, one step back631 allow_redirects: bool = True,632 ssl: SSLConfig = None,633 timeout: TimeoutConfig = None,634 ) -> SyncResponse:635 return self.request(636 "PUT",637 url,638 data=data,639 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 640why, one step back640 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 641why, one step back641 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 642why, one step back642 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 643–654why, one step back643 allow_redirects=allow_redirects,644 ssl=ssl,645 timeout=timeout,646 )647648 def patch(649 self,650 url: URLTypes,651 *,652 data: RequestData = b"",653 query_params: QueryParamTypes = None,654 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 655why, one step back655 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 656why, one step back656 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 657why, one step back657 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 658–666why, one step back658 allow_redirects: bool = True,659 ssl: SSLConfig = None,660 timeout: TimeoutConfig = None,661 ) -> SyncResponse:662 return self.request(663 "PATCH",664 url,665 data=data,666 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 667why, one step back667 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 668why, one step back668 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 669why, one step back669 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 670–681why, one step back670 allow_redirects=allow_redirects,671 ssl=ssl,672 timeout=timeout,673 )674675 def delete(676 self,677 url: URLTypes,678 *,679 data: RequestData = b"",680 query_params: QueryParamTypes = None,681 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 682why, one step back682 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 683why, one step back683 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 684why, one step back684 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 685–693why, one step back685 allow_redirects: bool = True,686 ssl: SSLConfig = None,687 timeout: TimeoutConfig = None,688 ) -> SyncResponse:689 return self.request(690 "DELETE",691 url,692 data=data,693 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 694why, one step back694 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 695why, one step back695 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 696why, one step back696 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 697–709why, one step back697 allow_redirects=allow_redirects,698 ssl=ssl,699 timeout=timeout,700 )701702 def prepare_request(self, request: Request) -> None:703 self._client.prepare_request(request)704705 def send(706 self,707 request: Request,708 *,709 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 710why, one step back710 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 711–718why, one step back711 allow_redirects: bool = True,712 ssl: SSLConfig = None,713 timeout: TimeoutConfig = None,714 ) -> SyncResponse:715 response = self._loop.run_until_complete(716 self._client.send(717 request,718 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 719why, one step back719 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 720–739why, one step back720 allow_redirects=allow_redirects,721 ssl=ssl,722 timeout=timeout,723 )724 )725 return SyncResponse(response, self._loop)726727 def close(self) -> None:728 self._loop.run_until_complete(self._client.close())729730 def __enter__(self) -> "Client":731 return self732733 def __exit__(734 self,735 exc_type: typing.Type[BaseException] = None,736 exc_value: BaseException = None,737 traceback: TracebackType = None,738 ) -> None:739 self.close()