Skip to content
Myr

client.py

This file

Path

httpcore/client.py

244 lines · 6.5 KiB · Python

Last changed by

Merge branch 'master' into wait-for-connection-closeTom Christiebefore adoption20bc1f3

History of this file

Take 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 URL, ByteOrByteStream, HeaderTypes, Request, Response, URLTypes192021class 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 = AuthenticationAdapter(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: URLTypes,41        *,42        content: ByteOrByteStream = b"",43        headers: HeaderTypes = None,44        stream: bool = False,45        allow_redirects: bool = True,46        ssl: SSLConfig = None,47        timeout: TimeoutConfig = None,48    ) -> Response:49        request = Request(method, url, headers=headers, content=content)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: URLTypes,63        *,64        headers: HeaderTypes = None,65        stream: bool = False,66        allow_redirects: bool = True,67        ssl: SSLConfig = None,68        timeout: TimeoutConfig = None,69    ) -> Response:70        return await self.request(71            "GET",72            url,73            headers=headers,74            stream=stream,75            allow_redirects=allow_redirects,76            ssl=ssl,77            timeout=timeout,78        )7980    async def options(81        self,82        url: URLTypes,83        *,84        headers: HeaderTypes = None,85        stream: bool = False,86        allow_redirects: bool = True,87        ssl: SSLConfig = None,88        timeout: TimeoutConfig = None,89    ) -> Response:90        return await self.request(91            "OPTIONS",92            url,93            headers=headers,94            stream=stream,95            allow_redirects=allow_redirects,96            ssl=ssl,97            timeout=timeout,98        )99100    async def head(101        self,102        url: URLTypes,103        *,104        headers: HeaderTypes = None,105        stream: bool = False,106        allow_redirects: bool = False,  #  Note: Differs to usual default.107        ssl: SSLConfig = None,108        timeout: TimeoutConfig = None,109    ) -> Response:110        return await self.request(111            "HEAD",112            url,113            headers=headers,114            stream=stream,115            allow_redirects=allow_redirects,116            ssl=ssl,117            timeout=timeout,118        )119120    async def post(121        self,122        url: URLTypes,123        *,124        content: ByteOrByteStream = b"",125        headers: HeaderTypes = None,126        stream: bool = False,127        allow_redirects: bool = True,128        ssl: SSLConfig = None,129        timeout: TimeoutConfig = None,130    ) -> Response:131        return await self.request(132            "POST",133            url,134            content=content,135            headers=headers,136            stream=stream,137            allow_redirects=allow_redirects,138            ssl=ssl,139            timeout=timeout,140        )141142    async def put(143        self,144        url: URLTypes,145        *,146        content: ByteOrByteStream = b"",147        headers: HeaderTypes = None,148        stream: bool = False,149        allow_redirects: bool = True,150        ssl: SSLConfig = None,151        timeout: TimeoutConfig = None,152    ) -> Response:153        return await self.request(154            "PUT",155            url,156            content=content,157            headers=headers,158            stream=stream,159            allow_redirects=allow_redirects,160            ssl=ssl,161            timeout=timeout,162        )163164    async def patch(165        self,166        url: URLTypes,167        *,168        content: ByteOrByteStream = b"",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            "PATCH",177            url,178            content=content,179            headers=headers,180            stream=stream,181            allow_redirects=allow_redirects,182            ssl=ssl,183            timeout=timeout,184        )185186    async def delete(187        self,188        url: URLTypes,189        *,190        content: ByteOrByteStream = b"",191        headers: HeaderTypes = None,192        stream: bool = False,193        allow_redirects: bool = True,194        ssl: SSLConfig = None,195        timeout: TimeoutConfig = None,196    ) -> Response:197        return await self.request(198            "DELETE",199            url,200            content=content,201            headers=headers,202            stream=stream,203            allow_redirects=allow_redirects,204            ssl=ssl,205            timeout=timeout,206        )207208    def prepare_request(self, request: Request) -> None:209        self.adapter.prepare_request(request)210211    async def send(212        self,213        request: Request,214        *,215        stream: bool = False,216        allow_redirects: bool = True,217        ssl: SSLConfig = None,218        timeout: TimeoutConfig = None,219    ) -> Response:220        options = {221            "stream": stream,222            "allow_redirects": allow_redirects,223        }  # type: typing.Dict[str, typing.Any]224225        if ssl is not None:226            options["ssl"] = ssl227        if timeout is not None:228            options["timeout"] = timeout229230        return await self.adapter.send(request, **options)231232    async def close(self) -> None:233        await self.adapter.close()234235    async def __aenter__(self) -> "Client":236        return self237238    async def __aexit__(239        self,240        exc_type: typing.Type[BaseException] = None,241        exc_value: BaseException = None,242        traceback: TracebackType = None,243    ) -> None:244        await self.close()