-
Notifications
You must be signed in to change notification settings - Fork 2
/
routing.py
47 lines (32 loc) · 1.28 KB
/
routing.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
import functools
from tornado.routing import PathMatches
from core import PipelineDelegate
class MethodMatches(PathMatches):
"""Matches request path and maethod."""
def __init__(self, path_pattern, method: str):
super().__init__(path_pattern)
self.method = method.upper()
def match(self, request):
result = super().match(request)
if result is not None:
if request.method.upper() != self.method:
return None
return result
class Router:
_routes = []
def __call__(self, path: str, name=None, method: str = None):
def wrapper(func):
if method is None:
self._routes.append((path, PipelineDelegate, {'delegate': func}, name))
else:
self._routes.append((MethodMatches(path, method), PipelineDelegate, {'delegate': func}, name))
return func
return wrapper
get = functools.partialmethod(__call__, method='GET')
post = functools.partialmethod(__call__, method='POST')
put = functools.partialmethod(__call__, method='PUT')
patch = functools.partialmethod(__call__, method='PATCH')
delete = functools.partialmethod(__call__, method='DELETE')
def get_routes(self):
return tuple(self._routes)
route = Router()