-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi30.py
More file actions
80 lines (61 loc) · 2.26 KB
/
api30.py
File metadata and controls
80 lines (61 loc) · 2.26 KB
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
import json
import re
from dataclasses import dataclass, field
from http import HTTPStatus
from typing import Dict, Callable, Any, Tuple, List, Iterable
@dataclass
class Request:
method: str
path: str
@classmethod
def from_env(cls, env) -> 'Request':
return cls(
env['REQUEST_METHOD'],
env['PATH_INFO']
)
@dataclass
class Response:
data: Any = None
status: HTTPStatus = HTTPStatus.OK
headers: Dict[str, str] = field(default_factory=dict)
@property
def headers_list(self) -> List[Tuple[str, str]]:
return [(k, v) for k, v in self.headers.items()]
@property
def body(self) -> Iterable[bytes]:
if self.data is None:
return []
return [json.dumps(self.data).encode()]
ApiFunction = Callable[[Request, ...], Response]
class Api30:
def __init__(self):
self._routes: Dict[Tuple[str, re.Pattern], ApiFunction] = {}
self._middlewares: List = []
def __call__(self, environ, start_response: callable) -> Iterable[bytes]:
req = Request.from_env(environ)
self._pre_view(req)
for (method, pattern), function in self._routes.items():
if req.method == method and (match := pattern.fullmatch(req.path)):
resp = function(req, *match.groups())
self._post_view(req, resp)
start_response(
self._format_status(resp.status),
resp.headers,
)
return resp.body
start_response(self._format_status(HTTPStatus.NOT_FOUND), [('Content-Type', 'text/plain')])
return []
def _pre_view(self, req: Request):
for m in self._middlewares:
m.pre(req)
def _post_view(self, req: Request, resp: Response):
for m in self._middlewares:
m.post(req, resp)
def _format_status(self, status: HTTPStatus) -> str:
return f'{status.value} {status.name}'
def add_route(self, method: str, path: str, func: ApiFunction) -> None:
self._routes[(method, re.compile(path))] = func
def get(self, path: str, func: ApiFunction) -> None:
self.add_route('GET', path, func)
def push_middleware(self, middleware):
self._middlewares.append(middleware)