8
8
"""
9
9
10
10
try :
11
- from typing import Callable , Protocol , Union
11
+ from typing import Callable , Protocol , Union , List
12
12
from socket import socket
13
13
from socketpool import SocketPool
14
14
except ImportError :
@@ -41,7 +41,7 @@ def __init__(self, socket_source: Protocol, root_path: str) -> None:
41
41
self ._sock = None
42
42
self .root_path = root_path
43
43
44
- def route (self , path : str , method : str = GET ) -> Callable :
44
+ def route (self , path : str , methods : Union [ str , List [ str ]] = GET ) -> Callable :
45
45
"""
46
46
Decorator used to add a route.
47
47
@@ -55,13 +55,27 @@ def route(self, path: str, method: str = GET) -> Callable:
55
55
def route_func(request):
56
56
...
57
57
58
- @server.route("/example/<my_parameter>", HTTPMethod.GET)
58
+ # It is necessary to specify other methods like POST, PUT, etc.
59
+ @server.route("/example", POST)
60
+ def route_func(request):
61
+ ...
62
+
63
+ # Multiple methods can be specified
64
+ @server.route("/example", [GET, POST])
65
+ def route_func(request):
66
+ ...
67
+
68
+ # URL parameters can be specified
69
+ @server.route("/example/<my_parameter>", GET)
59
70
def route_func(request, my_parameter):
60
71
...
61
72
"""
73
+ if isinstance (methods , str ):
74
+ methods = [methods ]
62
75
63
76
def route_decorator (func : Callable ) -> Callable :
64
- self .routes .add (_HTTPRoute (path , method ), func )
77
+ for method in methods :
78
+ self .routes .add (_Route (path , method ), func )
65
79
return func
66
80
67
81
return route_decorator
0 commit comments