Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(apigateway): access parent api resolver from router #842

Merged
merged 6 commits into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ def include_router(self, router: "Router", prefix: Optional[str] = None) -> None
prefix : str, optional
An optional prefix to be added to the originally defined rule
"""

# Add reference to parent ApiGatewayResolver to support use cases where people subclass it to add custom logic
router.api_resolver = self

for route, func in router._routes.items():
if prefix:
rule = route[0]
Expand All @@ -678,6 +682,7 @@ class Router(BaseRouter):

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

def route(
self,
Expand Down
19 changes: 19 additions & 0 deletions tests/functional/event_handler/test_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,3 +1057,22 @@ def foo(account_id):
assert post_result["headers"]["Content-Type"] == content_types.APPLICATION_JSON
assert put_result["statusCode"] == 404
assert put_result["headers"]["Content-Type"] == content_types.APPLICATION_JSON


def test_api_gateway_app_router_access_to_resolver():
# GIVEN a Router with registered routes
app = ApiGatewayResolver()
router = Router()

@router.get("/my/path")
def foo():
# WHEN accessing the api resolver instance via the router
# THEN it is accessible and equal to the instantiated api resolver
assert app == router.api_resolver
return {}

app.include_router(router)
result = app(LOAD_GW_EVENT, {})

assert result["statusCode"] == 200
assert result["headers"]["Content-Type"] == content_types.APPLICATION_JSON