-
Notifications
You must be signed in to change notification settings - Fork 89
/
base.py
116 lines (96 loc) · 3.01 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import enum
from types import TracebackType
from typing import AsyncIterator, Tuple, Type
from .._types import URL, Headers, T
class NewConnectionRequired(Exception):
pass
class ConnectionState(enum.IntEnum):
"""
PENDING READY
| | ^
v V |
ACTIVE |
| | |
| V |
V IDLE-+
FULL |
| |
V V
CLOSED
"""
PENDING = 0 # Connection not yet acquired.
READY = 1 # Re-acquired from pool, about to send a request.
ACTIVE = 2 # Active requests.
FULL = 3 # Active requests, no more stream IDs available.
IDLE = 4 # No active requests.
CLOSED = 5 # Connection closed.
class AsyncByteStream:
"""
The base interface for request and response bodies.
Concrete implementations should subclass this class, and implement
the :meth:`__aiter__` method, and optionally the :meth:`aclose` method.
"""
async def __aiter__(self) -> AsyncIterator[bytes]:
"""
Yield bytes representing the request or response body.
"""
yield b"" # pragma: nocover
async def aclose(self) -> None:
"""
Must be called by the client to indicate that the stream has been closed.
"""
pass # pragma: nocover
class AsyncHTTPTransport:
"""
The base interface for sending HTTP requests.
Concrete implementations should subclass this class, and implement
the :meth:`arequest` method, and optionally the :meth:`aclose` method.
"""
async def arequest(
self,
method: bytes,
url: URL,
headers: Headers = None,
stream: AsyncByteStream = None,
ext: dict = None,
) -> Tuple[int, Headers, AsyncByteStream, dict]:
"""
The interface for sending a single HTTP request, and returning a response.
Parameters
----------
method:
The HTTP method, such as ``b'GET'``.
url:
The URL as a 4-tuple of (scheme, host, port, path).
headers:
Any HTTP headers to send with the request.
stream:
The body of the HTTP request.
ext:
A dictionary of optional extensions.
Returns
-------
status_code:
The HTTP status code, such as ``200``.
headers:
Any HTTP headers included on the response.
stream:
The body of the HTTP response.
ext:
A dictionary of optional extensions.
"""
raise NotImplementedError() # pragma: nocover
async def aclose(self) -> None:
"""
Close the implementation, which should close any outstanding response streams,
and any keep alive connections.
"""
async def __aenter__(self: T) -> T:
return self
async def __aexit__(
self,
exc_type: Type[BaseException] = None,
exc_value: BaseException = None,
traceback: TracebackType = None,
) -> None:
await self.aclose()