Skip to content
Myr

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 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

line 2why, one step back
2import typing

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 3why, one step back
3from http.cookiejar import CookieJar

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 4–5why, one step back
4from types import TracebackType
5

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

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

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

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

line 21why, one step back
21 AuthTypes,

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

line 22why, one step back
22 Headers,

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

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

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

line 26why, one step back
26 RequestData,

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

line 27why, one step back
27 Response,

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

line 28why, one step back
28 SyncResponse,

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

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 32–33why, one step back
32
33

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

line 34why, one step back
34class AsyncClient:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 38–39why, one step back
38 ssl: SSLConfig = DEFAULT_SSL_CONFIG,
39 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

Truckin' onTom Christiebefore adoption6b60d95

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 44why, one step back
44 ):

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

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

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 51–52why, one step back
51 self.max_redirects = max_redirects
52 self.dispatch = dispatch

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 53–55why, one step back
53
54 async def get(55 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 57why, one step back
57 *,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 60why, one step back
60 cookies: CookieJar = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 68–69why, one step back
68 "GET",
69 url,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 75–77why, one step back
75 allow_redirects=allow_redirects,
76 ssl=ssl,77 timeout=timeout,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 78–79why, one step back
78 )
79

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 80–83why, one step back
80 async def options(
81 self,82 url: URLTypes,83 *,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 86why, one step back
86 cookies: CookieJar = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 89–95why, one step back
89 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 back
96 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 101–109why, one step back
101 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 back
110 query_params: QueryParamTypes = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 112why, one step back
112 cookies: CookieJar = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 115–121why, one step back
115 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 back
122 query_params=query_params,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 127–131why, one step back
127 allow_redirects=allow_redirects,
128 ssl=ssl,129 timeout=timeout,130 )131

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 132–133why, one step back
132 async def post(
133 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 135why, one step back
135 *,

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

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

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 139why, one step back
139 cookies: CookieJar = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

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

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 156–159why, one step back
156 ssl=ssl,
157 timeout=timeout,158 )159

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 160–163why, one step back
160 async def put(
161 self,162 url: URLTypes,163 *,

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

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

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 167why, one step back
167 cookies: CookieJar = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 170–176why, one step back
170 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 back
177 data=data,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 183–191why, one step back
183 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 back
192 data: RequestData = b"",

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 195why, one step back
195 cookies: CookieJar = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 198–204why, one step back
198 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 back
205 data=data,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 211–219why, one step back
211 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 back
220 data: RequestData = b"",

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

line 223why, one step back
223 cookies: CookieJar = None,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 226–232why, one step back
226 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 back
233 data=data,

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

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Initial pass at cookie supportTom Christiebefore adoption4a3325f

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 239–243why, one step back
239 allow_redirects=allow_redirects,
240 ssl=ssl,241 timeout=timeout,242 )243

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

lines 244–251why, one step back
244 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 back
252 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 255–259why, one step back
255 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 back
260 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 back
266 )
267 self.prepare_request(request)268 response = await self.send(269 request,270 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 272–277why, one step back
272 allow_redirects=allow_redirects,
273 ssl=ssl,274 timeout=timeout,275 )276 return response277

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 280–285why, one step back
280
281 async def send(282 self,283 request: Request,284 *,285 stream: bool = False,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 287–288why, one step back
287 ssl: SSLConfig = None,
288 timeout: TimeoutConfig = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

lines 322–323why, one step back
322 if history is None:
323 history = []

Finessing interfaceTom Christiebefore adoptiond7619a9

line 324why, one step back
324

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

lines 325–331why, one step back
325 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 back
332

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

lines 333–339why, one step back
333 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 break

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 340why, one step back
340

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

lines 341–347why, one step back
341 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 back
348 response = await self.send_handling_redirects(

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

lines 349–377why, one step back
349 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.4

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

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

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

lines 384–387why, one step back
384 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 back
388 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 back
389 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.content431

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 434why, one step back
434

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 436–444why, one step back
436 return self
437438 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 back
445
446447class Client:448 def __init__(449 self,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 451–455why, one step back
451 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 back
456 backend: ConcurrencyBackend = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 460–464why, one step back
460 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 back
465 backend=backend,

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

lines 466–476why, one step back
466 )
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 back
477 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 480–484why, one step back
480 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 back
485 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 back
491 )
492 self.prepare_request(request)493 response = self.send(494 request,495 stream=stream,

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 497–508why, one step back
497 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 back
509 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 512–519why, one step back
512 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 back
520 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 523–533why, one step back
523 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 back
534 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 537–544why, one step back
537 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 back
545 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 548–558why, one step back
548 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 back
559 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 562–569why, one step back
562 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 back
570 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 573–584why, one step back
573 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 back
585 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 588–596why, one step back
588 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 back
597 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 600–611why, one step back
600 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 back
612 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 615–623why, one step back
615 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 back
624 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 627–638why, one step back
627 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 back
639 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 642–650why, one step back
642 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 back
651 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 654–665why, one step back
654 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 back
666 cookies: CookieJar = None,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 669–677why, one step back
669 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 back
678 cookies=cookies,

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

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

Auth (#65)Tom Christiebefore adoption5afa7dd

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

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

lines 681–693why, one step back
681 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 back
694 auth: AuthTypes = None,

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

lines 695–702why, one step back
695 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 back
703 auth=auth,

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

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