Skip to content

Commit be6e722

Browse files
author
Tom McCarthy
authored
feat(apigateway): access parent api resolver from router (#842)
1 parent fe2cf13 commit be6e722

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,10 @@ def include_router(self, router: "Router", prefix: Optional[str] = None) -> None
664664
prefix : str, optional
665665
An optional prefix to be added to the originally defined rule
666666
"""
667+
668+
# Add reference to parent ApiGatewayResolver to support use cases where people subclass it to add custom logic
669+
router.api_resolver = self
670+
667671
for route, func in router._routes.items():
668672
if prefix:
669673
rule = route[0]
@@ -678,6 +682,7 @@ class Router(BaseRouter):
678682

679683
def __init__(self):
680684
self._routes: Dict[tuple, Callable] = {}
685+
self.api_resolver: Optional[BaseRouter] = None
681686

682687
def route(
683688
self,

tests/functional/event_handler/test_api_gateway.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,3 +1057,22 @@ def foo(account_id):
10571057
assert post_result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
10581058
assert put_result["statusCode"] == 404
10591059
assert put_result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
1060+
1061+
1062+
def test_api_gateway_app_router_access_to_resolver():
1063+
# GIVEN a Router with registered routes
1064+
app = ApiGatewayResolver()
1065+
router = Router()
1066+
1067+
@router.get("/my/path")
1068+
def foo():
1069+
# WHEN accessing the api resolver instance via the router
1070+
# THEN it is accessible and equal to the instantiated api resolver
1071+
assert app == router.api_resolver
1072+
return {}
1073+
1074+
app.include_router(router)
1075+
result = app(LOAD_GW_EVENT, {})
1076+
1077+
assert result["statusCode"] == 200
1078+
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON

0 commit comments

Comments
 (0)