Skip to content

Commit 26c6bac

Browse files
BVMikoBrian Villemarette
authored and
Brian Villemarette
committed
Update api_gateway.py
Add Blueprint class to ApiGatewayResolver
1 parent c21ba45 commit 26c6bac

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 41 additions & 2 deletions
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
@@ -630,3 +630,42 @@ def _to_response(self, result: Union[Dict, Response]) -> Response:
630630

631631
def _json_dump(self, obj: Any) -> str:
632632
return self._serializer(obj)
633+
634+
class Blueprint():
635+
"""Blueprint helper class to allow splitting ApiGatewayResolver into multiple files"""
636+
def __init__(self):
637+
self._api: Dict[tuple, Callable] = {}
638+
639+
def __call__(self, rule: str, method: Union[str, Tuple[str], List[str]], cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None,):
640+
def actual_decorator(func: Callable):
641+
@wraps(func)
642+
def wrapper(app: ApiGatewayResolver):
643+
def inner_wrapper():
644+
return func(app)
645+
return inner_wrapper
646+
if isinstance(method, (list, tuple)):
647+
for item in method:
648+
self._api[(rule, item, cors, compress, cache_control)] = wrapper
649+
else:
650+
self._api[(rule, method, cors, compress, cache_control)] = wrapper
651+
return actual_decorator
652+
653+
def get(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None):
654+
return self.__call__(rule, "GET", cors, compress, cache_control)
655+
656+
def post(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None):
657+
return self.__call__(rule, "POST", cors, compress, cache_control)
658+
659+
def put(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None):
660+
return self.__call__(rule, "PUT", cors, compress, cache_control)
661+
662+
def delete(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None):
663+
return self.__call__(rule, "DELETE", cors, compress, cache_control)
664+
665+
def patch(self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None):
666+
return self.__call__(rule, "PATCH", cors, compress, cache_control)
667+
668+
def register_to_app(self, app:ApiGatewayResolver):
669+
"""Bind a blueprint object to an existing ApiGatewayResolver instance"""
670+
for route, func in self._api.items():
671+
app.route(*route)(func(app=app))

0 commit comments

Comments
 (0)