client.py
This file
Path
httpcore/client.py
124 lines · 3.8 KiB · Python
Last changed by
Rollin'Tom Christiebefore adoption8a29a0a
History of this fileTake it
1import typing2from types import TracebackType34from .auth import AuthAdapter5from .config import (6 DEFAULT_MAX_REDIRECTS,7 DEFAULT_POOL_LIMITS,8 DEFAULT_SSL_CONFIG,9 DEFAULT_TIMEOUT_CONFIG,10 PoolLimits,11 SSLConfig,12 TimeoutConfig,13)14from .connection_pool import ConnectionPool15from .cookies import CookieAdapter16from .environment import EnvironmentAdapter17from .models import URL, Request, Response18from .redirects import RedirectAdapter192021class Client:22 def __init__(23 self,24 ssl: SSLConfig = DEFAULT_SSL_CONFIG,25 timeout: TimeoutConfig = DEFAULT_TIMEOUT_CONFIG,26 limits: PoolLimits = DEFAULT_POOL_LIMITS,27 max_redirects: int = DEFAULT_MAX_REDIRECTS,28 ):29 connection_pool = ConnectionPool(ssl=ssl, timeout=timeout, limits=limits)30 cookie_adapter = CookieAdapter(dispatch=connection_pool)31 auth_adapter = AuthAdapter(dispatch=cookie_adapter)32 redirect_adapter = RedirectAdapter(33 dispatch=auth_adapter, max_redirects=max_redirects34 )35 self.adapter = EnvironmentAdapter(dispatch=redirect_adapter)3637 async def request(38 self,39 method: str,40 url: typing.Union[str, URL],41 *,42 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",43 headers: typing.List[typing.Tuple[bytes, bytes]] = [],44 stream: bool = False,45 allow_redirects: bool = True,46 ssl: typing.Optional[SSLConfig] = None,47 timeout: typing.Optional[TimeoutConfig] = None,48 ) -> Response:49 request = Request(method, url, headers=headers, body=body)50 self.prepare_request(request)51 response = await self.send(52 request,53 stream=stream,54 allow_redirects=allow_redirects,55 ssl=ssl,56 timeout=timeout,57 )58 return response5960 async def get(61 self,62 url: typing.Union[str, URL],63 *,64 headers: typing.List[typing.Tuple[bytes, bytes]] = [],65 stream: bool = False,66 ssl: typing.Optional[SSLConfig] = None,67 timeout: typing.Optional[TimeoutConfig] = None,68 ) -> Response:69 return await self.request(70 "GET", url, headers=headers, stream=stream, ssl=ssl, timeout=timeout71 )7273 async def post(74 self,75 url: typing.Union[str, URL],76 *,77 body: typing.Union[bytes, typing.AsyncIterator[bytes]] = b"",78 headers: typing.List[typing.Tuple[bytes, bytes]] = [],79 stream: bool = False,80 ssl: typing.Optional[SSLConfig] = None,81 timeout: typing.Optional[TimeoutConfig] = None,82 ) -> Response:83 return await self.request(84 "POST",85 url,86 body=body,87 headers=headers,88 stream=stream,89 ssl=ssl,90 timeout=timeout,91 )9293 def prepare_request(self, request: Request) -> None:94 self.adapter.prepare_request(request)9596 async def send(97 self,98 request: Request,99 *,100 stream: bool = False,101 allow_redirects: bool = True,102 ssl: typing.Optional[SSLConfig] = None,103 timeout: typing.Optional[TimeoutConfig] = None,104 ) -> Response:105 options = {"stream": stream} # type: typing.Dict[str, typing.Any]106 if ssl is not None:107 options["ssl"] = ssl108 if timeout is not None:109 options["timeout"] = timeout110 return await self.adapter.send(request, **options)111112 async def close(self) -> None:113 await self.adapter.close()114115 async def __aenter__(self) -> "Client":116 return self117118 async def __aexit__(119 self,120 exc_type: typing.Type[BaseException] = None,121 exc_value: BaseException = None,122 traceback: TracebackType = None,123 ) -> None:124 await self.close()