[go: nahoru, domu]

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

add context variables and stage variables to API NG context #11111

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

bentsku
Copy link
Contributor
@bentsku bentsku commented Jun 28, 2024

Motivation

We need a separate context, representing the $context variables used to render VTL templates, and which is also passed in the payload to AWS_PROXY integrations.

More informations about $context here.
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference

It is a bit of duplicated information from the original RestApiInvocationContext object and the InvocationRequest, but they serve different purposes.

If we need to update different contexts at the same time, then maybe we could simplify. But we learned that creating those context when needed, collecting informations from different sources, can be pretty hard.

The InvocationRequest goal is simply to represent the incoming request "as is", with no transformation.

The idea would be to go from InvocationRequest, get the Resource. Use the original InvocationRequest, the Resource,the ContextVariables and StageVariables to construct the IntegrationRequest and send it down.
The InvocationRequest will not be mutated. The ContextVariables will be mutated and populated along the way of the different handlers.

Changes

  • add the ContextVariables and populate it
  • add the not yet used LoggingContextVariables which can add more data to the logging
  • add IntegrationRequest for future work in integration request handler
  • add the stage variables and fetch them from the stage
  • add small tests to validate we're populate the contexts properly

TODO

What's left to do:

  • add unit tests

@bentsku bentsku added aws:apigateway Amazon API Gateway semver: patch Non-breaking changes which can be included in patch releases labels Jun 28, 2024
@bentsku bentsku self-assigned this Jun 28, 2024
Copy link
github-actions bot commented Jun 28, 2024

LocalStack Community integration with Pro

  2 files  ±    0    2 suites  ±0   23m 3s ⏱️ - 1h 14m 45s
643 tests  - 2 517  598 ✅  - 2 162  45 💤  - 351  0 ❌ ±0 
645 runs   - 2 517  598 ✅  - 2 162  47 💤  - 351  0 ❌ ±0 

Results for commit f96581c. ± Comparison against base commit adc580c.

This pull request removes 2517 tests.
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_lambda_dynamodb
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_opensearch_crud
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_search_books
tests.aws.scenario.bookstore.test_bookstore.TestBookstoreApplication ‑ test_setup
tests.aws.scenario.kinesis_firehose.test_kinesis_firehose.TestKinesisFirehoseScenario ‑ test_kinesis_firehose_s3
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_destination_sns
tests.aws.scenario.lambda_destination.test_lambda_destination_scenario.TestLambdaDestinationScenario ‑ test_infra
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_prefill_dynamodb_table
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input0-SUCCEEDED]
tests.aws.scenario.loan_broker.test_loan_broker.TestLoanBrokerScenario ‑ test_stepfunctions_input_recipient_list[step_function_input1-SUCCEEDED]
…

♻️ This comment has been updated with latest results.

@bentsku bentsku marked this pull request as ready for review June 28, 2024 13:21
Copy link
Contributor
@cloutierMat cloutierMat left a comment

Choose a reason for hiding this comment

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

Awesome! I can see so much extra code and repeated functionality we will be able to remove by having the context variables built and maintained together like that!

The split in integration request already makes it very clear as to what is meant to happen and this clear display of the logic's order will help maintaining the service 🚀

Nice additions to the tests too 😉

Comment on lines +38 to +52
class IntegrationRequest(TypedDict, total=False):
http_method: Optional[HTTPMethod]
"""HTTP Method of the incoming request"""
uri: Optional[str]
"""URI of the integration"""
query_string_parameters: Optional[dict[str, str]]
"""Query string parameters of the request"""
headers: Optional[dict[str, str]]
"""Headers of the request"""
multi_value_query_string_parameters: Optional[dict[str, list[str]]]
"""Multi value query string parameters of the request"""
multi_value_headers: Optional[dict[str, list[str]]]
"""Multi value headers of the request"""
body: Optional[bytes]
"""Body content of the request"""
Copy link
Contributor

Choose a reason for hiding this comment

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

Having that clear separation from the invocation request will be key in maintaining transparency over the requests.

requestTimeEpoch=int(now.timestamp() * 1000),
stage=context.stage,
)
return context_variables
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we log this dict immediately?

def update_context_variables_with_resource(
context_variables: ContextVariables, resource: Resource
):
# TODO: log updating the context_variables?
Copy link
Contributor

Choose a reason for hiding this comment

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

It is fine to leave as a TODO, but I think it would really useful to log them in debug mode.

Comment on lines +5 to +6
# this is merged with the Context returned by the Authorizer, which can attach any property to this dict in string
# format
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
aws:apigateway Amazon API Gateway semver: patch Non-breaking changes which can be included in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants