|
10 | 10 | from enum import Enum
|
11 | 11 | from functools import partial
|
12 | 12 | from http import HTTPStatus
|
13 |
| -from typing import Any, Callable, Dict, List, Optional, Set, Union |
| 13 | +from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union |
14 | 14 |
|
15 | 15 | from aws_lambda_powertools.event_handler import content_types
|
16 | 16 | from aws_lambda_powertools.event_handler.exceptions import ServiceError
|
@@ -453,27 +453,30 @@ def __init__(
|
453 | 453 | def route(
|
454 | 454 | self,
|
455 | 455 | rule: str,
|
456 |
| - method: str, |
| 456 | + method: Union[str, Union[List[str], Tuple[str]]], |
457 | 457 | cors: Optional[bool] = None,
|
458 | 458 | compress: bool = False,
|
459 | 459 | cache_control: Optional[str] = None,
|
460 | 460 | ):
|
461 | 461 | """Route decorator includes parameter `method`"""
|
462 | 462 |
|
463 | 463 | def register_resolver(func: Callable):
|
464 |
| - logger.debug(f"Adding route using rule {rule} and method {method.upper()}") |
| 464 | + methods = (method,) if isinstance(method, str) else method |
| 465 | + logger.debug(f"Adding route using rule {rule} and methods: {','.join((m.upper() for m in methods))}") |
465 | 466 | if cors is None:
|
466 | 467 | cors_enabled = self._cors_enabled
|
467 | 468 | else:
|
468 | 469 | cors_enabled = cors
|
469 |
| - self._routes.append(Route(method, self._compile_regex(rule), func, cors_enabled, compress, cache_control)) |
470 |
| - route_key = method + rule |
471 |
| - if route_key in self._route_keys: |
472 |
| - warnings.warn(f"A route like this was already registered. method: '{method}' rule: '{rule}'") |
473 |
| - self._route_keys.append(route_key) |
474 |
| - if cors_enabled: |
475 |
| - logger.debug(f"Registering method {method.upper()} to Allow Methods in CORS") |
476 |
| - self._cors_methods.add(method.upper()) |
| 470 | + |
| 471 | + for item in methods: |
| 472 | + self._routes.append(Route(item, self._compile_regex(rule), func, cors_enabled, compress, cache_control)) |
| 473 | + route_key = item + rule |
| 474 | + if route_key in self._route_keys: |
| 475 | + warnings.warn(f"A route like this was already registered. method: '{item}' rule: '{rule}'") |
| 476 | + self._route_keys.append(route_key) |
| 477 | + if cors_enabled: |
| 478 | + logger.debug(f"Registering method {item.upper()} to Allow Methods in CORS") |
| 479 | + self._cors_methods.add(item.upper()) |
477 | 480 | return func
|
478 | 481 |
|
479 | 482 | return register_resolver
|
@@ -679,14 +682,13 @@ def __init__(self):
|
679 | 682 | def route(
|
680 | 683 | self,
|
681 | 684 | rule: str,
|
682 |
| - method: Union[str, List[str]], |
| 685 | + method: Union[str, Union[List[str], Tuple[str]]], |
683 | 686 | cors: Optional[bool] = None,
|
684 | 687 | compress: bool = False,
|
685 | 688 | cache_control: Optional[str] = None,
|
686 | 689 | ):
|
687 | 690 | def register_route(func: Callable):
|
688 |
| - methods = method if isinstance(method, list) else [method] |
689 |
| - for item in methods: |
690 |
| - self._routes[(rule, item, cors, compress, cache_control)] = func |
| 691 | + methods = (method,) if isinstance(method, str) else tuple(method) |
| 692 | + self._routes[(rule, methods, cors, compress, cache_control)] = func |
691 | 693 |
|
692 | 694 | return register_route
|
0 commit comments