Skip to content
Myr

client.py

This file

Path

httpcore/client.py

743 lines · 20.7 KiB · Python

Last changed by

query_params -> paramsTom Christiebefore adoptionbb70377

History of this 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 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 61why, one step back
61 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 73why, one step back
73 params=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 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 87why, one step back
87 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 99why, one step back
99 params=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 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 113why, one step back
113 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 125why, one step back
125 params=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"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 140why, one step back
140 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 153why, one step back
153 params=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"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 168why, one step back
168 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 181why, one step back
181 params=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"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 196why, one step back
196 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 209why, one step back
209 params=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"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 224why, one step back
224 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,

query_params -> paramsTom Christiebefore adoptionbb70377

line 237why, one step back
237 params=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–252why, one step back
247 async def request(
248 self,249 method: str,250 url: URLTypes,251 *,252 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 253why, one step back
253 params: QueryParamTypes = None,

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

line 254why, one step back
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–265why, one step back
263 method,
264 url,265 data=data,

query_params -> paramsTom Christiebefore adoptionbb70377

line 266why, one step back
266 params=params,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 267–268why, one step back
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

query_params -> paramsTom Christiebefore adoptionbb70377

lines 284–286why, one step back
284 def merge_cookies(
285 self, cookies: CookieTypes = None286 ) -> typing.Optional[CookieTypes]:

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 287–292why, one step back
287 if cookies or self.cookies:
288 merged_cookies = Cookies(self.cookies)289 merged_cookies.update(cookies)290 return merged_cookies291 return cookies292

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 299–300why, one step back
299 ssl: SSLConfig = None,
300 timeout: TimeoutConfig = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

lines 302–331why, one step back
302 ) -> Response:
303 if auth is None:304 auth = self.auth305306 url = request.url307 if auth is None and (url.username or url.password):308 auth = HTTPBasicAuth(username=url.username, password=url.password)309310 if auth is not None:311 if isinstance(auth, tuple):312 auth = HTTPBasicAuth(username=auth[0], password=auth[1])313 request = auth(request)314315 response = await self.send_handling_redirects(316 request,317 stream=stream,318 ssl=ssl,319 timeout=timeout,320 allow_redirects=allow_redirects,321 )322 return response323324 async def send_handling_redirects(325 self,326 request: Request,327 *,328 stream: bool = False,329 ssl: SSLConfig = None,330 timeout: TimeoutConfig = None,331 allow_redirects: bool = True,

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

line 332why, one step back
332 history: typing.List[Response] = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

lines 334–335why, one step back
334 if history is None:
335 history = []

Finessing interfaceTom Christiebefore adoptiond7619a9

line 336why, one step back
336

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

lines 337–343why, one step back
337 while True:
338 # We perform these checks here, so that calls to `response.next()`339 # will raise redirect errors if appropriate.340 if len(history) > self.max_redirects:341 raise TooManyRedirects()342 if request.url in [response.url for response in history]:343 raise RedirectLoop()

Finessing interfaceTom Christiebefore adoptiond7619a9

line 344why, one step back
344

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

lines 345–348why, one step back
345 response = await self.dispatch.send(
346 request, stream=stream, ssl=ssl, timeout=timeout347 )348 response.history = list(history)

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 349why, one step back
349 self.cookies.extract_cookies(response)

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 353why, one step back
353

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

lines 354–360why, one step back
354 if allow_redirects:
355 request = self.build_redirect_request(request, response)356 else:357358 async def send_next() -> Response:359 nonlocal request, response, ssl, allow_redirects, timeout, history360 request = self.build_redirect_request(request, response)

Auth (#65)Tom Christiebefore adoption5afa7dd

line 361why, one step back
361 response = await self.send_handling_redirects(

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

lines 362–380why, one step back
362 request,
363 stream=stream,364 allow_redirects=allow_redirects,365 ssl=ssl,366 timeout=timeout,367 history=history,368 )369 return response370371 response.next = send_next # type: ignore372 break373374 return response375376 def build_redirect_request(self, request: Request, response: Response) -> Request:377 method = self.redirect_method(request, response)378 url = self.redirect_url(request, response)379 headers = self.redirect_headers(request, url)380 content = self.redirect_content(request, method)

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

line 381why, one step back
381 cookies = self.merge_cookies(request.cookies)

query_params -> paramsTom Christiebefore adoptionbb70377

lines 382–384why, one step back
382 return Request(
383 method=method, url=url, headers=headers, data=content, cookies=cookies384 )

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

lines 385–393why, one step back
385
386 def redirect_method(self, request: Request, response: Response) -> str:387 """388 When being redirected we may want to change the method of the request389 based on certain specs or browser behavior.390 """391 method = request.method392393 # https://tools.ietf.org/html/rfc7231#section-6.4.4

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

line 394why, one step back
394 if response.status_code == codes.SEE_OTHER and method != "HEAD":

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

lines 395–398why, one step back
395 method = "GET"
396397 # Do what the browsers do, despite standards...398 # Turn 302s into GETs.

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

line 399why, one step back
399 if response.status_code == codes.FOUND and method != "HEAD":

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

lines 400–403why, one step back
400 method = "GET"
401402 # If a POST is responded to with a 301, turn it into a GET.403 # This bizarre behaviour is explained in 'requests' issue 1704.

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

line 404why, one step back
404 if response.status_code == codes.MOVED_PERMANENTLY and method == "POST":

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

lines 405–447why, one step back
405 method = "GET"
406407 return method408409 def redirect_url(self, request: Request, response: Response) -> URL:410 """411 Return the URL for the redirect to follow.412 """413 location = response.headers["Location"]414415 url = URL(location, allow_relative=True)416417 # Facilitate relative 'Location' headers, as allowed by RFC 7231.418 # (e.g. '/path/to/resource' instead of 'http://domain.tld/path/to/resource')419 if url.is_relative_url:420 url = url.resolve_with(request.url)421422 # Attach previous fragment if needed (RFC 7231 7.1.2)423 if request.url.fragment and not url.fragment:424 url = url.copy_with(fragment=request.url.fragment)425426 return url427428 def redirect_headers(self, request: Request, url: URL) -> Headers:429 """430 Strip Authorization headers when responses are redirected away from431 the origin.432 """433 headers = Headers(request.headers)434 if url.origin != request.url.origin:435 del headers["Authorization"]436 return headers437438 def redirect_content(self, request: Request, method: str) -> bytes:439 """440 Return the body that should be used for the redirect request.441 """442 if method != request.method and method == "GET":443 return b""444 if request.is_streaming:445 raise RedirectBodyUnavailable()446 return request.content447

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 450why, one step back
450

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 452–460why, one step back
452 return self
453454 async def __aexit__(455 self,456 exc_type: typing.Type[BaseException] = None,457 exc_value: BaseException = None,458 traceback: TracebackType = None,459 ) -> None:460 await self.close()

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

lines 461–465why, one step back
461
462463class Client:464 def __init__(465 self,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 467–471why, one step back
467 ssl: SSLConfig = DEFAULT_SSL_CONFIG,
468 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,469 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,470 max_redirects: int = DEFAULT_MAX_REDIRECTS,471 dispatch: Dispatcher = None,

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

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 476–480why, one step back
476 ssl=ssl,
477 timeout=timeout,478 pool_limits=pool_limits,479 max_redirects=max_redirects,480 dispatch=dispatch,

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

line 481why, one step back
481 backend=backend,

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

lines 482–484why, one step back
482 )
483 self._loop = asyncio.new_event_loop()484

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 485–488why, one step back
485 @property
486 def cookies(self) -> Cookies:487 return self._client.cookies488

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

lines 489–494why, one step back
489 def request(
490 self,491 method: str,492 url: URLTypes,493 *,494 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 495why, one step back
495 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 500–504why, one step back
500 allow_redirects: bool = True,
501 ssl: SSLConfig = None,502 timeout: TimeoutConfig = None,503 ) -> SyncResponse:504 request = Request(

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 505–507why, one step back
505 method,
506 url,507 data=data,

query_params -> paramsTom Christiebefore adoptionbb70377

line 508why, one step back
508 params=params,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

lines 509–510why, one step back
509 headers=headers,
510 cookies=self._client.merge_cookies(cookies),

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

lines 511–515why, one step back
511 )
512 self.prepare_request(request)513 response = self.send(514 request,515 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 517–526why, one step back
517 allow_redirects=allow_redirects,
518 ssl=ssl,519 timeout=timeout,520 )521 return response522523 def get(524 self,525 url: URLTypes,526 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 527why, one step back
527 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 532–539why, one step back
532 allow_redirects: bool = True,
533 ssl: SSLConfig = None,534 timeout: TimeoutConfig = None,535 ) -> SyncResponse:536 return self.request(537 "GET",538 url,539 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 543–551why, one step back
543 allow_redirects=allow_redirects,
544 ssl=ssl,545 timeout=timeout,546 )547548 def options(549 self,550 url: URLTypes,551 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 552why, one step back
552 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 557–564why, one step back
557 allow_redirects: bool = True,
558 ssl: SSLConfig = None,559 timeout: TimeoutConfig = None,560 ) -> SyncResponse:561 return self.request(562 "OPTIONS",563 url,564 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 568–576why, one step back
568 allow_redirects=allow_redirects,
569 ssl=ssl,570 timeout=timeout,571 )572573 def head(574 self,575 url: URLTypes,576 *,

query_params -> paramsTom Christiebefore adoptionbb70377

line 577why, one step back
577 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 582–589why, one step back
582 allow_redirects: bool = False, #  Note: Differs to usual default.
583 ssl: SSLConfig = None,584 timeout: TimeoutConfig = None,585 ) -> SyncResponse:586 return self.request(587 "HEAD",588 url,589 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 593–602why, one step back
593 allow_redirects=allow_redirects,
594 ssl=ssl,595 timeout=timeout,596 )597598 def post(599 self,600 url: URLTypes,601 *,602 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 603why, one step back
603 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 608–616why, one step back
608 allow_redirects: bool = True,
609 ssl: SSLConfig = None,610 timeout: TimeoutConfig = None,611 ) -> SyncResponse:612 return self.request(613 "POST",614 url,615 data=data,616 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 620–629why, one step back
620 allow_redirects=allow_redirects,
621 ssl=ssl,622 timeout=timeout,623 )624625 def put(626 self,627 url: URLTypes,628 *,629 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 630why, one step back
630 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 635–643why, one step back
635 allow_redirects: bool = True,
636 ssl: SSLConfig = None,637 timeout: TimeoutConfig = None,638 ) -> SyncResponse:639 return self.request(640 "PUT",641 url,642 data=data,643 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 647–656why, one step back
647 allow_redirects=allow_redirects,
648 ssl=ssl,649 timeout=timeout,650 )651652 def patch(653 self,654 url: URLTypes,655 *,656 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 657why, one step back
657 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 662–670why, one step back
662 allow_redirects: bool = True,
663 ssl: SSLConfig = None,664 timeout: TimeoutConfig = None,665 ) -> SyncResponse:666 return self.request(667 "PATCH",668 url,669 data=data,670 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 674–683why, one step back
674 allow_redirects=allow_redirects,
675 ssl=ssl,676 timeout=timeout,677 )678679 def delete(680 self,681 url: URLTypes,682 *,683 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 684why, one step back
684 params: QueryParamTypes = None,

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

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

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 689–697why, one step back
689 allow_redirects: bool = True,
690 ssl: SSLConfig = None,691 timeout: TimeoutConfig = None,692 ) -> SyncResponse:693 return self.request(694 "DELETE",695 url,696 data=data,697 headers=headers,

Cookie support (#73)Tom Christiebefore adoptionfeae0a8

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 701–713why, one step back
701 allow_redirects=allow_redirects,
702 ssl=ssl,703 timeout=timeout,704 )705706 def prepare_request(self, request: Request) -> None:707 self._client.prepare_request(request)708709 def send(710 self,711 request: Request,712 *,713 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 715–722why, one step back
715 allow_redirects: bool = True,
716 ssl: SSLConfig = None,717 timeout: TimeoutConfig = None,718 ) -> SyncResponse:719 response = self._loop.run_until_complete(720 self._client.send(721 request,722 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 724–743why, one step back
724 allow_redirects=allow_redirects,
725 ssl=ssl,726 timeout=timeout,727 )728 )729 return SyncResponse(response, self._loop)730731 def close(self) -> None:732 self._loop.run_until_complete(self._client.close())733734 def __enter__(self) -> "Client":735 return self736737 def __exit__(738 self,739 exc_type: typing.Type[BaseException] = None,740 exc_value: BaseException = None,741 traceback: TracebackType = None,742 ) -> None:743 self.close()