-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.py
210 lines (164 loc) · 5.19 KB
/
types.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""Types for bareASGI and bareClient"""
from abc import ABCMeta, abstractmethod
from typing import (
AbstractSet,
Any,
AsyncIterable,
Awaitable,
Callable,
Iterable,
List,
Mapping,
MutableMapping,
Optional,
Tuple,
Union
)
class ParseError(Exception):
"""Exception raised on a parse error"""
class HttpInternalError(Exception):
"""Exception raised for an internal error"""
class HttpDisconnectError(Exception):
"""Exception raise on HTTP disconnect"""
class WebSocketInternalError(Exception):
"""Exception raised for a WebSocket internal error"""
Scope = Mapping[str, Any]
Message = Mapping[str, Any]
Context = Optional[Mapping[str, Any]]
Info = Optional[MutableMapping[str, Any]]
Receive = Callable[[], Awaitable[Message]]
Send = Callable[[Message], Awaitable[None]]
ASGIInstance = Callable[[Receive, Send], Awaitable[None]]
ASGIApp = Callable[[Scope], ASGIInstance]
LifespanHandler = Callable[[Scope, Info, Message], Awaitable[None]]
Header = Tuple[bytes, bytes]
Headers = List[Header]
RouteMatches = Mapping[str, Any]
Content = AsyncIterable[bytes]
PushResponse = Tuple[str, Headers]
PushResponses = Iterable[PushResponse]
class WebSocket(metaclass=ABCMeta):
"""The interface for a server side WebSocket."""
@abstractmethod
async def accept(
self,
subprotocol: Optional[str] = None,
headers: Optional[List[Header]] = None
) -> None:
"""Accept the socket.
This must be done before any other action is taken.
Args:
subprotocol (Optional[str], optional): An optional subprotocol sent
by the client. Defaults to None.
headers (Optional[List[Header]], optional): Optional headers to
send. Defaults to None.
"""
@abstractmethod
async def receive(self) -> Optional[Union[bytes, str]]:
"""Receive data from the WebSocket.
Returns:
Optional[Union[bytes, str]]: Either bytes of a string depending on
the client.
"""
@abstractmethod
async def send(self, content: Union[bytes, str]) -> None:
"""Send data to the client.
Args:
content (Union[bytes, str]): Either bytes or a string.
"""
@abstractmethod
async def close(self, code: int = 1000) -> None:
"""Close the WebSocket.
Args:
code (int, optional): The reason code. Defaults to 1000.
"""
HttpFullResponse = Tuple[
int,
Optional[Headers],
Optional[Content],
Optional[PushResponses]
]
HttpResponse = Union[
int,
Tuple[int],
Tuple[int, Optional[Headers]],
Tuple[int, Optional[Headers], Optional[Content]],
HttpFullResponse
]
HttpRequestCallback = Callable[
[Scope, Info, RouteMatches, Content],
Awaitable[HttpResponse]
]
WebSocketRequestCallback = Callable[
[Scope, Info, RouteMatches, WebSocket],
Awaitable[None]
]
HttpMiddlewareCallback = Callable[
[Scope, Info, RouteMatches, Content, HttpRequestCallback],
Awaitable[HttpFullResponse]
]
class HttpRouter(metaclass=ABCMeta):
"""The interface for an HTTP router"""
@property # type: ignore
@abstractmethod
def not_found_response(self) -> HttpResponse:
"""The response when a handler could not be found for a method/path
Returns:
HttpResponse: The response when a route cannot be found.
"""
@not_found_response.setter # type: ignore
@abstractmethod
def not_found_response(self, value: HttpResponse) -> None:
...
@abstractmethod
def add(
self,
methods: AbstractSet[str],
path: str,
callback: HttpRequestCallback
) -> None:
"""Add an HTTP request handler
Args:
methods (AbstractSet[str]): The supported HTTP methods.
path (str): The path.
callback (HttpRequestCallback): The request handler.
"""
@abstractmethod
def resolve(
self,
method: str,
path: str
) -> Tuple[Optional[HttpRequestCallback], Optional[RouteMatches]]:
"""Resolve a request to a handler with the route matches
Args:
method (str): The HTTP method.
path (str): The path.
Returns:
Tuple[Optional[HttpRequestCallback], Optional[RouteMatches]]: A
handler and the optional route matches.
"""
class WebSocketRouter(metaclass=ABCMeta):
"""The interface for a WebSocket router"""
@abstractmethod
def add(
self,
path: str,
callback: WebSocketRequestCallback
) -> None:
"""Add the WebSocket handler for a route
Args:
path (str): The path.
callback (WebSocketRequestCallback): The handler
"""
@abstractmethod
def resolve(
self,
path: str
) -> Tuple[Optional[HttpRequestCallback], Optional[RouteMatches]]:
"""Resolve a route to a handler
Args:
path (str): The path
Returns:
Tuple[Optional[HttpRequestCallback], Optional[RouteMatches]]: A
handler and possible route matches
"""