Skip to content

Commit dd22d5a

Browse files
author
Brian Villemarette
committed
Support a prefix defined during the Blueprint registration
1 parent 2b0ff5e commit dd22d5a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,9 +631,13 @@ 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:
634+
def register_blueprint(self, blueprint: "Blueprint", prefix: Optional[str] = None) -> None:
635635
"""Adds all routes defined in a blueprint"""
636636
for route, func in blueprint.api.items():
637+
if prefix and route[0] == "/":
638+
route = (prefix, *route[1:])
639+
elif prefix:
640+
route = (f"{prefix}{route[0]}" if prefix else route[0], *route[1:])
637641
self.route(*route)(func(app=self))
638642

639643

tests/functional/event_handler/test_api_gateway.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,3 +903,42 @@ def foo(app: ApiGatewayResolver, account_id):
903903
# THEN process event correctly
904904
assert result["statusCode"] == 200
905905
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
906+
907+
908+
def test_api_gateway_app_proxy_with_prefix():
909+
# GIVEN a Blueprint with registered routes
910+
# AND a prefix is defined during the registration
911+
app = ApiGatewayResolver()
912+
blueprint = Blueprint()
913+
914+
@blueprint.get(rule="/path")
915+
def foo(app: ApiGatewayResolver):
916+
return {}
917+
918+
app.register_blueprint(blueprint, prefix="/my")
919+
# WHEN calling the event handler after applying routes from blueprint object
920+
result = app(LOAD_GW_EVENT, {})
921+
922+
# THEN process event correctly
923+
assert result["statusCode"] == 200
924+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
925+
926+
927+
def test_api_gateway_app_proxy_with_prefix_equals_path():
928+
# GIVEN a Blueprint with registered routes
929+
# AND a prefix is defined during the registration
930+
app = ApiGatewayResolver()
931+
blueprint = Blueprint()
932+
933+
@blueprint.get(rule="/")
934+
def foo(app: ApiGatewayResolver):
935+
return {}
936+
937+
app.register_blueprint(blueprint, prefix="/my/path")
938+
# WHEN calling the event handler after applying routes from blueprint object
939+
# WITH the request path matching the registration prefix
940+
result = app(LOAD_GW_EVENT, {})
941+
942+
# THEN process event correctly
943+
assert result["statusCode"] == 200
944+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON

0 commit comments

Comments
 (0)