interfaces.py
This file
Path
httpcore/interfaces.py
209 lines · 5.8 KiB · Python
Last changed by
Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
History of this fileTake it
Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
line 1why, one step back1import enumMerge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
line 2why, one step back2import sslMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 3–5why, one step back3import typing4from types import TracebackType5Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 6why, one step back6from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypesMerge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
lines 7–8why, one step back7from .models import (8 URL,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 9–11why, one step back9 AsyncRequest,10 AsyncRequestData,11 AsyncResponse,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 12why, one step back12 Headers,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
lines 13–19why, one step back13 HeaderTypes,14 QueryParamTypes,15 Request,16 RequestData,17 Response,18 URLTypes,19)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 20why, one step back20Merge pull request #59 from encode/use-stdlib-status-codesTom Christiebefore adoptionafc8369
lines 21–24why, one step back2122class Protocol(str, enum.Enum):23 HTTP_11 = "HTTP/1.1"24 HTTP_2 = "HTTP/2"Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 25–26why, one step back2526Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 27why, one step back27class AsyncDispatcher:Updating docstringsTom Christiebefore adoption5f8000f
line 28why, one step back28 """Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 29why, one step back29 Base class for async dispatcher classes, that handle sending the request.Updating docstringsTom Christiebefore adoption5f8000f
lines 30–31why, one step back3031 Stubs out the interface, as well as providing a `.request()` convienenceMinor docstring tweakTom Christiebefore adoptionf1b10e5
line 32why, one step back32 implementation, to make it easy to use or test stand-alone dispatchers,Updating docstringsTom Christiebefore adoption5f8000f
lines 33–35why, one step back33 without requiring a complete `Client` instance.34 """35Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 36–38why, one step back36 async def request(37 self,38 method: str,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 39why, one step back39 url: URLTypes,Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 40why, one step back40 *,Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 41–88why, one step back41 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 *,Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53
line 89why, one step back89 data: RequestData = b"",query_params -> paramsTom Christiebefore adoptionbb70377
line 90why, one step back90 params: QueryParamTypes = None,Prefer 'content' over 'body'Tom Christiebefore adoption4255d41
line 91why, one step back91 headers: HeaderTypes = None,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 92–94why, one step back92 verify: VerifyTypes = None,93 cert: CertTypes = None,94 timeout: TimeoutTypes = NoneMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 95why, one step back95 ) -> Response:query_params -> paramsTom Christiebefore adoptionbb70377
line 96why, one step back96 request = Request(method, url, data=data, params=params, headers=headers)Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 97why, one step back97 return self.send(request, verify=verify, cert=cert, timeout=timeout)Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 98why, one step back98Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 99why, one step back99 def send(Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
lines 100–101why, one step back100 self,101 request: Request,Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
lines 102–104why, one step back102 verify: VerifyTypes = None,103 cert: CertTypes = None,104 timeout: TimeoutTypes = None,Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 105why, one step back105 ) -> Response:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 106–107why, one step back106 raise NotImplementedError() # pragma: nocover107Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 108why, one step back108 def close(self) -> None:Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144
line 109why, one step back109 pass # pragma: nocoverMerge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 110why, one step back110Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 111why, one step back111 def __enter__(self) -> "Dispatcher":Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 112–113why, one step back112 return self113Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 114why, one step back114 def __exit__(Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 115–119why, one step back115 self,116 exc_type: typing.Type[BaseException] = None,117 exc_value: BaseException = None,118 traceback: TracebackType = None,119 ) -> None:Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 120why, one step back120 self.close()Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 121–123why, one step back121122123class BaseReader:Updating docstringsTom Christiebefore adoption5f8000f
lines 124–129why, one step back124 """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 """129Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 130why, one step back130 async def read(self, n: int, timeout: TimeoutConfig = None) -> bytes:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 131–134why, one step back131 raise NotImplementedError() # pragma: no cover132133134class BaseWriter:Updating docstringsTom Christiebefore adoption5f8000f
lines 135–140why, one step back135 """136 A stream writer. Abstracts away any asyncio-specfic interfaces137 into a more generic base class, that we can use with alternate138 backend, or for stand-alone test cases.139 """140Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 141–143why, one step back141 def write_no_block(self, data: bytes) -> None:142 raise NotImplementedError() # pragma: no cover143Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041
line 144why, one step back144 async def write(self, data: bytes, timeout: TimeoutConfig = None) -> None:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 145–151why, one step back145 raise NotImplementedError() # pragma: no cover146147 async def close(self) -> None:148 raise NotImplementedError() # pragma: no cover149150151class BasePoolSemaphore:Updating docstringsTom Christiebefore adoption5f8000f
lines 152–157why, one step back152 """153 A semaphore for use with connection pooling.154155 Abstracts away any asyncio-specfic interfaces.156 """157Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
lines 158–161why, one step back158 async def acquire(self) -> None:159 raise NotImplementedError() # pragma: no cover160161 def release(self) -> None:Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af
lines 162–175why, one step back162 raise NotImplementedError() # pragma: no cover163164165class ConcurrencyBackend:166 async def connect(167 self,168 hostname: str,169 port: int,170 ssl_context: typing.Optional[ssl.SSLContext],171 timeout: TimeoutConfig,172 ) -> typing.Tuple[BaseReader, BaseWriter, Protocol]:173 raise NotImplementedError() # pragma: no cover174175 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054
line 176why, one step back176 raise NotImplementedError() # pragma: no coverSync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
lines 177–209why, one step back177178 async def run_in_threadpool(179 self, func: typing.Callable, *args: typing.Any, **kwargs: typing.Any180 ) -> typing.Any:181 raise NotImplementedError() # pragma: no cover182183 async def iterate_in_threadpool(self, iterator): # type: ignore184 class IterationComplete(Exception):185 pass186187 def next_wrapper(iterator): # type: ignore188 try:189 return next(iterator)190 except StopIteration:191 raise IterationComplete()192193 while True:194 try:195 yield await self.run_in_threadpool(next_wrapper, iterator)196 except IterationComplete:197 break198199 def run(200 self, coroutine: typing.Callable, *args: typing.Any, **kwargs: typing.Any201 ) -> typing.Any:202 raise NotImplementedError() # pragma: no cover203204 def iterate(self, async_iterator): # type: ignore205 while True:206 try:207 yield self.run(async_iterator.__anext__)208 except StopAsyncIteration:209 break