MousaZeidBaker / aws-lambda-typing

Python type hints for AWS Lambda
MIT License
106 stars 19 forks source link

Add API Gateway "HTTP" API & authorization events & responses #99

Open vergenzt opened 9 months ago

vergenzt commented 9 months ago

Opening this as a draft for now since it's not really ready to be merged. I'm using it locally via a git dependency on my fork in the mean time.

I'm not super happy with where it's at right now, partly because I tried to roll two different changes into one PR. šŸ™‚ I may file a separate issue to reflect the second change I was trying to make, and split it out of this PR.

  1. Add the new HTTP API & authorization TypedDicts for reference
  2. Make the relevant TypedDicts generic, parameterized by the shape of the user's custom context response that they may return in a Lambda authorizer

Unfortunately I'm getting stuck in a sticky typing situation with #2 that I think has no workaround: There doesn't appear to be a way to declare a generic TypedDict (which requires TypedDict class syntax) where one of the generically-typed members has a key that is not a valid identifier (which requires TypedDict expression syntax). šŸ˜•

In this case because the user's custom authorization context ends up being passed into the APIGatewayProxyEventV2's authorizer object under a key called "lambda" (which is a Python keyword), I've found no way to construct a TypedDict with a generic type for the "lambda" key, and thus seem to be at a dead end with making the events generic.

(My motivation for doing this was so that static type checking can ensure I'm expecting the same authorizer context shape in my event handlers as I return from the authorization handler.)

Will address #98 once properly cleaned up