|
6 | 6 | import traceback
|
7 | 7 | import zlib
|
8 | 8 | from enum import Enum
|
9 |
| -from functools import partial |
| 9 | +from functools import partial, wraps |
10 | 10 | from http import HTTPStatus
|
11 |
| -from typing import Any, Callable, Dict, List, Optional, Set, Union |
| 11 | +from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union |
12 | 12 |
|
13 | 13 | from aws_lambda_powertools.event_handler import content_types
|
14 | 14 | from aws_lambda_powertools.event_handler.exceptions import ServiceError
|
@@ -628,3 +628,42 @@ def _to_response(self, result: Union[Dict, Response]) -> Response:
|
628 | 628 |
|
629 | 629 | def _json_dump(self, obj: Any) -> str:
|
630 | 630 | return self._serializer(obj)
|
| 631 | + |
| 632 | +class Blueprint(): |
| 633 | + """Blueprint helper class to allow splitting ApiGatewayResolver into multiple files""" |
| 634 | + def __init__(self): |
| 635 | + self._api: Dict[tuple, Callable] = {} |
| 636 | + |
| 637 | + def __call__(self, rule: str, method: Union[str, Tuple[str], List[str]], cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None,): |
| 638 | + def actual_decorator(func: Callable): |
| 639 | + @wraps(func) |
| 640 | + def wrapper(app: ApiGatewayResolver): |
| 641 | + def inner_wrapper(): |
| 642 | + return func(app) |
| 643 | + return inner_wrapper |
| 644 | + if isinstance(method, (list, tuple)): |
| 645 | + for item in method: |
| 646 | + self._api[(rule, item, cors, compress, cache_control)] = wrapper |
| 647 | + else: |
| 648 | + self._api[(rule, method, cors, compress, cache_control)] = wrapper |
| 649 | + return actual_decorator |
| 650 | + |
| 651 | + def get(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None): |
| 652 | + return self.__call__(rule, "GET", cors, compress, cache_control) |
| 653 | + |
| 654 | + def post(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None): |
| 655 | + return self.__call__(rule, "POST", cors, compress, cache_control) |
| 656 | + |
| 657 | + def put(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None): |
| 658 | + return self.__call__(rule, "PUT", cors, compress, cache_control) |
| 659 | + |
| 660 | + def delete(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None): |
| 661 | + return self.__call__(rule, "DELETE", cors, compress, cache_control) |
| 662 | + |
| 663 | + def patch(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None): |
| 664 | + return self.__call__(rule, "PATCH", cors, compress, cache_control) |
| 665 | + |
| 666 | + def register_to_app(self, app:ApiGatewayResolver): |
| 667 | + """Bind a blueprint object to an existing ApiGatewayResolver instance""" |
| 668 | + for route, func in self._api.items(): |
| 669 | + app.route(*route)(func(app=app)) |
0 commit comments