Skip to content
Myr

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 file

Take it

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

line 1why, one step back
1import enum

Merge pull request #60 from encode/concurrency-backend-classTom Christiebefore adoption3d997af

line 2why, one step back
2import ssl

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 3–5why, one step back
3import typing
4from types import TracebackType5

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 6why, one step back
6from .config import CertTypes, PoolLimits, TimeoutConfig, TimeoutTypes, VerifyTypes

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

lines 7–8why, one step back
7from .models import (
8 URL,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 9–11why, one step back
9 AsyncRequest,
10 AsyncRequestData,11 AsyncResponse,

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 12why, one step back
12 Headers,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

lines 13–19why, one step back
13 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 back
20

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

lines 21–24why, one step back
21
22class 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 back
25
26

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 27why, one step back
27class AsyncDispatcher:

Updating docstringsTom Christiebefore adoption5f8000f

line 28why, one step back
28 """

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 29why, one step back
29 Base class for async dispatcher classes, that handle sending the request.

Updating docstringsTom Christiebefore adoption5f8000f

lines 30–31why, one step back
30
31 Stubs out the interface, as well as providing a `.request()` convienence

Minor docstring tweakTom Christiebefore adoptionf1b10e5

line 32why, one step back
32 implementation, to make it easy to use or test stand-alone dispatchers,

Updating docstringsTom Christiebefore adoption5f8000f

lines 33–35why, one step back
33 without requiring a complete `Client` instance.
34 """35

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 36–38why, one step back
36 async def request(
37 self,38 method: str,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 39why, one step back
39 url: URLTypes,

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 40why, one step back
40 *,

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 41–88why, one step back
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 *,

Merge pull request #44 from encode/request-dataTom Christiebefore adoption39a5c53

line 89why, one step back
89 data: RequestData = b"",

query_params -> paramsTom Christiebefore adoptionbb70377

line 90why, one step back
90 params: QueryParamTypes = None,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

line 91why, one step back
91 headers: HeaderTypes = None,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 92–94why, one step back
92 verify: VerifyTypes = None,
93 cert: CertTypes = None,94 timeout: TimeoutTypes = None

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 95why, one step back
95 ) -> Response:

query_params -> paramsTom Christiebefore adoptionbb70377

line 96why, one step back
96 request = Request(method, url, data=data, params=params, headers=headers)

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 97why, one step back
97 return self.send(request, verify=verify, cert=cert, timeout=timeout)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 98why, one step back
98

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 99why, one step back
99 def send(

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

lines 100–101why, one step back
100 self,
101 request: Request,

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

lines 102–104why, one step back
102 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 back
105 ) -> Response:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 106–107why, one step back
106 raise NotImplementedError() # pragma: nocover
107

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 108why, one step back
108 def close(self) -> None:

Merge pull request #55 from encode/drop-adaptersTom Christiebefore adoptiond72a144

line 109why, one step back
109 pass # pragma: nocover

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 110why, one step back
110

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 111why, one step back
111 def __enter__(self) -> "Dispatcher":

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 112–113why, one step back
112 return self
113

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

line 114why, one step back
114 def __exit__(

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 115–119why, one step back
115 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 back
120 self.close()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 121–123why, one step back
121
122123class BaseReader:

Updating docstringsTom Christiebefore adoption5f8000f

lines 124–129why, one step back
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 """129

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 130why, one step back
130 async def read(self, n: int, timeout: TimeoutConfig = None) -> bytes:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 131–134why, one step back
131 raise NotImplementedError() # pragma: no cover
132133134class BaseWriter:

Updating docstringsTom Christiebefore adoption5f8000f

lines 135–140why, one step back
135 """
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 """140

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 141–143why, one step back
141 def write_no_block(self, data: bytes) -> None:
142 raise NotImplementedError() # pragma: no cover143

Work on bringing API into parity with `requests`. (#76)Tom Christiebefore adoption9574041

line 144why, one step back
144 async def write(self, data: bytes, timeout: TimeoutConfig = None) -> None:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 145–151why, one step back
145 raise NotImplementedError() # pragma: no cover
146147 async def close(self) -> None:148 raise NotImplementedError() # pragma: no cover149150151class BasePoolSemaphore:

Updating docstringsTom Christiebefore adoption5f8000f

lines 152–157why, one step back
152 """
153 A semaphore for use with connection pooling.154155 Abstracts away any asyncio-specfic interfaces.156 """157

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 158–161why, one step back
158 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 back
162 raise NotImplementedError() # pragma: no cover
163164165class 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 back
176 raise NotImplementedError() # pragma: no cover

Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7

lines 177–209why, one step back
177
178 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