Skip to content

Commit 3e3bd7f

Browse files
authored
Update api_gateway.py
Add Blueprint class to ApiGatewayResolver
1 parent c8cf3ba commit 3e3bd7f

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+41-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import traceback
77
import zlib
88
from enum import Enum
9-
from functools import partial
9+
from functools import partial, wraps
1010
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
1212

1313
from aws_lambda_powertools.event_handler import content_types
1414
from aws_lambda_powertools.event_handler.exceptions import ServiceError
@@ -628,3 +628,42 @@ def _to_response(self, result: Union[Dict, Response]) -> Response:
628628

629629
def _json_dump(self, obj: Any) -> str:
630630
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

Comments
 (0)