Skip to content

Commit 46efbd4

Browse files
author
Brian Villemarette
committed
Swap registration from a function of blueprints to a function of the app
1 parent 4ca6194 commit 46efbd4

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

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

632+
def register_blueprint(self, blueprint: "Blueprint") -> None:
633+
"""Adds all routes defined in a blueprint"""
634+
for route, func in blueprint.api.items():
635+
self.route(*route)(func(app=self))
636+
632637

633638
class Blueprint:
634639
"""Blueprint helper class to allow splitting ApiGatewayResolver into multiple files"""
635640

636641
def __init__(self):
637-
self._api: Dict[tuple, Callable] = {}
642+
self.api: Dict[tuple, Callable] = {}
638643

639644
def route(
640645
self,
@@ -654,9 +659,9 @@ def inner_wrapper():
654659

655660
if isinstance(method, (list, tuple)):
656661
for item in method:
657-
self._api[(rule, item, cors, compress, cache_control)] = wrapper
662+
self.api[(rule, item, cors, compress, cache_control)] = wrapper
658663
else:
659-
self._api[(rule, method, cors, compress, cache_control)] = wrapper
664+
self.api[(rule, method, cors, compress, cache_control)] = wrapper
660665

661666
return actual_decorator
662667

@@ -678,8 +683,3 @@ def patch(
678683
self, rule: str, cors: Optional[bool] = None, compress: bool = False, cache_control: Optional[str] = None
679684
):
680685
return self.route(rule, "PATCH", cors, compress, cache_control)
681-
682-
def register_to_app(self, app: ApiGatewayResolver):
683-
"""Bind a blueprint object to an existing ApiGatewayResolver instance"""
684-
for route, func in self._api.items():
685-
app.route(*route)(func(app=app))

tests/functional/event_handler/test_api_gateway.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ def test_api_gateway_app_proxy():
854854
def foo(app):
855855
return {}
856856

857-
blueprint.register_to_app(app)
857+
app.register_blueprint(blueprint)
858858
# WHEN calling the event handler after applying routes from blueprint object
859859
result = app(LOAD_GW_EVENT, {})
860860

0 commit comments

Comments
 (0)