client.py
This file
Path
httpcore/client.py
763 lines · 21.2 KiB · Python
Last changed by
Add json interfacesTom Christiebefore adoption5a63d31
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"",Add json interfacesTom Christiebefore adoption5a63d31
line 140why, one step back140 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 141why, one step back141 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 142why, one step back142 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 143why, one step back143 cookies: CookieTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 144why, one step back144 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 145why, one step back145 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 146–148why, one step back146 allow_redirects: bool = True,147 ssl: SSLConfig = None,148 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 149–152why, one step back149 ) -> Response:150 return await self.request(151 "POST",152 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 153why, one step back153 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 154why, one step back154 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 155why, one step back155 params=params,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 156why, one step back156 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 157why, one step back157 cookies=cookies,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 158why, one step back158 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 159why, one step back159 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
line 160why, one step back160 allow_redirects=allow_redirects,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 161–164why, one step back161 ssl=ssl,162 timeout=timeout,163 )164Finessing interfaceTom Christiebefore adoptiond7619a9
lines 165–168why, one step back165 async def put(166 self,167 url: URLTypes,168 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 169why, one step back169 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 170why, one step back170 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 171why, one step back171 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 172why, one step back172 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 173why, one step back173 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 174why, one step back174 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 175why, one step back175 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 176–182why, one step back176 allow_redirects: bool = True,177 ssl: SSLConfig = None,178 timeout: TimeoutConfig = None,179 ) -> Response:180 return await self.request(181 "PUT",182 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 183why, one step back183 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 184why, one step back184 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 185why, one step back185 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 186why, one step back186 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 187why, one step back187 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 188why, one step back188 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 189why, one step back189 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 190–198why, one step back190 allow_redirects=allow_redirects,191 ssl=ssl,192 timeout=timeout,193 )194195 async def patch(196 self,197 url: URLTypes,198 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 199why, one step back199 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 200why, one step back200 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 201why, one step back201 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 202why, one step back202 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 203why, one step back203 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 204why, one step back204 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 205why, one step back205 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 206–212why, one step back206 allow_redirects: bool = True,207 ssl: SSLConfig = None,208 timeout: TimeoutConfig = None,209 ) -> Response:210 return await self.request(211 "PATCH",212 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 213why, one step back213 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 214why, one step back214 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 215why, one step back215 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 216why, one step back216 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 217why, one step back217 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 218why, one step back218 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 219why, one step back219 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 220–228why, one step back220 allow_redirects=allow_redirects,221 ssl=ssl,222 timeout=timeout,223 )224225 async def delete(226 self,227 url: URLTypes,228 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 229why, one step back229 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 230why, one step back230 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 231why, one step back231 params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 232why, one step back232 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 233why, one step back233 cookies: CookieTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 234why, one step back234 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 235why, one step back235 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 236–242why, one step back236 allow_redirects: bool = True,237 ssl: SSLConfig = None,238 timeout: TimeoutConfig = None,239 ) -> Response:240 return await self.request(241 "DELETE",242 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 243why, one step back243 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 244why, one step back244 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 245why, one step back245 params=params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 246why, one step back246 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 247why, one step back247 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 248why, one step back248 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 249why, one step back249 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 250–254why, one step back250 allow_redirects=allow_redirects,251 ssl=ssl,252 timeout=timeout,253 )254Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 255–260why, one step back255 async def request(256 self,257 method: str,258 url: URLTypes,259 *,260 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 261why, one step back261 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 262why, one step back262 params: QueryParamTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 263why, one step back263 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 264why, one step back264 cookies: CookieTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 265why, one step back265 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 266why, one step back266 auth: AuthTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 267–271why, one step back267 allow_redirects: bool = True,268 ssl: SSLConfig = None,269 timeout: TimeoutConfig = None,270 ) -> Response:271 request = Request(Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 272–274why, one step back272 method,273 url,274 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 275why, one step back275 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 276why, one step back276 params=params,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 277–278why, one step back277 headers=headers,278 cookies=self.merge_cookies(cookies),Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 279–283why, one step back279 )280 self.prepare_request(request)281 response = await self.send(282 request,283 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 284why, one step back284 auth=auth,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 285–290why, one step back285 allow_redirects=allow_redirects,286 ssl=ssl,287 timeout=timeout,288 )289 return response290Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 291why, one step back291 def prepare_request(self, request: Request) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 292why, one step back292 request.prepare()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 293why, one step back293query_params -> paramsTom Christiebefore adoptionbb70377
lines 294–296why, one step back294 def merge_cookies(295 self, cookies: CookieTypes = None296 ) -> typing.Optional[CookieTypes]:Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 297–302why, one step back297 if cookies or self.cookies:298 merged_cookies = Cookies(self.cookies)299 merged_cookies.update(cookies)300 return merged_cookies301 return cookies302Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 303–307why, one step back303 async def send(304 self,305 request: Request,306 *,307 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 308why, one step back308 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 309–310why, one step back309 ssl: SSLConfig = None,310 timeout: TimeoutConfig = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 311why, one step back311 allow_redirects: bool = True,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 312–341why, one step back312 ) -> Response:313 if auth is None:314 auth = self.auth315316 url = request.url317 if auth is None and (url.username or url.password):318 auth = HTTPBasicAuth(username=url.username, password=url.password)319320 if auth is not None:321 if isinstance(auth, tuple):322 auth = HTTPBasicAuth(username=auth[0], password=auth[1])323 request = auth(request)324325 response = await self.send_handling_redirects(326 request,327 stream=stream,328 ssl=ssl,329 timeout=timeout,330 allow_redirects=allow_redirects,331 )332 return response333334 async def send_handling_redirects(335 self,336 request: Request,337 *,338 stream: bool = False,339 ssl: SSLConfig = None,340 timeout: TimeoutConfig = None,341 allow_redirects: bool = True,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 342why, one step back342 history: typing.List[Response] = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 343why, one step back343 ) -> Response:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 344–345why, one step back344 if history is None:345 history = []Finessing interfaceTom Christiebefore adoptiond7619a9
line 346why, one step back346Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 347–353why, one step back347 while True:348 # We perform these checks here, so that calls to `response.next()`349 # will raise redirect errors if appropriate.350 if len(history) > self.max_redirects:351 raise TooManyRedirects()352 if request.url in [response.url for response in history]:353 raise RedirectLoop()Finessing interfaceTom Christiebefore adoptiond7619a9
line 354why, one step back354Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 355–358why, one step back355 response = await self.dispatch.send(356 request, stream=stream, ssl=ssl, timeout=timeout357 )358 response.history = list(history)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 359why, one step back359 self.cookies.extract_cookies(response)Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 360–362why, one step back360 history = [response] + history361 if not response.is_redirect:362 breakMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 363why, one step back363Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 364–370why, one step back364 if allow_redirects:365 request = self.build_redirect_request(request, response)366 else:367368 async def send_next() -> Response:369 nonlocal request, response, ssl, allow_redirects, timeout, history370 request = self.build_redirect_request(request, response)Auth (#65)Tom Christiebefore adoption5afa7dd
line 371why, one step back371 response = await self.send_handling_redirects(Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 372–390why, one step back372 request,373 stream=stream,374 allow_redirects=allow_redirects,375 ssl=ssl,376 timeout=timeout,377 history=history,378 )379 return response380381 response.next = send_next # type: ignore382 break383384 return response385386 def build_redirect_request(self, request: Request, response: Response) -> Request:387 method = self.redirect_method(request, response)388 url = self.redirect_url(request, response)389 headers = self.redirect_headers(request, url)390 content = self.redirect_content(request, method)Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 391why, one step back391 cookies = self.merge_cookies(request.cookies)query_params -> paramsTom Christiebefore adoptionbb70377
lines 392–394why, one step back392 return Request(393 method=method, url=url, headers=headers, data=content, cookies=cookies394 )Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 395–403why, one step back395396 def redirect_method(self, request: Request, response: Response) -> str:397 """398 When being redirected we may want to change the method of the request399 based on certain specs or browser behavior.400 """401 method = request.method402403 # https://tools.ietf.org/html/rfc7231#section-6.4.4Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 404why, one step back404 if response.status_code == codes.SEE_OTHER and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 405–408why, one step back405 method = "GET"406407 # Do what the browsers do, despite standards...408 # Turn 302s into GETs.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 409why, one step back409 if response.status_code == codes.FOUND and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 410–413why, one step back410 method = "GET"411412 # If a POST is responded to with a 301, turn it into a GET.413 # This bizarre behaviour is explained in 'requests' issue 1704.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 414why, one step back414 if response.status_code == codes.MOVED_PERMANENTLY and method == "POST":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 415–457why, one step back415 method = "GET"416417 return method418419 def redirect_url(self, request: Request, response: Response) -> URL:420 """421 Return the URL for the redirect to follow.422 """423 location = response.headers["Location"]424425 url = URL(location, allow_relative=True)426427 # Facilitate relative 'Location' headers, as allowed by RFC 7231.428 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')429 if url.is_relative_url:430 url = url.resolve_with(request.url)431432 # Attach previous fragment if needed (RFC 7231 7.1.2)433 if request.url.fragment and not url.fragment:434 url = url.copy_with(fragment=request.url.fragment)435436 return url437438 def redirect_headers(self, request: Request, url: URL) -> Headers:439 """440 Strip Authorization headers when responses are redirected away from441 the origin.442 """443 headers = Headers(request.headers)444 if url.origin != request.url.origin:445 del headers["Authorization"]446 return headers447448 def redirect_content(self, request: Request, method: str) -> bytes:449 """450 Return the body that should be used for the redirect request.451 """452 if method != request.method and method == "GET":453 return b""454 if request.is_streaming:455 raise RedirectBodyUnavailable()456 return request.content457Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 458why, one step back458 async def close(self) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 459why, one step back459 await self.dispatch.close()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 460why, one step back460Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 461why, one step back461 async def __aenter__(self) -> "AsyncClient":Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 462–470why, one step back462 return self463464 async def __aexit__(465 self,466 exc_type: typing.Type[BaseException] = None,467 exc_value: BaseException = None,468 traceback: TracebackType = None,469 ) -> None:470 await self.close()Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 471–475why, one step back471472473class Client:474 def __init__(475 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 476why, one step back476 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 477–481why, one step back477 ssl: SSLConfig = DEFAULT_SSL_CONFIG,478 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,479 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,480 max_redirects: int = DEFAULT_MAX_REDIRECTS,481 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 482why, one step back482 backend: ConcurrencyBackend = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 483–484why, one step back483 ) -> None:484 self._client = AsyncClient(Auth (#65)Tom Christiebefore adoption5afa7dd
line 485why, one step back485 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 486–490why, one step back486 ssl=ssl,487 timeout=timeout,488 pool_limits=pool_limits,489 max_redirects=max_redirects,490 dispatch=dispatch,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 491why, one step back491 backend=backend,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 492–494why, one step back492 )493 self._loop = asyncio.new_event_loop()494Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 495–498why, one step back495 @property496 def cookies(self) -> Cookies:497 return self._client.cookies498Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 499–504why, one step back499 def request(500 self,501 method: str,502 url: URLTypes,503 *,504 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 505why, one step back505 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 506why, one step back506 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 507why, one step back507 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 508why, one step back508 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 509why, one step back509 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 510why, one step back510 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 511–515why, one step back511 allow_redirects: bool = True,512 ssl: SSLConfig = None,513 timeout: TimeoutConfig = None,514 ) -> SyncResponse:515 request = Request(Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 516–518why, one step back516 method,517 url,518 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 519why, one step back519 json=json,query_params -> paramsTom Christiebefore adoptionbb70377
line 520why, one step back520 params=params,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
lines 521–522why, one step back521 headers=headers,522 cookies=self._client.merge_cookies(cookies),Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 523–527why, one step back523 )524 self.prepare_request(request)525 response = self.send(526 request,527 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 528why, one step back528 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 529–538why, one step back529 allow_redirects=allow_redirects,530 ssl=ssl,531 timeout=timeout,532 )533 return response534535 def get(536 self,537 url: URLTypes,538 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 539why, one step back539 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 540why, one step back540 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 541why, one step back541 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 542why, one step back542 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 543why, one step back543 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 544–551why, one step back544 allow_redirects: bool = True,545 ssl: SSLConfig = None,546 timeout: TimeoutConfig = None,547 ) -> SyncResponse:548 return self.request(549 "GET",550 url,551 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 552why, one step back552 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 553why, one step back553 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 554why, one step back554 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 555–563why, one step back555 allow_redirects=allow_redirects,556 ssl=ssl,557 timeout=timeout,558 )559560 def options(561 self,562 url: URLTypes,563 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 564why, one step back564 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 565why, one step back565 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 566why, one step back566 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 567why, one step back567 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 568why, one step back568 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 569–576why, one step back569 allow_redirects: bool = True,570 ssl: SSLConfig = None,571 timeout: TimeoutConfig = None,572 ) -> SyncResponse:573 return self.request(574 "OPTIONS",575 url,576 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 577why, one step back577 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 578why, one step back578 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 579why, one step back579 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 580–588why, one step back580 allow_redirects=allow_redirects,581 ssl=ssl,582 timeout=timeout,583 )584585 def head(586 self,587 url: URLTypes,588 *,query_params -> paramsTom Christiebefore adoptionbb70377
line 589why, one step back589 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 590why, one step back590 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 591why, one step back591 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 592why, one step back592 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 593why, one step back593 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 594–601why, one step back594 allow_redirects: bool = False, # Note: Differs to usual default.595 ssl: SSLConfig = None,596 timeout: TimeoutConfig = None,597 ) -> SyncResponse:598 return self.request(599 "HEAD",600 url,601 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 602why, one step back602 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 603why, one step back603 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 604why, one step back604 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 605–614why, one step back605 allow_redirects=allow_redirects,606 ssl=ssl,607 timeout=timeout,608 )609610 def post(611 self,612 url: URLTypes,613 *,614 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 615why, one step back615 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 616why, one step back616 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 617why, one step back617 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 618why, one step back618 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 619why, one step back619 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 620why, one step back620 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 621–628why, one step back621 allow_redirects: bool = True,622 ssl: SSLConfig = None,623 timeout: TimeoutConfig = None,624 ) -> SyncResponse:625 return self.request(626 "POST",627 url,628 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 629why, one step back629 json=json,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 630why, one step back630 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 631why, one step back631 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 632why, one step back632 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 633why, one step back633 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 634–643why, one step back634 allow_redirects=allow_redirects,635 ssl=ssl,636 timeout=timeout,637 )638639 def put(640 self,641 url: URLTypes,642 *,643 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 644why, one step back644 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 645why, one step back645 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 646why, one step back646 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 647why, one step back647 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 648why, one step back648 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 649why, one step back649 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 650–657why, one step back650 allow_redirects: bool = True,651 ssl: SSLConfig = None,652 timeout: TimeoutConfig = None,653 ) -> SyncResponse:654 return self.request(655 "PUT",656 url,657 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 658why, one step back658 json=json,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 659why, one step back659 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 660why, one step back660 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 661why, one step back661 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 662why, one step back662 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 663–672why, one step back663 allow_redirects=allow_redirects,664 ssl=ssl,665 timeout=timeout,666 )667668 def patch(669 self,670 url: URLTypes,671 *,672 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 673why, one step back673 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 674why, one step back674 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 675why, one step back675 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 676why, one step back676 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 677why, one step back677 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 678why, one step back678 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 679–686why, one step back679 allow_redirects: bool = True,680 ssl: SSLConfig = None,681 timeout: TimeoutConfig = None,682 ) -> SyncResponse:683 return self.request(684 "PATCH",685 url,686 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 687why, one step back687 json=json,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 688why, one step back688 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 689why, one step back689 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 690why, one step back690 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 691why, one step back691 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 692–701why, one step back692 allow_redirects=allow_redirects,693 ssl=ssl,694 timeout=timeout,695 )696697 def delete(698 self,699 url: URLTypes,700 *,701 data: RequestData = b"",Add json interfacesTom Christiebefore adoption5a63d31
line 702why, one step back702 json: typing.Any = None,query_params -> paramsTom Christiebefore adoptionbb70377
line 703why, one step back703 params: QueryParamTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 704why, one step back704 headers: HeaderTypes = None,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 705why, one step back705 cookies: CookieTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 706why, one step back706 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 707why, one step back707 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 708–715why, one step back708 allow_redirects: bool = True,709 ssl: SSLConfig = None,710 timeout: TimeoutConfig = None,711 ) -> SyncResponse:712 return self.request(713 "DELETE",714 url,715 data=data,Add json interfacesTom Christiebefore adoption5a63d31
line 716why, one step back716 json=json,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 717why, one step back717 headers=headers,Cookie support (#73)Tom Christiebefore adoptionfeae0a8
line 718why, one step back718 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 719why, one step back719 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 720why, one step back720 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 721–733why, one step back721 allow_redirects=allow_redirects,722 ssl=ssl,723 timeout=timeout,724 )725726 def prepare_request(self, request: Request) -> None:727 self._client.prepare_request(request)728729 def send(730 self,731 request: Request,732 *,733 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 734why, one step back734 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 735–742why, one step back735 allow_redirects: bool = True,736 ssl: SSLConfig = None,737 timeout: TimeoutConfig = None,738 ) -> SyncResponse:739 response = self._loop.run_until_complete(740 self._client.send(741 request,742 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 743why, one step back743 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 744–763why, one step back744 allow_redirects=allow_redirects,745 ssl=ssl,746 timeout=timeout,747 )748 )749 return SyncResponse(response, self._loop)750751 def close(self) -> None:752 self._loop.run_until_complete(self._client.close())753754 def __enter__(self) -> "Client":755 return self756757 def __exit__(758 self,759 exc_type: typing.Type[BaseException] = None,760 exc_value: BaseException = None,761 traceback: TracebackType = None,762 ) -> None:763 self.close()