Skip to content

Commit 4ca6194

Browse files
author
Brian Villemarette
committed
Convert from callable to use route; add test; fix up formatting
1 parent 3e3bd7f commit 4ca6194

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

+26-10
Original file line numberDiff line numberDiff line change
@@ -629,41 +629,57 @@ def _to_response(self, result: Union[Dict, Response]) -> Response:
629629
def _json_dump(self, obj: Any) -> str:
630630
return self._serializer(obj)
631631

632-
class Blueprint():
632+
633+
class Blueprint:
633634
"""Blueprint helper class to allow splitting ApiGatewayResolver into multiple files"""
635+
634636
def __init__(self):
635637
self._api: Dict[tuple, Callable] = {}
636638

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,):
639+
def route(
640+
self,
641+
rule: str,
642+
method: Union[str, Tuple[str], List[str]],
643+
cors: Optional[bool] = None,
644+
compress: bool = False,
645+
cache_control: Optional[str] = None,
646+
):
638647
def actual_decorator(func: Callable):
639648
@wraps(func)
640649
def wrapper(app: ApiGatewayResolver):
641650
def inner_wrapper():
642651
return func(app)
652+
643653
return inner_wrapper
654+
644655
if isinstance(method, (list, tuple)):
645656
for item in method:
646657
self._api[(rule, item, cors, compress, cache_control)] = wrapper
647658
else:
648659
self._api[(rule, method, cors, compress, cache_control)] = wrapper
660+
649661
return actual_decorator
650662

651663
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)
664+
return self.route(rule, "GET", cors, compress, cache_control)
653665

654666
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)
667+
return self.route(rule, "POST", cors, compress, cache_control)
656668

657669
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)
670+
return self.route(rule, "PUT", cors, compress, cache_control)
659671

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)
672+
def delete(
673+
self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None
674+
):
675+
return self.route(rule, "DELETE", cors, compress, cache_control)
662676

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)
677+
def patch(
678+
self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None
679+
):
680+
return self.route(rule, "PATCH", cors, compress, cache_control)
665681

666-
def register_to_app(self, app:ApiGatewayResolver):
682+
def register_to_app(self, app: ApiGatewayResolver):
667683
"""Bind a blueprint object to an existing ApiGatewayResolver instance"""
668684
for route, func in self._api.items():
669685
app.route(*route)(func(app=app))

tests/functional/event_handler/test_api_gateway.py

+19
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from aws_lambda_powertools.event_handler import content_types
1414
from aws_lambda_powertools.event_handler.api_gateway import (
1515
ApiGatewayResolver,
16+
Blueprint,
1617
CORSConfig,
1718
ProxyEventType,
1819
Response,
@@ -842,3 +843,21 @@ def foo():
842843
# THEN process event correctly
843844
assert result["statusCode"] == 200
844845
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
846+
847+
848+
def test_api_gateway_app_proxy():
849+
# GIVEN a Blueprint with registered routes
850+
app = ApiGatewayResolver()
851+
blueprint = Blueprint()
852+
853+
@blueprint.get("/my/path")
854+
def foo(app):
855+
return {}
856+
857+
blueprint.register_to_app(app)
858+
# WHEN calling the event handler after applying routes from blueprint object
859+
result = app(LOAD_GW_EVENT, {})
860+
861+
# THEN process event correctly
862+
assert result["statusCode"] == 200
863+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON

0 commit comments

Comments
 (0)