Skip to content
Myr

client.py

This file

Path

httpcore/client.py

271 lines · 7.3 KiB · Python

Last changed by

Add query_params supportTom Christiebefore adoptionfaededa

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

Add query_params supportTom Christiebefore adoptionfaededa

lines 18–26why, one step back
18from .models import (
19 URL,20 ByteOrByteStream,21 HeaderTypes,22 QueryParamTypes,23 Request,24 Response,25 URLTypes,26)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 27–33why, one step back
27
2829class Client:30 def __init__(31 self,32 ssl: SSLConfig = DEFAULT_SSL_CONFIG,33 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,

Truckin' onTom Christiebefore adoption6b60d95

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 35–36why, one step back
35 max_redirects: int = DEFAULT_MAX_REDIRECTS,
36 ):

Truckin' onTom Christiebefore adoption6b60d95

lines 37–39why, one step back
37 connection_pool = ConnectionPool(
38 ssl=ssl, timeout=timeout, pool_limits=pool_limits39 )

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 40–49why, one step back
40 cookie_adapter = CookieAdapter(dispatch=connection_pool)
41 auth_adapter = AuthenticationAdapter(dispatch=cookie_adapter)42 redirect_adapter = RedirectAdapter(43 dispatch=auth_adapter, max_redirects=max_redirects44 )45 self.adapter = EnvironmentAdapter(dispatch=redirect_adapter)4647 async def request(48 self,49 method: str,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 51why, one step back
51 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 55–56why, one step back
55 stream: bool = False,
56 allow_redirects: bool = True,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 57–58why, one step back
57 ssl: SSLConfig = None,
58 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Add query_params supportTom Christiebefore adoptionfaededa

lines 60–62why, one step back
60 request = Request(
61 method, url, query_params=query_params, headers=headers, content=content62 )

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 63–74why, one step back
63 self.prepare_request(request)
64 response = await self.send(65 request,66 stream=stream,67 allow_redirects=allow_redirects,68 ssl=ssl,69 timeout=timeout,70 )71 return response7273 async def get(74 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 76why, one step back
76 *,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 80–82why, one step back
80 allow_redirects: bool = True,
81 ssl: SSLConfig = None,82 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 85–86why, one step back
85 "GET",
86 url,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 88–92why, one step back
88 headers=headers,
89 stream=stream,90 allow_redirects=allow_redirects,91 ssl=ssl,92 timeout=timeout,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 93–94why, one step back
93 )
94

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 95–98why, one step back
95 async def options(
96 self,97 url: URLTypes,98 *,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 100–108why, one step back
100 headers: HeaderTypes = None,
101 stream: bool = False,102 allow_redirects: bool = True,103 ssl: SSLConfig = None,104 timeout: TimeoutConfig = None,105 ) -> Response:106 return await self.request(107 "OPTIONS",108 url,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 110–120why, one step back
110 headers=headers,
111 stream=stream,112 allow_redirects=allow_redirects,113 ssl=ssl,114 timeout=timeout,115 )116117 async def head(118 self,119 url: URLTypes,120 *,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 122–130why, one step back
122 headers: HeaderTypes = None,
123 stream: bool = False,124 allow_redirects: bool = False, #  Note: Differs to usual default.125 ssl: SSLConfig = None,126 timeout: TimeoutConfig = None,127 ) -> Response:128 return await self.request(129 "HEAD",130 url,

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 132–138why, one step back
132 headers=headers,
133 stream=stream,134 allow_redirects=allow_redirects,135 ssl=ssl,136 timeout=timeout,137 )138

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 139–140why, one step back
139 async def post(
140 self,

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 142why, one step back
142 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 147–149why, one step back
147 allow_redirects: bool = True,
148 ssl: SSLConfig = None,149 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 150–153why, one step back
150 ) -> Response:
151 return await self.request(152 "POST",153 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 156–157why, one step back
156 headers=headers,
157 stream=stream,

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

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

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

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 180–190why, one step back
180 headers=headers,
181 stream=stream,182 allow_redirects=allow_redirects,183 ssl=ssl,184 timeout=timeout,185 )186187 async def patch(188 self,189 url: URLTypes,190 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

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 "PATCH",201 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 204–214why, one step back
204 headers=headers,
205 stream=stream,206 allow_redirects=allow_redirects,207 ssl=ssl,208 timeout=timeout,209 )210211 async def delete(212 self,213 url: URLTypes,214 *,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 217–225why, one step back
217 headers: HeaderTypes = None,
218 stream: bool = False,219 allow_redirects: bool = True,220 ssl: SSLConfig = None,221 timeout: TimeoutConfig = None,222 ) -> Response:223 return await self.request(224 "DELETE",225 url,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Add query_params supportTom Christiebefore adoptionfaededa

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 228–234why, one step back
228 headers=headers,
229 stream=stream,230 allow_redirects=allow_redirects,231 ssl=ssl,232 timeout=timeout,233 )234

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 235–243why, one step back
235 def prepare_request(self, request: Request) -> None:
236 self.adapter.prepare_request(request)237238 async def send(239 self,240 request: Request,241 *,242 stream: bool = False,243 allow_redirects: bool = True,

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 244–245why, one step back
244 ssl: SSLConfig = None,
245 timeout: TimeoutConfig = None,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

Finessing interfaceTom Christiebefore adoptiond7619a9

lines 247–251why, one step back
247 options = {
248 "stream": stream,249 "allow_redirects": allow_redirects,250 } # type: typing.Dict[str, typing.Any]251

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 252–255why, one step back
252 if ssl is not None:
253 options["ssl"] = ssl254 if timeout is not None:255 options["timeout"] = timeout

Finessing interfaceTom Christiebefore adoptiond7619a9

line 256why, one step back
256

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 257–271why, one step back
257 return await self.adapter.send(request, **options)
258259 async def close(self) -> None:260 await self.adapter.close()261262 async def __aenter__(self) -> "Client":263 return self264265 async def __aexit__(266 self,267 exc_type: typing.Type[BaseException] = None,268 exc_value: BaseException = None,269 traceback: TracebackType = None,270 ) -> None:271 await self.close()