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(event-handler): context support to share data between routers #1567

Conversation

heitorlessa
Copy link
Contributor

@heitorlessa heitorlessa commented Oct 4, 2022

Issue number: #1547

Summary

Changes

Please provide a summary of what's being changed

This feature introduces a context (dict) to share data between app and router instances. For safety, it auto clears any data available within context after an event is routed.

By default, we merge a router context into app's context when customers use app.include_router(). This prevents common situations where a Router naturally wants to access a key only available in an App instance: router.context["my_key"].

NOTE. The same experience will work for ALB, API Gateway REST, API Gateway HTTP, and AppSync resolvers.

User experience

Please share what the user experience looks like before and after this change

Single view where handler makes some context available

from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.utilities.typing import LambdaContext

app = APIGatewayRestResolver()


@app.get("/todos")
def my_path():
    return {"is_admin": app.context["is_admin"]}


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    app.append_context(is_admin=True, another_useful_data={})
    return app.resolve(event, context)

Accessing context from Router instances

import requests

from aws_lambda_powertools.event_handler.api_gateway import Router
from requests import Response

router = Router()


@router.get("/todos")
def get_todos():
    is_admin = router.context.get("is_admin", False)
    todos = {}

    if is_admin:
        todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
        todos.raise_for_status()
        todos = todos.json()

    return {"todos": todos}

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Oct 4, 2022
@github-actions github-actions bot added the feature New feature or functionality label Oct 4, 2022
@heitorlessa heitorlessa linked an issue Oct 4, 2022 that may be closed by this pull request
2 tasks
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Oct 4, 2022
@pull-request-size pull-request-size bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 4, 2022
@heitorlessa heitorlessa marked this pull request as ready for review October 4, 2022 14:04
@heitorlessa heitorlessa requested a review from a team as a code owner October 4, 2022 14:04
@heitorlessa heitorlessa requested review from rubenfonseca and removed request for a team October 4, 2022 14:04
@heitorlessa heitorlessa changed the title feat(apigateway): context support to share data between routers feat(event-handler): context support to share data between routers Oct 4, 2022
@leandrodamascena leandrodamascena requested review from leandrodamascena and removed request for rubenfonseca October 4, 2022 14:27
@leandrodamascena leandrodamascena self-assigned this Oct 4, 2022
Copy link
Contributor

@leandrodamascena leandrodamascena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested many scenarios locally and running on Lambda and everything is working perfectly!

Congrats @heitorlessa 🚀 💯 🏅

@leandrodamascena leandrodamascena merged commit 5500dff into aws-powertools:develop Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RFC: Pass custom context dict to resolve method
2 participants