Skip to content
Myr

interfaces.py

This file

Path

httpcore/interfaces.py

119 lines · 3.1 KiB · Python

Last changed by

Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369

History of this file

Take it

1import enum2import typing3from types import TracebackType45from .config import SSLConfig, TimeoutConfig6from .models import (7    URL,8    Headers,9    HeaderTypes,10    QueryParamTypes,11    Request,12    RequestData,13    Response,14    URLTypes,15)1617OptionalTimeout = typing.Optional[TimeoutConfig]181920class Protocol(str, enum.Enum):21    HTTP_11 = "HTTP/1.1"22    HTTP_2 = "HTTP/2"232425class Dispatcher:26    """27    The base class for all adapter or dispatcher classes.2829    Stubs out the interface, as well as providing a `.request()` convienence30    implementation, to make it easy to use or test stand-alone adapters,31    without requiring a complete `Client` instance.32    """3334    async def request(35        self,36        method: str,37        url: URLTypes,38        *,39        data: RequestData = b"",40        query_params: QueryParamTypes = None,41        headers: HeaderTypes = None,42        stream: bool = False,43        ssl: SSLConfig = None,44        timeout: TimeoutConfig = None45    ) -> Response:46        request = Request(47            method, url, data=data, query_params=query_params, headers=headers48        )49        self.prepare_request(request)50        response = await self.send(request, stream=stream, ssl=ssl, timeout=timeout)51        return response5253    def prepare_request(self, request: Request) -> None:54        request.prepare()5556    async def send(57        self,58        request: Request,59        stream: bool = False,60        ssl: SSLConfig = None,61        timeout: TimeoutConfig = None,62    ) -> Response:63        raise NotImplementedError()  # pragma: nocover6465    async def close(self) -> None:66        pass  # pragma: nocover6768    async def __aenter__(self) -> "Dispatcher":69        return self7071    async def __aexit__(72        self,73        exc_type: typing.Type[BaseException] = None,74        exc_value: BaseException = None,75        traceback: TracebackType = None,76    ) -> None:77        await self.close()787980class BaseReader:81    """82    A stream reader. Abstracts away any asyncio-specfic interfaces83    into a more generic base class, that we can use with alternate84    backend, or for stand-alone test cases.85    """8687    async def read(self, n: int, timeout: OptionalTimeout = None) -> bytes:88        raise NotImplementedError()  # pragma: no cover899091class BaseWriter:92    """93    A stream writer. Abstracts away any asyncio-specfic interfaces94    into a more generic base class, that we can use with alternate95    backend, or for stand-alone test cases.96    """9798    def write_no_block(self, data: bytes) -> None:99        raise NotImplementedError()  # pragma: no cover100101    async def write(self, data: bytes, timeout: OptionalTimeout = None) -> None:102        raise NotImplementedError()  # pragma: no cover103104    async def close(self) -> None:105        raise NotImplementedError()  # pragma: no cover106107108class BasePoolSemaphore:109    """110    A semaphore for use with connection pooling.111112    Abstracts away any asyncio-specfic interfaces.113    """114115    async def acquire(self) -> None:116        raise NotImplementedError()  # pragma: no cover117118    def release(self) -> None:119        raise NotImplementedError()  # pragma: no cover