Ways to store per-request context data #1107
Replies: 1 comment
-
I could be an interesting RFC in general. But for now Here is an example where def test_custom_field():
app = ApiGatewayResolver()
@app.exception_handler(ValueError)
def handle_value_error(ex: ValueError):
return Response(
status_code=418,
content_type=content_types.TEXT_HTML,
body=app.current_event["client_language"],
)
@app.get("/my/path")
def get_lambda():
# Setting a custom value via the `raw_event` dict
app.current_event.raw_event["client_language"] = "FR"
raise ValueError("Foo!")
result = app(LOAD_GW_EVENT, {})
assert result["statusCode"] == 418
assert result["headers"]["Content-Type"] == content_types.TEXT_HTML
assert result["body"] == "FR" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I was wondering if Python Powertools provide a way to store context data that is accessible from anywhere during the request lifecycle.
My use case would be passing along a variable that contains the language of the client, which could be parsed from headers/cookie/etc.
The field could then be used for example in exception handlers, to determine which language should be used when returning an HTML error page to the user. Adding a language field in my custom Python errors doesn't feel right since I raise those errors from various parts of the code, while
app.current_event
is read-only I think so I can't set fields in there.How would you solve a problem like this one? Am I missing something?
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions