Skip to content

Commit 33fecc9

Browse files
committed
Allowed passing multiple methods at the same time to .route
1 parent 4224ac8 commit 33fecc9

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

adafruit_httpserver/server.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"""
99

1010
try:
11-
from typing import Callable, Protocol, Union
11+
from typing import Callable, Protocol, Union, List
1212
from socket import socket
1313
from socketpool import SocketPool
1414
except ImportError:
@@ -41,7 +41,7 @@ def __init__(self, socket_source: Protocol, root_path: str) -> None:
4141
self._sock = None
4242
self.root_path = root_path
4343

44-
def route(self, path: str, method: str = GET) -> Callable:
44+
def route(self, path: str, methods: Union[str, List[str]] = GET) -> Callable:
4545
"""
4646
Decorator used to add a route.
4747
@@ -55,13 +55,27 @@ def route(self, path: str, method: str = GET) -> Callable:
5555
def route_func(request):
5656
...
5757
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)
5970
def route_func(request, my_parameter):
6071
...
6172
"""
73+
if isinstance(methods, str):
74+
methods = [methods]
6275

6376
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)
6579
return func
6680

6781
return route_decorator

0 commit comments

Comments
 (0)