client.py
This file
Path
httpcore/client.py
723 lines · 20.2 KiB · Python
Last changed by
Initial pass at cookie supportTom Christiebefore adoption4a3325f
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
line 2why, one step back2import typingInitial pass at cookie supportTom Christiebefore adoption4a3325f
line 3why, one step back3from http.cookiejar import CookieJarMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 4–5why, one step back4from types import TracebackType5Auth (#65)Tom Christiebefore adoption5afa7dd
line 6why, one step back6from .auth import HTTPBasicAuthMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 7–16why, one step back7from .config import (8 DEFAULT_MAX_REDIRECTS,9 DEFAULT_POOL_LIMITS,10 DEFAULT_SSL_CONFIG,11 DEFAULT_TIMEOUT_CONFIG,12 PoolLimits,13 SSLConfig,14 TimeoutConfig,15)16from .dispatch.connection_pool import ConnectionPoolMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 17why, one step back17from .exceptions import RedirectBodyUnavailable, RedirectLoop, TooManyRedirectsMerge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 18why, one step back18from .interfaces import ConcurrencyBackend, DispatcherMerge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 19–20why, one step back19from .models import (20 URL,Auth (#65)Tom Christiebefore adoption5afa7dd
line 21why, one step back21 AuthTypes,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 22why, one step back22 Headers,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 23–25why, one step back23 HeaderTypes,24 QueryParamTypes,25 Request,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 26why, one step back26 RequestData,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 27why, one step back27 Response,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 28why, one step back28 SyncResponse,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
lines 29–30why, one step back29 URLTypes,30)Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 31why, one step back31from .status_codes import codesMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 32–33why, one step back3233Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 34why, one step back34class AsyncClient:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 35–36why, one step back35 def __init__(36 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 37why, one step back37 auth: AuthTypes = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 38–39why, one step back38 ssl: SSLConfig = DEFAULT_SSL_CONFIG,39 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,Truckin' onTom Christiebefore adoption6b60d95
line 40why, one step back40 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 41why, one step back41 max_redirects: int = DEFAULT_MAX_REDIRECTS,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 42why, one step back42 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 43why, one step back43 backend: ConcurrencyBackend = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 44why, one step back44 ):Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 45why, one step back45 if dispatch is None:Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
lines 46–48why, one step back46 dispatch = ConnectionPool(47 ssl=ssl, timeout=timeout, pool_limits=pool_limits, backend=backend48 )Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 49why, one step back49Auth (#65)Tom Christiebefore adoption5afa7dd
line 50why, one step back50 self.auth = authMerge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 51–52why, one step back51 self.max_redirects = max_redirects52 self.dispatch = dispatchMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 53–55why, one step back5354 async def get(55 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 56why, one step back56 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 57why, one step back57 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 58why, one step back58 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 59why, one step back59 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 60why, one step back60 cookies: CookieJar = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 61why, one step back61 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 62why, one step back62 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 63–65why, one step back63 allow_redirects: bool = True,64 ssl: SSLConfig = None,65 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 66–67why, one step back66 ) -> Response:67 return await self.request(Finessing interfaceTom Christiebefore adoptiond7619a9
lines 68–69why, one step back68 "GET",69 url,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 70why, one step back70 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 71why, one step back71 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 72why, one step back72 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 73why, one step back73 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 74why, one step back74 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 75–77why, one step back75 allow_redirects=allow_redirects,76 ssl=ssl,77 timeout=timeout,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 78–79why, one step back78 )79Finessing interfaceTom Christiebefore adoptiond7619a9
lines 80–83why, one step back80 async def options(81 self,82 url: URLTypes,83 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 84why, one step back84 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 85why, one step back85 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 86why, one step back86 cookies: CookieJar = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 87why, one step back87 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 88why, one step back88 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 89–95why, one step back89 allow_redirects: bool = True,90 ssl: SSLConfig = None,91 timeout: TimeoutConfig = None,92 ) -> Response:93 return await self.request(94 "OPTIONS",95 url,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 96why, one step back96 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 97why, one step back97 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 98why, one step back98 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 99why, one step back99 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 100why, one step back100 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 101–109why, one step back101 allow_redirects=allow_redirects,102 ssl=ssl,103 timeout=timeout,104 )105106 async def head(107 self,108 url: URLTypes,109 *,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 110why, one step back110 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 111why, one step back111 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 112why, one step back112 cookies: CookieJar = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 113why, one step back113 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 114why, one step back114 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 115–121why, one step back115 allow_redirects: bool = False, # Note: Differs to usual default.116 ssl: SSLConfig = None,117 timeout: TimeoutConfig = None,118 ) -> Response:119 return await self.request(120 "HEAD",121 url,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 122why, one step back122 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 123why, one step back123 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 124why, one step back124 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 125why, one step back125 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 126why, one step back126 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 127–131why, one step back127 allow_redirects=allow_redirects,128 ssl=ssl,129 timeout=timeout,130 )131Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 132–133why, one step back132 async def post(133 self,Finessing interfaceTom Christiebefore adoptiond7619a9
line 134why, one step back134 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 135why, one step back135 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 136why, one step back136 data: RequestData = b"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 137why, one step back137 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 138why, one step back138 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 139why, one step back139 cookies: CookieJar = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 140why, one step back140 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 141why, one step back141 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 142–144why, one step back142 allow_redirects: bool = True,143 ssl: SSLConfig = None,144 timeout: TimeoutConfig = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 145–148why, one step back145 ) -> Response:146 return await self.request(147 "POST",148 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 149why, one step back149 data=data,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 150why, one step back150 query_params=query_params,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 151why, one step back151 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 152why, one step back152 cookies=cookies,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 153why, one step back153 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 154why, one step back154 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
line 155why, one step back155 allow_redirects=allow_redirects,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 156–159why, one step back156 ssl=ssl,157 timeout=timeout,158 )159Finessing interfaceTom Christiebefore adoptiond7619a9
lines 160–163why, one step back160 async def put(161 self,162 url: URLTypes,163 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 164why, one step back164 data: RequestData = b"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 165why, one step back165 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 166why, one step back166 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 167why, one step back167 cookies: CookieJar = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 168why, one step back168 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 169why, one step back169 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 170–176why, one step back170 allow_redirects: bool = True,171 ssl: SSLConfig = None,172 timeout: TimeoutConfig = None,173 ) -> Response:174 return await self.request(175 "PUT",176 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 177why, one step back177 data=data,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 178why, one step back178 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 179why, one step back179 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 180why, one step back180 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 181why, one step back181 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 182why, one step back182 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 183–191why, one step back183 allow_redirects=allow_redirects,184 ssl=ssl,185 timeout=timeout,186 )187188 async def patch(189 self,190 url: URLTypes,191 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 192why, one step back192 data: RequestData = b"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 193why, one step back193 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 194why, one step back194 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 195why, one step back195 cookies: CookieJar = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 196why, one step back196 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 197why, one step back197 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 198–204why, one step back198 allow_redirects: bool = True,199 ssl: SSLConfig = None,200 timeout: TimeoutConfig = None,201 ) -> Response:202 return await self.request(203 "PATCH",204 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 205why, one step back205 data=data,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 206why, one step back206 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 207why, one step back207 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 208why, one step back208 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 209why, one step back209 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 210why, one step back210 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 211–219why, one step back211 allow_redirects=allow_redirects,212 ssl=ssl,213 timeout=timeout,214 )215216 async def delete(217 self,218 url: URLTypes,219 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 220why, one step back220 data: RequestData = b"",Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 221why, one step back221 query_params: QueryParamTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 222why, one step back222 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 223why, one step back223 cookies: CookieJar = None,Finessing interfaceTom Christiebefore adoptiond7619a9
line 224why, one step back224 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 225why, one step back225 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 226–232why, one step back226 allow_redirects: bool = True,227 ssl: SSLConfig = None,228 timeout: TimeoutConfig = None,229 ) -> Response:230 return await self.request(231 "DELETE",232 url,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 233why, one step back233 data=data,Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
line 234why, one step back234 query_params=query_params,Finessing interfaceTom Christiebefore adoptiond7619a9
line 235why, one step back235 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 236why, one step back236 cookies=cookies,Finessing interfaceTom Christiebefore adoptiond7619a9
line 237why, one step back237 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 238why, one step back238 auth=auth,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 239–243why, one step back239 allow_redirects=allow_redirects,240 ssl=ssl,241 timeout=timeout,242 )243Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 244–251why, one step back244 async def request(245 self,246 method: str,247 url: URLTypes,248 *,249 data: RequestData = b"",250 query_params: QueryParamTypes = None,251 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 252why, one step back252 cookies: CookieJar = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 253why, one step back253 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 254why, one step back254 auth: AuthTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 255–259why, one step back255 allow_redirects: bool = True,256 ssl: SSLConfig = None,257 timeout: TimeoutConfig = None,258 ) -> Response:259 request = Request(Initial pass at cookie supportTom Christiebefore adoption4a3325f
lines 260–265why, one step back260 method,261 url,262 data=data,263 query_params=query_params,264 headers=headers,265 cookies=cookies,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 266–270why, one step back266 )267 self.prepare_request(request)268 response = await self.send(269 request,270 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 271why, one step back271 auth=auth,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 272–277why, one step back272 allow_redirects=allow_redirects,273 ssl=ssl,274 timeout=timeout,275 )276 return response277Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 278why, one step back278 def prepare_request(self, request: Request) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 279why, one step back279 request.prepare()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 280–285why, one step back280281 async def send(282 self,283 request: Request,284 *,285 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 286why, one step back286 auth: AuthTypes = None,Finessing interfaceTom Christiebefore adoptiond7619a9
lines 287–288why, one step back287 ssl: SSLConfig = None,288 timeout: TimeoutConfig = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 289why, one step back289 allow_redirects: bool = True,Auth (#65)Tom Christiebefore adoption5afa7dd
lines 290–319why, one step back290 ) -> Response:291 if auth is None:292 auth = self.auth293294 url = request.url295 if auth is None and (url.username or url.password):296 auth = HTTPBasicAuth(username=url.username, password=url.password)297298 if auth is not None:299 if isinstance(auth, tuple):300 auth = HTTPBasicAuth(username=auth[0], password=auth[1])301 request = auth(request)302303 response = await self.send_handling_redirects(304 request,305 stream=stream,306 ssl=ssl,307 timeout=timeout,308 allow_redirects=allow_redirects,309 )310 return response311312 async def send_handling_redirects(313 self,314 request: Request,315 *,316 stream: bool = False,317 ssl: SSLConfig = None,318 timeout: TimeoutConfig = None,319 allow_redirects: bool = True,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 320why, one step back320 history: typing.List[Response] = None,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 321why, one step back321 ) -> Response:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 322–323why, one step back322 if history is None:323 history = []Finessing interfaceTom Christiebefore adoptiond7619a9
line 324why, one step back324Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 325–331why, one step back325 while True:326 # We perform these checks here, so that calls to `response.next()`327 # will raise redirect errors if appropriate.328 if len(history) > self.max_redirects:329 raise TooManyRedirects()330 if request.url in [response.url for response in history]:331 raise RedirectLoop()Finessing interfaceTom Christiebefore adoptiond7619a9
line 332why, one step back332Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 333–339why, one step back333 response = await self.dispatch.send(334 request, stream=stream, ssl=ssl, timeout=timeout335 )336 response.history = list(history)337 history = [response] + history338 if not response.is_redirect:339 breakMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 340why, one step back340Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 341–347why, one step back341 if allow_redirects:342 request = self.build_redirect_request(request, response)343 else:344345 async def send_next() -> Response:346 nonlocal request, response, ssl, allow_redirects, timeout, history347 request = self.build_redirect_request(request, response)Auth (#65)Tom Christiebefore adoption5afa7dd
line 348why, one step back348 response = await self.send_handling_redirects(Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 349–377why, one step back349 request,350 stream=stream,351 allow_redirects=allow_redirects,352 ssl=ssl,353 timeout=timeout,354 history=history,355 )356 return response357358 response.next = send_next # type: ignore359 break360361 return response362363 def build_redirect_request(self, request: Request, response: Response) -> Request:364 method = self.redirect_method(request, response)365 url = self.redirect_url(request, response)366 headers = self.redirect_headers(request, url)367 content = self.redirect_content(request, method)368 return Request(method=method, url=url, headers=headers, data=content)369370 def redirect_method(self, request: Request, response: Response) -> str:371 """372 When being redirected we may want to change the method of the request373 based on certain specs or browser behavior.374 """375 method = request.method376377 # https://tools.ietf.org/html/rfc7231#section-6.4.4Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 378why, one step back378 if response.status_code == codes.SEE_OTHER and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 379–382why, one step back379 method = "GET"380381 # Do what the browsers do, despite standards...382 # Turn 302s into GETs.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 383why, one step back383 if response.status_code == codes.FOUND and method != "HEAD":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 384–387why, one step back384 method = "GET"385386 # If a POST is responded to with a 301, turn it into a GET.387 # This bizarre behaviour is explained in 'requests' issue 1704.Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 388why, one step back388 if response.status_code == codes.MOVED_PERMANENTLY and method == "POST":Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 389–431why, one step back389 method = "GET"390391 return method392393 def redirect_url(self, request: Request, response: Response) -> URL:394 """395 Return the URL for the redirect to follow.396 """397 location = response.headers["Location"]398399 url = URL(location, allow_relative=True)400401 # Facilitate relative 'Location' headers, as allowed by RFC 7231.402 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')403 if url.is_relative_url:404 url = url.resolve_with(request.url)405406 # Attach previous fragment if needed (RFC 7231 7.1.2)407 if request.url.fragment and not url.fragment:408 url = url.copy_with(fragment=request.url.fragment)409410 return url411412 def redirect_headers(self, request: Request, url: URL) -> Headers:413 """414 Strip Authorization headers when responses are redirected away from415 the origin.416 """417 headers = Headers(request.headers)418 if url.origin != request.url.origin:419 del headers["Authorization"]420 return headers421422 def redirect_content(self, request: Request, method: str) -> bytes:423 """424 Return the body that should be used for the redirect request.425 """426 if method != request.method and method == "GET":427 return b""428 if request.is_streaming:429 raise RedirectBodyUnavailable()430 return request.content431Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 432why, one step back432 async def close(self) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 433why, one step back433 await self.dispatch.close()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 434why, one step back434Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 435why, one step back435 async def __aenter__(self) -> "AsyncClient":Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 436–444why, one step back436 return self437438 async def __aexit__(439 self,440 exc_type: typing.Type[BaseException] = None,441 exc_value: BaseException = None,442 traceback: TracebackType = None,443 ) -> None:444 await self.close()Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 445–449why, one step back445446447class Client:448 def __init__(449 self,Auth (#65)Tom Christiebefore adoption5afa7dd
line 450why, one step back450 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 451–455why, one step back451 ssl: SSLConfig = DEFAULT_SSL_CONFIG,452 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,453 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,454 max_redirects: int = DEFAULT_MAX_REDIRECTS,455 dispatch: Dispatcher = None,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 456why, one step back456 backend: ConcurrencyBackend = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 457–458why, one step back457 ) -> None:458 self._client = AsyncClient(Auth (#65)Tom Christiebefore adoption5afa7dd
line 459why, one step back459 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 460–464why, one step back460 ssl=ssl,461 timeout=timeout,462 pool_limits=pool_limits,463 max_redirects=max_redirects,464 dispatch=dispatch,Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 465why, one step back465 backend=backend,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 466–476why, one step back466 )467 self._loop = asyncio.new_event_loop()468469 def request(470 self,471 method: str,472 url: URLTypes,473 *,474 data: RequestData = b"",475 query_params: QueryParamTypes = None,476 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 477why, one step back477 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 478why, one step back478 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 479why, one step back479 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 480–484why, one step back480 allow_redirects: bool = True,481 ssl: SSLConfig = None,482 timeout: TimeoutConfig = None,483 ) -> SyncResponse:484 request = Request(Initial pass at cookie supportTom Christiebefore adoption4a3325f
lines 485–490why, one step back485 method,486 url,487 data=data,488 query_params=query_params,489 headers=headers,490 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 491–495why, one step back491 )492 self.prepare_request(request)493 response = self.send(494 request,495 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 496why, one step back496 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 497–508why, one step back497 allow_redirects=allow_redirects,498 ssl=ssl,499 timeout=timeout,500 )501 return response502503 def get(504 self,505 url: URLTypes,506 *,507 query_params: QueryParamTypes = None,508 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 509why, one step back509 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 510why, one step back510 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 511why, one step back511 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 512–519why, one step back512 allow_redirects: bool = True,513 ssl: SSLConfig = None,514 timeout: TimeoutConfig = None,515 ) -> SyncResponse:516 return self.request(517 "GET",518 url,519 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 520why, one step back520 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 521why, one step back521 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 522why, one step back522 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 523–533why, one step back523 allow_redirects=allow_redirects,524 ssl=ssl,525 timeout=timeout,526 )527528 def options(529 self,530 url: URLTypes,531 *,532 query_params: QueryParamTypes = None,533 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 534why, one step back534 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 535why, one step back535 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 536why, one step back536 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 537–544why, one step back537 allow_redirects: bool = True,538 ssl: SSLConfig = None,539 timeout: TimeoutConfig = None,540 ) -> SyncResponse:541 return self.request(542 "OPTIONS",543 url,544 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 545why, one step back545 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 546why, one step back546 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 547why, one step back547 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 548–558why, one step back548 allow_redirects=allow_redirects,549 ssl=ssl,550 timeout=timeout,551 )552553 def head(554 self,555 url: URLTypes,556 *,557 query_params: QueryParamTypes = None,558 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 559why, one step back559 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 560why, one step back560 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 561why, one step back561 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 562–569why, one step back562 allow_redirects: bool = False, # Note: Differs to usual default.563 ssl: SSLConfig = None,564 timeout: TimeoutConfig = None,565 ) -> SyncResponse:566 return self.request(567 "HEAD",568 url,569 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 570why, one step back570 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 571why, one step back571 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 572why, one step back572 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 573–584why, one step back573 allow_redirects=allow_redirects,574 ssl=ssl,575 timeout=timeout,576 )577578 def post(579 self,580 url: URLTypes,581 *,582 data: RequestData = b"",583 query_params: QueryParamTypes = None,584 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 585why, one step back585 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 586why, one step back586 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 587why, one step back587 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 588–596why, one step back588 allow_redirects: bool = True,589 ssl: SSLConfig = None,590 timeout: TimeoutConfig = None,591 ) -> SyncResponse:592 return self.request(593 "POST",594 url,595 data=data,596 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 597why, one step back597 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 598why, one step back598 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 599why, one step back599 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 600–611why, one step back600 allow_redirects=allow_redirects,601 ssl=ssl,602 timeout=timeout,603 )604605 def put(606 self,607 url: URLTypes,608 *,609 data: RequestData = b"",610 query_params: QueryParamTypes = None,611 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 612why, one step back612 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 613why, one step back613 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 614why, one step back614 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 615–623why, one step back615 allow_redirects: bool = True,616 ssl: SSLConfig = None,617 timeout: TimeoutConfig = None,618 ) -> SyncResponse:619 return self.request(620 "PUT",621 url,622 data=data,623 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 624why, one step back624 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 625why, one step back625 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 626why, one step back626 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 627–638why, one step back627 allow_redirects=allow_redirects,628 ssl=ssl,629 timeout=timeout,630 )631632 def patch(633 self,634 url: URLTypes,635 *,636 data: RequestData = b"",637 query_params: QueryParamTypes = None,638 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 639why, one step back639 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 640why, one step back640 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 641why, one step back641 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 642–650why, one step back642 allow_redirects: bool = True,643 ssl: SSLConfig = None,644 timeout: TimeoutConfig = None,645 ) -> SyncResponse:646 return self.request(647 "PATCH",648 url,649 data=data,650 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 651why, one step back651 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 652why, one step back652 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 653why, one step back653 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 654–665why, one step back654 allow_redirects=allow_redirects,655 ssl=ssl,656 timeout=timeout,657 )658659 def delete(660 self,661 url: URLTypes,662 *,663 data: RequestData = b"",664 query_params: QueryParamTypes = None,665 headers: HeaderTypes = None,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 666why, one step back666 cookies: CookieJar = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 667why, one step back667 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 668why, one step back668 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 669–677why, one step back669 allow_redirects: bool = True,670 ssl: SSLConfig = None,671 timeout: TimeoutConfig = None,672 ) -> SyncResponse:673 return self.request(674 "DELETE",675 url,676 data=data,677 headers=headers,Initial pass at cookie supportTom Christiebefore adoption4a3325f
line 678why, one step back678 cookies=cookies,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
line 679why, one step back679 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 680why, one step back680 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 681–693why, one step back681 allow_redirects=allow_redirects,682 ssl=ssl,683 timeout=timeout,684 )685686 def prepare_request(self, request: Request) -> None:687 self._client.prepare_request(request)688689 def send(690 self,691 request: Request,692 *,693 stream: bool = False,Auth (#65)Tom Christiebefore adoption5afa7dd
line 694why, one step back694 auth: AuthTypes = None,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 695–702why, one step back695 allow_redirects: bool = True,696 ssl: SSLConfig = None,697 timeout: TimeoutConfig = None,698 ) -> SyncResponse:699 response = self._loop.run_until_complete(700 self._client.send(701 request,702 stream=stream,Auth (#65)Tom Christiebefore adoption5afa7dd
line 703why, one step back703 auth=auth,Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0
lines 704–723why, one step back704 allow_redirects=allow_redirects,705 ssl=ssl,706 timeout=timeout,707 )708 )709 return SyncResponse(response, self._loop)710711 def close(self) -> None:712 self._loop.run_until_complete(self._client.close())713714 def __enter__(self) -> "Client":715 return self716717 def __exit__(718 self,719 exc_type: typing.Type[BaseException] = None,720 exc_value: BaseException = None,721 traceback: TracebackType = None,722 ) -> None:723 self.close()