Skip to content
Myr

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 file

Take it

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 1why, one step back
1import asyncio

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 2–4why, one step back
2import typing
3from types import TracebackType4

Auth (#65)Tom Christiebefore adoption5afa7dd

line 5why, one step back
5from .auth import HTTPBasicAuth

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 6–15why, one step back
6from .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 ConnectionPool

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 16why, one step back
16from .exceptions import RedirectBodyUnavailable, RedirectLoop, TooManyRedirects

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 17why, one step back
17from .interfaces import ConcurrencyBackend, Dispatcher

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

lines 18–19why, one step back
18from .models import (
19 URL,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 20why, one step back
20 AuthTypes,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 21–22why, one step back
21 Cookies,
22 CookieTypes,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 23why, one step back
23 Headers,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

lines 24–26why, one step back
24 HeaderTypes,
25 QueryParamTypes,26 Request,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

line 27why, one step back
27 RequestData,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 28why, one step back
28 Response,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 29why, one step back
29 SyncResponse,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

lines 30–31why, one step back
30 URLTypes,
31)

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 32why, one step back
32from .status_codes import codes

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 33–34why, one step back
33
34

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 35why, one step back
35class AsyncClient:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 36–37why, one step back
36 def __init__(
37 self,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 38why, one step back
38 auth: AuthTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 39why, one step back
39 cookies: CookieTypes = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 40–41why, one step back
40 ssl: SSLConfig = DEFAULT_SSL_CONFIG,
41 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

Truckin' onTom Christiebefore adoption6b60d95

line 42why, one step back
42 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 43why, one step back
43 max_redirects: int = DEFAULT_MAX_REDIRECTS,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 44why, one step back
44 dispatch: Dispatcher = None,

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 45why, one step back
45 backend: ConcurrencyBackend = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 46why, one step back
46 ):

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 47why, one step back
47 if dispatch is None:

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

lines 48–50why, one step back
48 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 back
51

Auth (#65)Tom Christiebefore adoption5afa7dd

line 52why, one step back
52 self.auth = auth

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 53why, one step back
53 self.cookies = Cookies(cookies)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 54–55why, one step back
54 self.max_redirects = max_redirects
55 self.dispatch = dispatch

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 56–58why, one step back
56
57 async def get(58 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 59why, one step back
59 url: URLTypes,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 60why, one step back
60 *,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 61why, one step back
61 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 62why, one step back
62 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 63why, one step back
63 cookies: CookieTypes = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 64why, one step back
64 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 65why, one step back
65 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 66–68why, one step back
66 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 back
69 ) -> Response:
70 return await self.request(

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 71–72why, one step back
71 "GET",
72 url,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 73why, one step back
73 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 74why, one step back
74 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 75why, one step back
75 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 76why, one step back
76 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 77why, one step back
77 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 78–80why, one step back
78 allow_redirects=allow_redirects,
79 ssl=ssl,80 timeout=timeout,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 81–82why, one step back
81 )
82

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 83–86why, one step back
83 async def options(
84 self,85 url: URLTypes,86 *,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 87why, one step back
87 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 88why, one step back
88 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 89why, one step back
89 cookies: CookieTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 90why, one step back
90 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 91why, one step back
91 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 92–98why, one step back
92 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 back
99 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 100why, one step back
100 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 101why, one step back
101 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 102why, one step back
102 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 103why, one step back
103 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 104–112why, one step back
104 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 back
113 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 114why, one step back
114 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 115why, one step back
115 cookies: CookieTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 116why, one step back
116 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 117why, one step back
117 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 118–124why, one step back
118 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 back
125 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 126why, one step back
126 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 127why, one step back
127 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 128why, one step back
128 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 129why, one step back
129 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 130–134why, one step back
130 allow_redirects=allow_redirects,
131 ssl=ssl,132 timeout=timeout,133 )134

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 135–136why, one step back
135 async def post(
136 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 137why, one step back
137 url: URLTypes,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 138why, one step back
138 *,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

line 139why, one step back
139 data: RequestData = b"",

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 140why, one step back
140 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 141why, one step back
141 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 142why, one step back
142 cookies: CookieTypes = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 143why, one step back
143 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 144why, one step back
144 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 145–147why, one step back
145 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 back
148 ) -> Response:
149 return await self.request(150 "POST",151 url,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

line 152why, one step back
152 data=data,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 153why, one step back
153 query_params=query_params,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 154why, one step back
154 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 155why, one step back
155 cookies=cookies,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 156why, one step back
156 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 157why, one step back
157 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 158why, one step back
158 allow_redirects=allow_redirects,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 159–162why, one step back
159 ssl=ssl,
160 timeout=timeout,161 )162

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 163–166why, one step back
163 async def put(
164 self,165 url: URLTypes,166 *,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

line 167why, one step back
167 data: RequestData = b"",

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 168why, one step back
168 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 169why, one step back
169 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 170why, one step back
170 cookies: CookieTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 171why, one step back
171 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 172why, one step back
172 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 173–179why, one step back
173 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 back
180 data=data,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 181why, one step back
181 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 182why, one step back
182 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 183why, one step back
183 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 184why, one step back
184 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 185why, one step back
185 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 186–194why, one step back
186 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 back
195 data: RequestData = b"",

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 196why, one step back
196 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 197why, one step back
197 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 198why, one step back
198 cookies: CookieTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 199why, one step back
199 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 200why, one step back
200 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 201–207why, one step back
201 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 back
208 data=data,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 209why, one step back
209 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 210why, one step back
210 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 211why, one step back
211 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 212why, one step back
212 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 213why, one step back
213 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 214–222why, one step back
214 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 back
223 data: RequestData = b"",

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 224why, one step back
224 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 225why, one step back
225 headers: HeaderTypes = None,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 226why, one step back
226 cookies: CookieTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 227why, one step back
227 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 228why, one step back
228 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 229–235why, one step back
229 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 back
236 data=data,

Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425

line 237why, one step back
237 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 238why, one step back
238 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 239why, one step back
239 cookies=cookies,

Finessing interfaceTom Christiebefore adoptiond7619a9

line 240why, one step back
240 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 241why, one step back
241 auth=auth,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 242–246why, one step back
242 allow_redirects=allow_redirects,
243 ssl=ssl,244 timeout=timeout,245 )246

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 247–254why, one step back
247 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 back
255 cookies: CookieTypes = None,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 256why, one step back
256 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 257why, one step back
257 auth: AuthTypes = None,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 258–262why, one step back
258 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 back
263 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 back
269 )
270 self.prepare_request(request)271 response = await self.send(272 request,273 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 274why, one step back
274 auth=auth,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 275–280why, one step back
275 allow_redirects=allow_redirects,
276 ssl=ssl,277 timeout=timeout,278 )279 return response280

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 281why, one step back
281 def prepare_request(self, request: Request) -> None:

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 282why, one step back
282 request.prepare()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 283why, one step back
283

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 284–290why, one step back
284 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 cookies290

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 291–295why, one step back
291 async def send(
292 self,293 request: Request,294 *,295 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 296why, one step back
296 auth: AuthTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 297–298why, one step back
297 ssl: SSLConfig = None,
298 timeout: TimeoutConfig = None,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 299why, one step back
299 allow_redirects: bool = True,

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 300–329why, one step back
300 ) -> 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 back
330 history: typing.List[Response] = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 331why, one step back
331 ) -> Response:

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 332–333why, one step back
332 if history is None:
333 history = []

