Skip to content
Myr

client.py

This file

Path

httpcore/client.py

246 lines · 6.6 KiB · Python

Last changed by

Truckin' onTom Christiebefore adoption6b60d95

History of this file

Take it

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 1–17why, one step back
1import typing
2from types import TracebackType34from .adapters.authentication import AuthenticationAdapter5from .adapters.cookies import CookieAdapter6from .adapters.environment import EnvironmentAdapter7from .adapters.redirects import RedirectAdapter8from .config import (9 DEFAULT_MAX_REDIRECTS,10 DEFAULT_POOL_LIMITS,11 DEFAULT_SSL_CONFIG,12 DEFAULT_TIMEOUT_CONFIG,13 PoolLimits,14 SSLConfig,15 TimeoutConfig,16)17from .dispatch.connection_pool import ConnectionPool

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 18why, one step back
18from .models import URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 19–25why, one step back
19
2021class Client:22 def __init__(23 self,24 ssl: SSLConfig = DEFAULT_SSL_CONFIG,25 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

Truckin' onTom Christiebefore adoption6b60d95

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 27–28why, one step back
27 max_redirects: int = DEFAULT_MAX_REDIRECTS,
28 ):

Truckin' onTom Christiebefore adoption6b60d95

lines 29–31why, one step back
29 connection_pool = ConnectionPool(
30 ssl=ssl, timeout=timeout, pool_limits=pool_limits31 )

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 32–41why, one step back
32 cookie_adapter = CookieAdapter(dispatch=connection_pool)
33 auth_adapter = AuthenticationAdapter(dispatch=cookie_adapter)34 redirect_adapter = RedirectAdapter(35 dispatch=auth_adapter, max_redirects=max_redirects36 )37 self.adapter = EnvironmentAdapter(dispatch=redirect_adapter)3839 async def request(40 self,41 method: str,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 43why, one step back
43 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 44why, one step back
44 content: ByteOrByteStream = b"",

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 46–47why, one step back
46 stream: bool = False,
47 allow_redirects: bool = True,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 48–49why, one step back
48 ssl: SSLConfig = None,
49 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 51why, one step back
51 request = Request(method, url, headers=headers, content=content)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 52–63why, one step back
52 self.prepare_request(request)
53 response = await self.send(54 request,55 stream=stream,56 allow_redirects=allow_redirects,57 ssl=ssl,58 timeout=timeout,59 )60 return response6162 async def get(63 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 65why, one step back
65 *,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 68–70why, one step back
68 allow_redirects: bool = True,
69 ssl: SSLConfig = None,70 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 71–72why, one step back
71 ) -> Response:
72 return await self.request(

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 73–79why, one step back
73 "GET",
74 url,75 headers=headers,76 stream=stream,77 allow_redirects=allow_redirects,78 ssl=ssl,79 timeout=timeout,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 82–121why, one step back
82 async def options(
83 self,84 url: URLTypes,85 *,86 headers: HeaderTypes = None,87 stream: bool = False,88 allow_redirects: bool = True,89 ssl: SSLConfig = None,90 timeout: TimeoutConfig = None,91 ) -> Response:92 return await self.request(93 "OPTIONS",94 url,95 headers=headers,96 stream=stream,97 allow_redirects=allow_redirects,98 ssl=ssl,99 timeout=timeout,100 )101102 async def head(103 self,104 url: URLTypes,105 *,106 headers: HeaderTypes = None,107 stream: bool = False,108 allow_redirects: bool = False, #  Note: Differs to usual default.109 ssl: SSLConfig = None,110 timeout: TimeoutConfig = None,111 ) -> Response:112 return await self.request(113 "HEAD",114 url,115 headers=headers,116 stream=stream,117 allow_redirects=allow_redirects,118 ssl=ssl,119 timeout=timeout,120 )121

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 122–123why, one step back
122 async def post(
123 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 125why, one step back
125 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 126why, one step back
126 content: ByteOrByteStream = b"",

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 129–131why, one step back
129 allow_redirects: bool = True,
130 ssl: SSLConfig = None,131 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 132–135why, one step back
132 ) -> Response:
133 return await self.request(134 "POST",135 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 136why, one step back
136 content=content,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 137–138why, one step back
137 headers=headers,
138 stream=stream,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 140–143why, one step back
140 ssl=ssl,
141 timeout=timeout,142 )143

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 144–147why, one step back
144 async def put(
145 self,146 url: URLTypes,147 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 148why, one step back
148 content: ByteOrByteStream = b"",

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 149–157why, one step back
149 headers: HeaderTypes = None,
150 stream: bool = False,151 allow_redirects: bool = True,152 ssl: SSLConfig = None,153 timeout: TimeoutConfig = None,154 ) -> Response:155 return await self.request(156 "PUT",157 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 159–169why, one step back
159 headers=headers,
160 stream=stream,161 allow_redirects=allow_redirects,162 ssl=ssl,163 timeout=timeout,164 )165166 async def patch(167 self,168 url: URLTypes,169 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 170why, one step back
170 content: ByteOrByteStream = b"",

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 171–179why, one step back
171 headers: HeaderTypes = None,
172 stream: bool = False,173 allow_redirects: bool = True,174 ssl: SSLConfig = None,175 timeout: TimeoutConfig = None,176 ) -> Response:177 return await self.request(178 "PATCH",179 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 181–191why, one step back
181 headers=headers,
182 stream=stream,183 allow_redirects=allow_redirects,184 ssl=ssl,185 timeout=timeout,186 )187188 async def delete(189 self,190 url: URLTypes,191 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 192why, one step back
192 content: ByteOrByteStream = b"",

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 193–201why, one step back
193 headers: HeaderTypes = None,
194 stream: bool = False,195 allow_redirects: bool = True,196 ssl: SSLConfig = None,197 timeout: TimeoutConfig = None,198 ) -> Response:199 return await self.request(200 "DELETE",201 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 202why, one step back
202 content=content,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 203–209why, one step back
203 headers=headers,
204 stream=stream,205 allow_redirects=allow_redirects,206 ssl=ssl,207 timeout=timeout,208 )209

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 210–218why, one step back
210 def prepare_request(self, request: Request) -> None:
211 self.adapter.prepare_request(request)212213 async def send(214 self,215 request: Request,216 *,217 stream: bool = False,218 allow_redirects: bool = True,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 219–220why, one step back
219 ssl: SSLConfig = None,
220 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 222–226why, one step back
222 options = {
223 "stream": stream,224 "allow_redirects": allow_redirects,225 } # type: typing.Dict[str, typing.Any]226

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 227–230why, one step back
227 if ssl is not None:
228 options["ssl"] = ssl229 if timeout is not None:230 options["timeout"] = timeout

Finessing interfaceTom Christiebefore adoptiond7619a9

line 231why, one step back
231

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 232–246why, one step back
232 return await self.adapter.send(request, **options)
233234 async def close(self) -> None:235 await self.adapter.close()236237 async def __aenter__(self) -> "Client":238 return self239240 async def __aexit__(241 self,242 exc_type: typing.Type[BaseException] = None,243 exc_value: BaseException = None,244 traceback: TracebackType = None,245 ) -> None:246 await self.close()