client.py
This file
Path
httpcore/client.py
271 lines · 7.3 KiB · Python
Last changed by
Merge pull request #42 from encode/query-paramsTom Christiebefore adoption3de0425
History of this fileTake it
1import typing2from 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 ConnectionPool18from .models import (19 URL,20 ByteOrByteStream,21 HeaderTypes,22 QueryParamTypes,23 Request,24 Response,25 URLTypes,26)272829class Client:30 def __init__(31 self,32 ssl: SSLConfig = DEFAULT_SSL_CONFIG,33 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,34 pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,35 max_redirects: int = DEFAULT_MAX_REDIRECTS,36 ):37 connection_pool = ConnectionPool(38 ssl=ssl, timeout=timeout, pool_limits=pool_limits39 )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,50 url: URLTypes,51 *,52 content: ByteOrByteStream = b"",53 query_params: QueryParamTypes = None,54 headers: HeaderTypes = None,55 stream: bool = False,56 allow_redirects: bool = True,57 ssl: SSLConfig = None,58 timeout: TimeoutConfig = None,59 ) -> Response:60 request = Request(61 method, url, query_params=query_params, headers=headers, content=content62 )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,75 url: URLTypes,76 *,77 query_params: QueryParamTypes = None,78 headers: HeaderTypes = None,79 stream: bool = False,80 allow_redirects: bool = True,81 ssl: SSLConfig = None,82 timeout: TimeoutConfig = None,83 ) -> Response:84 return await self.request(85 "GET",86 url,87 query_params=query_params,88 headers=headers,89 stream=stream,90 allow_redirects=allow_redirects,91 ssl=ssl,92 timeout=timeout,93 )9495 async def options(96 self,97 url: URLTypes,98 *,99 query_params: QueryParamTypes = None,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,109 query_params=query_params,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 *,121 query_params: QueryParamTypes = None,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,131 query_params=query_params,132 headers=headers,133 stream=stream,134 allow_redirects=allow_redirects,135 ssl=ssl,136 timeout=timeout,137 )138139 async def post(140 self,141 url: URLTypes,142 *,143 content: ByteOrByteStream = b"",144 query_params: QueryParamTypes = None,145 headers: HeaderTypes = None,146 stream: bool = False,147 allow_redirects: bool = True,148 ssl: SSLConfig = None,149 timeout: TimeoutConfig = None,150 ) -> Response:151 return await self.request(152 "POST",153 url,154 content=content,155 query_params=query_params,156 headers=headers,157 stream=stream,158 allow_redirects=allow_redirects,159 ssl=ssl,160 timeout=timeout,161 )162163 async def put(164 self,165 url: URLTypes,166 *,167 content: ByteOrByteStream = b"",168 query_params: QueryParamTypes = None,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,178 content=content,179 query_params=query_params,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 *,191 content: ByteOrByteStream = b"",192 query_params: QueryParamTypes = None,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,202 content=content,203 query_params=query_params,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 *,215 content: ByteOrByteStream = b"",216 query_params: QueryParamTypes = None,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,226 content=content,227 query_params=query_params,228 headers=headers,229 stream=stream,230 allow_redirects=allow_redirects,231 ssl=ssl,232 timeout=timeout,233 )234235 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,244 ssl: SSLConfig = None,245 timeout: TimeoutConfig = None,246 ) -> Response:247 options = {248 "stream": stream,249 "allow_redirects": allow_redirects,250 } # type: typing.Dict[str, typing.Any]251252 if ssl is not None:253 options["ssl"] = ssl254 if timeout is not None:255 options["timeout"] = timeout256257 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()