Finessing interfaceTom Christiebefore adoptiond7619a9

line 334why, one step back
334

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 335–341why, one step back
335 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 back
342

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 343–346why, one step back
343 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 back
347 self.cookies.extract_cookies(response)

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 348–350why, one step back
348 history = [response] + history
349 if not response.is_redirect:350 break

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 351why, one step back
351

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 352–358why, one step back
352 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 back
359 response = await self.send_handling_redirects(

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 360–378why, one step back
360 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 back
379 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 back
381
382 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.4

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

line 390why, one step back
390 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 back
391 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 back
395 if response.status_code == codes.FOUND and method != "HEAD":

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 396–399why, one step back
396 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 back
400 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 back
401 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.content443

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 444why, one step back
444 async def close(self) -> None:

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 445why, one step back
445 await self.dispatch.close()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 446why, one step back
446

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 447why, one step back
447 async def __aenter__(self) -> "AsyncClient":

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 448–456why, one step back
448 return self
449450 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 back
457
458459class Client:460 def __init__(461 self,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 462why, one step back
462 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 463–467why, one step back
463 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 back
468 backend: ConcurrencyBackend = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 469–470why, one step back
469 ) -> None:
470 self._client = AsyncClient(

Auth (#65)Tom Christiebefore adoption5afa7dd

line 471why, one step back
471 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 472–476why, one step back
472 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 back
477 backend=backend,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 478–480why, one step back
478 )
479 self._loop = asyncio.new_event_loop()480

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 481–484why, one step back
481 @property
482 def cookies(self) -> Cookies:483 return self._client.cookies484

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 485–492why, one step back
485 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 back
493 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 494why, one step back
494 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 495why, one step back
495 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 496–500why, one step back
496 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 back
501 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 back
507 )
508 self.prepare_request(request)509 response = self.send(510 request,511 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 512why, one step back
512 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 513–524why, one step back
513 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 back
525 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 526why, one step back
526 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 527why, one step back
527 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 528–535why, one step back
528 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 back
536 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 537why, one step back
537 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 538why, one step back
538 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 539–549why, one step back
539 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 back
550 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 551why, one step back
551 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 552why, one step back
552 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 553–560why, one step back
553 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 back
561 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 562why, one step back
562 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 563why, one step back
563 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 564–574why, one step back
564 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 back
575 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 576why, one step back
576 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 577why, one step back
577 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 578–585why, one step back
578 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 back
586 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 587why, one step back
587 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 588why, one step back
588 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 589–600why, one step back
589 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 back
601 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 602why, one step back
602 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 603why, one step back
603 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 604–612why, one step back
604 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 back
613 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 614why, one step back
614 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 615why, one step back
615 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 616–627why, one step back
616 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 back
628 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 629why, one step back
629 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 630why, one step back
630 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 631–639why, one step back
631 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 back
640 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 641why, one step back
641 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 642why, one step back
642 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 643–654why, one step back
643 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 back
655 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 656why, one step back
656 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 657why, one step back
657 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 658–666why, one step back
658 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 back
667 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 668why, one step back
668 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 669why, one step back
669 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 670–681why, one step back
670 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 back
682 cookies: CookieTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 683why, one step back
683 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 684why, one step back
684 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 685–693why, one step back
685 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 back
694 cookies=cookies,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

line 695why, one step back
695 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

line 696why, one step back
696 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 697–709why, one step back
697 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 back
710 auth: AuthTypes = None,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 711–718why, one step back
711 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 back
719 auth=auth,

Merge pull request #58 from encode/sync-by-defaultTom Christiebefore adoptionb6c27f0

lines 720–739why, one step back
720 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()