-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
65 lines (44 loc) · 1.82 KB
/
api.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
from webob import Request, Response
from typing import Callable, Dict
from requests import Session as RequestsSession
from wsgiadapter import WSGIAdapter as RequestsWSGIAdapter
from parse import parse
import inspect
class API:
def __init__(self):
self.routes: Dict[str,Callable] = {}
def __call__(self, environ: dict, start_response: Callable):
request = Request(environ)
response = self.handle_request(request)
return response(environ, start_response)
def find_handler(self, request_path: str):
for path, handler in self.routes.items():
parse_result = parse(path, request_path)
if parse_result is not None:
return handler, parse_result.named
return None, None
def handle_request(self, request: Request):
response = Response()
handler, kwargs = self.find_handler(request_path=request.path)
if handler is not None:
if inspect.isclass(handler):
handler = getattr(handler(), request.method.lower(), None)
if handler is None:
raise AttributeError("Method not allowed", request.method)
handler(request, response, **kwargs)
else:
self.default_response(response)
return response
def route(self, path):
assert path not in self.routes, "Such route already exists."
def wrapper(handler):
self.routes[path] = handler
return handler
return wrapper
def default_response(self, response: Response):
response.status_code = 404
response.text = "Not found."
def test_session(self, base_url="http://testserver"):
session = RequestsSession()
session.mount(prefix=base_url, adapter=RequestsWSGIAdapter(self))
return session