Skip to content

RFC: Pass custom context dict to resolve method #1547

Closed
@MaartenUreel

Description

@MaartenUreel

Is this related to an existing feature request or issue?

#1546

Which AWS Lambda Powertools utility does this relate to?

Event Handler - REST API

Summary

It would be useful if you could provide an optional dict to the app.resolve() method, that would then be available in the handlers themselves.
This would contain generic information that is needed for all requests.

Use case

We are implementing callbacks for Twilio. For every event, we need to:

  1. Decode the body
  2. Parse the parameters
  3. Validate the request

Next, we need the same parameters from step 2 in almost every request.

Proposal

Provide an extra parameter to the resolve() method:

app.py

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

import todos


app = APIGatewayRestResolver()
app.include_router(todos.router)


def lambda_handler(event: dict, context: LambdaContext) -> dict:
    some_info = {"some_custom": "data"}
    return app.resolve(event, context, route_context=some_info)

In the handler itself, it could then be accessible via route_context:

todos.py

import requests

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

router = Router()


@router.get("/todos")
def get_todos():
    some_data = router.route_context.get("some_custom")
   
    todos: Response = requests.get("https://jsonplaceholder.typicode.com/todos")
    todos.raise_for_status()

    return {"todos": todos.json()[:10]}

Out of scope

n/a

Potential challenges

n/a

Dependencies and Integrations

No response

Alternative solutions

Currently I'm passing the data in app.lambda_context.__dict__.

Acknowledgment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions