Skip to content

Commit c0adab0

Browse files
author
Brian Villemarette
committed
Swap registration from a function of blueprints to a function of the app
1 parent 6db69bc commit c0adab0

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
@@ -631,12 +631,17 @@ def _to_response(self, result: Union[Dict, Response]) -> Response:
631631
def _json_dump(self, obj: Any) -> str:
632632
return self._serializer(obj)
633633

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

635640
class Blueprint:
636641
"""Blueprint helper class to allow splitting ApiGatewayResolver into multiple files"""
637642

638643
def __init__(self):
639-
self._api: Dict[tuple, Callable] = {}
644+
self.api: Dict[tuple, Callable] = {}
640645

641646
def route(
642647
self,
@@ -656,9 +661,9 @@ def inner_wrapper():
656661

657662
if isinstance(method, (list, tuple)):
658663
for item in method:
659-
self._api[(rule, item, cors, compress, cache_control)] = wrapper
664+
self.api[(rule, item, cors, compress, cache_control)] = wrapper
660665
else:
661-
self._api[(rule, method, cors, compress, cache_control)] = wrapper
666+
self.api[(rule, method, cors, compress, cache_control)] = wrapper
662667

663668
return actual_decorator
664669

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

tests/functional/event_handler/test_api_gateway.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -872,7 +872,7 @@ def test_api_gateway_app_proxy():
872872
def foo(app):
873873
return {}
874874

875-
blueprint.register_to_app(app)
875+
app.register_blueprint(blueprint)
876876
# WHEN calling the event handler after applying routes from blueprint object
877877
result = app(LOAD_GW_EVENT, {})
878878

0 commit comments

Comments
 (0)