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

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        pool_limits: PoolLimits = DEFAULT_POOL_LIMITS,27        max_redirects: int = DEFAULT_MAX_REDIRECTS,28    ):29        connection_pool = ConnectionPool(30            ssl=ssl, timeout=timeout, pool_limits=pool_limits31        )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,42        url: URLTypes,43        *,44        content: ByteOrByteStream = b"",45        headers: HeaderTypes = None,46        stream: bool = False,47        allow_redirects: bool = True,48        ssl: SSLConfig = None,49        timeout: TimeoutConfig = None,50    ) -> Response:51        request = Request(method, url, headers=headers, content=content)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,64        url: URLTypes,65        *,66        headers: HeaderTypes = None,67        stream: bool = False,68        allow_redirects: bool = True,69        ssl: SSLConfig = None,70        timeout: TimeoutConfig = None,71    ) -> Response:72        return await self.request(73            "GET",74            url,75            headers=headers,76            stream=stream,77            allow_redirects=allow_redirects,78            ssl=ssl,79            timeout=timeout,80        )8182    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        )121122    async def post(123        self,124        url: URLTypes,125        *,126        content: ByteOrByteStream = b"",127        headers: HeaderTypes = None,128        stream: bool = False,129        allow_redirects: bool = True,130        ssl: SSLConfig = None,131        timeout: TimeoutConfig = None,132    ) -> Response:133        return await self.request(134            "POST",135            url,136            content=content,137            headers=headers,138            stream=stream,139            allow_redirects=allow_redirects,140            ssl=ssl,141            timeout=timeout,142        )143144    async def put(145        self,146        url: URLTypes,147        *,148        content: ByteOrByteStream = b"",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,158            content=content,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        *,170        content: ByteOrByteStream = b"",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,180            content=content,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        *,192        content: ByteOrByteStream = b"",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,202            content=content,203            headers=headers,204            stream=stream,205            allow_redirects=allow_redirects,206            ssl=ssl,207            timeout=timeout,208        )209210    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,219        ssl: SSLConfig = None,220        timeout: TimeoutConfig = None,221    ) -> Response:222        options = {223            "stream": stream,224            "allow_redirects": allow_redirects,225        }  # type: typing.Dict[str, typing.Any]226227        if ssl is not None:228            options["ssl"] = ssl229        if timeout is not None:230            options["timeout"] = timeout231232        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()