Skip to content
Myr

interfaces.py

This file

Path

httpcore/interfaces.py

134 lines · 3.6 KiB · Python

Last changed by

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

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,

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

line 9why, one step back
9 Headers,

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

lines 10–16why, one step back
10 HeaderTypes,
11 QueryParamTypes,12 Request,13 RequestData,14 Response,15 URLTypes,16)

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 17why, one step back
17

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

lines 18–21why, one step back
18
19class Protocol(str, enum.Enum):20 HTTP_11 = "HTTP/1.1"21 HTTP_2 = "HTTP/2"

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 22–23why, one step back
22
23

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

line 24why, one step back
24class Dispatcher:

Updating docstringsTom Christiebefore adoption5f8000f

line 25why, one step back
25 """

Minor docstring tweakTom Christiebefore adoptionf1b10e5

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

Updating docstringsTom Christiebefore adoption5f8000f

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

Minor docstring tweakTom Christiebefore adoptionf1b10e5

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

Updating docstringsTom Christiebefore adoption5f8000f

lines 30–32why, one step back
30 without requiring a complete `Client` instance.
31 """32

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 33–35why, one step back
33 async def request(
34 self,35 method: str,

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 37why, one step back
37 *,

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

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

query_params -> paramsTom Christiebefore adoptionbb70377

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

Prefer 'content' over 'body'Tom Christiebefore adoption4255d41

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

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

line 41why, one step back
41 stream: bool = False,

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

lines 42–44why, one step back
42 verify: VerifyTypes = None,
43 cert: CertTypes = None,44 timeout: TimeoutTypes = None

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

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

query_params -> paramsTom Christiebefore adoptionbb70377

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 47why, one step back
47 self.prepare_request(request)

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

lines 48–50why, one step back
48 response = await self.send(
49 request, stream=stream, verify=verify, cert=cert, timeout=timeout50 )

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 51–53why, one step back
51 return response
5253 def prepare_request(self, request: Request) -> None:

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

line 54why, one step back
54 request.prepare()

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 55why, one step back
55

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

lines 56–59why, one step back
56 async def send(
57 self,58 request: Request,59 stream: bool = False,

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

lines 60–62why, one step back
60 verify: VerifyTypes = None,
61 cert: CertTypes = None,62 timeout: TimeoutTypes = None,

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 64–66why, one step back
64 raise NotImplementedError() # pragma: nocover
6566 async def close(self) -> None:

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 68why, one step back
68

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

line 69why, one step back
69 async def __aenter__(self) -> "Dispatcher":

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 70–81why, one step back
70 return self
7172 async def __aexit__(73 self,74 exc_type: typing.Type[BaseException] = None,75 exc_value: BaseException = None,76 traceback: TracebackType = None,77 ) -> None:78 await self.close()798081class BaseReader:

Updating docstringsTom Christiebefore adoption5f8000f

lines 82–87why, one step back
82 """
83 A stream reader. Abstracts away any asyncio-specfic interfaces84 into a more generic base class, that we can use with alternate85 backend, or for stand-alone test cases.86 """87

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 89–92why, one step back
89 raise NotImplementedError() # pragma: no cover
909192class BaseWriter:

Updating docstringsTom Christiebefore adoption5f8000f

lines 93–98why, one step back
93 """
94 A stream writer. Abstracts away any asyncio-specfic interfaces95 into a more generic base class, that we can use with alternate96 backend, or for stand-alone test cases.97 """98

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 99–101why, one step back
99 def write_no_block(self, data: bytes) -> None:
100 raise NotImplementedError() # pragma: no cover101

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

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

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 103–109why, one step back
103 raise NotImplementedError() # pragma: no cover
104105 async def close(self) -> None:106 raise NotImplementedError() # pragma: no cover107108109class BasePoolSemaphore:

Updating docstringsTom Christiebefore adoption5f8000f

lines 110–115why, one step back
110 """
111 A semaphore for use with connection pooling.112113 Abstracts away any asyncio-specfic interfaces.114 """115

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

lines 116–119why, one step back
116 async def acquire(self) -> None:
117 raise NotImplementedError() # pragma: no cover118119 def release(self) -> None:

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

lines 120–133why, one step back
120 raise NotImplementedError() # pragma: no cover
121122123class ConcurrencyBackend:124 async def connect(125 self,126 hostname: str,127 port: int,128 ssl_context: typing.Optional[ssl.SSLContext],129 timeout: TimeoutConfig,130 ) -> typing.Tuple[BaseReader, BaseWriter, Protocol]:131 raise NotImplementedError() # pragma: no cover132133 def get_semaphore(self, limits: PoolLimits) -> BasePoolSemaphore:

Merge pull request #23 from encode/adaptersTom Christiebefore adoption696c054

line 134why, one step back
134 raise NotImplementedError() # pragma: no cover