interfaces.py
This file
Path
http3/interfaces.py
229 lines · 6.4 KiB · Python
Last changed by
Read/Write timeout modes (#104)Tom Christiebefore adoptionf4f05e3
History of this fileTake it
http3 (#86)Tom Christiebefore adoptionc9747aa
lines 1–129why, one step back1import enum2import ssl3import typing4from types import TracebackType56from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypes7from .models import (8 URL,9 AsyncRequest,10 AsyncRequestData,11 AsyncResponse,12 Headers,13 HeaderTypes,14 QueryParamTypes,15 Request,16 RequestData,17 Response,18 URLTypes,19)202122class Protocol(str, enum.Enum):23 HTTP_11 = "HTTP/1.1"24 HTTP_2 = "HTTP/2"252627class AsyncDispatcher:28 """29 Base class for async dispatcher classes, that handle sending the request.3031 Stubs out the interface, as well as providing a `.request()` convienence32 implementation, to make it easy to use or test stand-alone dispatchers,33 without requiring a complete `Client` instance.34 """3536 async def request(37 self,38 method: str,39 url: URLTypes,40 *,41 data: AsyncRequestData = b"",42 params: QueryParamTypes = None,43 headers: HeaderTypes = None,44 verify: VerifyTypes = None,45 cert: CertTypes = None,46 timeout: TimeoutTypes = None47 ) -> AsyncResponse:48 request = AsyncRequest(method, url, data=data, params=params, headers=headers)49 return await self.send(request, verify=verify, cert=cert, timeout=timeout)5051 async def send(52 self,53 request: AsyncRequest,54 verify: VerifyTypes = None,55 cert: CertTypes = None,56 timeout: TimeoutTypes = None,57 ) -> AsyncResponse:58 raise NotImplementedError() # pragma: nocover5960 async def close(self) -> None:61 pass # pragma: nocover6263 async def __aenter__(self) -> "AsyncDispatcher":64 return self6566 async def __aexit__(67 self,68 exc_type: typing.Type[BaseException] = None,69 exc_value: BaseException = None,70 traceback: TracebackType = None,71 ) -> None:72 await self.close()737475class Dispatcher:76 """77 Base class for syncronous dispatcher classes, that handle sending the request.7879 Stubs out the interface, as well as providing a `.request()` convienence80 implementation, to make it easy to use or test stand-alone dispatchers,81 without requiring a complete `Client` instance.82 """8384 def request(85 self,86 method: str,87 url: URLTypes,88 *,89 data: RequestData = b"",90 params: QueryParamTypes = None,91 headers: HeaderTypes = None,92 verify: VerifyTypes = None,93 cert: CertTypes = None,94 timeout: TimeoutTypes = None95 ) -> Response:96 request = Request(method, url, data=data, params=params, headers=headers)97 return self.send(request, verify=verify, cert=cert, timeout=timeout)9899 def send(100 self,101 request: Request,102 verify: VerifyTypes = None,103 cert: CertTypes = None,104 timeout: TimeoutTypes = None,105 ) -> Response:106 raise NotImplementedError() # pragma: nocover107108 def close(self) -> None:109 pass # pragma: nocover110111 def __enter__(self) -> "Dispatcher":112 return self113114 def __exit__(115 self,116 exc_type: typing.Type[BaseException] = None,117 exc_value: BaseException = None,118 traceback: TracebackType = None,119 ) -> None:120 self.close()121122123class BaseReader:124 """125 A stream reader. Abstracts away any asyncio-specfic interfaces126 into a more generic base class, that we can use with alternate127 backend, or for stand-alone test cases.128 """129Read/Write timeout modes (#104)Tom Christiebefore adoptionf4f05e3
lines 130–132why, one step back130 async def read(131 self, n: int, timeout: TimeoutConfig = None, flag: typing.Any = None132 ) -> bytes:http3 (#86)Tom Christiebefore adoptionc9747aa
lines 133–211why, one step back133 raise NotImplementedError() # pragma: no cover134135136class BaseWriter:137 """138 A stream writer. Abstracts away any asyncio-specfic interfaces139 into a more generic base class, that we can use with alternate140 backend, or for stand-alone test cases.141 """142143 def write_no_block(self, data: bytes) -> None:144 raise NotImplementedError() # pragma: no cover145146 async def write(self, data: bytes, timeout: TimeoutConfig = None) -> None:147 raise NotImplementedError() # pragma: no cover148149 async def close(self) -> None:150 raise NotImplementedError() # pragma: no cover151152153class BasePoolSemaphore:154 """155 A semaphore for use with connection pooling.156157 Abstracts away any asyncio-specfic interfaces.158 """159160 async def acquire(self) -> None:161 raise NotImplementedError() # pragma: no cover162163 def release(self) -> None:164 raise NotImplementedError() # pragma: no cover165166167class ConcurrencyBackend:168 async def connect(169 self,170 hostname: str,171 port: int,172 ssl_context: typing.Optional[ssl.SSLContext],173 timeout: TimeoutConfig,174 ) -> typing.Tuple[BaseReader, BaseWriter, Protocol]:175 raise NotImplementedError() # pragma: no cover176177 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:178 raise NotImplementedError() # pragma: no cover179180 async def run_in_threadpool(181 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any182 ) -> typing.Any:183 raise NotImplementedError() # pragma: no cover184185 async def iterate_in_threadpool(self, iterator): # type: ignore186 class IterationComplete(Exception):187 pass188189 def next_wrapper(iterator): # type: ignore190 try:191 return next(iterator)192 except StopIteration:193 raise IterationComplete()194195 while True:196 try:197 yield await self.run_in_threadpool(next_wrapper, iterator)198 except IterationComplete:199 break200201 def run(202 self, coroutine: typing.Callable, *args: typing.Any, **kwargs: typing.Any203 ) -> typing.Any:204 raise NotImplementedError() # pragma: no cover205206 def iterate(self, async_iterator): # type: ignore207 while True:208 try:209 yield self.run(async_iterator.__anext__)210 except StopAsyncIteration:211 breakHandle early connection closes (#103)Tom Christiebefore adoptionb9c8a15
lines 212–229why, one step back212213 def background_manager(214 self, coroutine: typing.Callable, args: typing.Any215 ) -> "BaseBackgroundManager":216 raise NotImplementedError() # pragma: no cover217218219class BaseBackgroundManager:220 async def __aenter__(self) -> "BaseBackgroundManager":221 raise NotImplementedError() # pragma: no cover222223 async def __aexit__(224 self,225 exc_type: typing.Type[BaseException] = None,226 exc_value: BaseException = None,227 traceback: TracebackType = None,228 ) -> None:229 raise NotImplementedError() # pragma: no cover