[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

Apigw NG create method request handler #11096

Merged
merged 6 commits into from
Jun 26, 2024

Conversation

cloutierMat
Copy link
Contributor

Motivation

This PR ports the functionality from apigateway/invocations.py:RequestValidator to a method request handler.

The old invocation process wasn't routing the request before validation so some "passthrough" were required when no matching method were found. Cleaning up some of that code will make it cleaner and more obvious as to what is happening.

We can also start raising error from the gateway responses to be prepared for it's future implmentation.

Changes

  • updated the method request handler to validate the request parameters and the body.
  • removed passthrough when no methods exists
  • raise proper gateway exceptions
  • added tests porting from the previous unit tests of the RequestValidator to ensure the functionality hasn't regressed.
  • added a debug statement when the ModelResolver fails to find it's ref. There might be more work to do here, but that is the statement that helped me understand why my model wasn't resolving! :)

Out of scope

While there could be an argument to include validation of api key as part of this handler as they are configured together, they are separate enough in implementation that we can simplify readability/maintainability of the code by further breaking it down into it's own handler. All of the api key validation is done before request validation so a new handler seems like the best solution.

@cloutierMat cloutierMat added aws:apigateway Amazon API Gateway semver: minor Non-breaking changes which can be included in minor releases, but not in patch releases labels Jun 26, 2024
@cloutierMat cloutierMat added this to the 3.6 milestone Jun 26, 2024
@cloutierMat cloutierMat self-assigned this Jun 26, 2024
Copy link
github-actions bot commented Jun 26, 2024

LocalStack Community integration with Pro

  2 files  ±    0    2 suites  ±0   22m 46s ⏱️ - 1h 13m 50s
643 tests  - 2 508  598 ✅  - 2 157  45 💤  - 351  0 ❌ ±0 
645 runs   - 2 508  598 ✅  - 2 157  47 💤  - 351  0 ❌ ±0 

Results for commit 15f8768. ± Comparison against base commit 02302df.

This pull request removes 2508 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.

Copy link
Contributor
@bentsku bentsku left a comment

Choose a reason for hiding this comment

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

LGTM! Nice refactor of the code 🚀 Just a minor comment regarding the testing, still not sure what our strategy should be regarding testing of handlers... I'd lean towards testing handlers in isolation, and should we need it, only instantiate a chain if we need different handlers executed one after another? What do you think?

Comment on lines 21 to 27
@pytest.fixture
def method_handler_chain() -> RestApiGatewayHandlerChain:
"""Returns a dummy chain for testing."""
method_handler_chain = RestApiGatewayHandlerChain(request_handlers=[MethodRequestHandler()])
# Sets raise_on_error to allow capturing the Exceptions raised by the validator
method_handler_chain.raise_on_error = True
return method_handler_chain
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: should we use the handler chain with only handler, or should we directly instantiate the handler in the tests, so that we would not need to set method_handler_chain.error = None after every invocation?

In our Gateway, we instantiate a new handler chain for every incoming request.
We could maybe make the intent clearer by only testing the handler itself?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you are right, I will see how I can make it cleaner by calling the handler itself! 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Very neat update 💯 I like that, I think we should follow the same pattern for new tests, with the invoker!

@cloutierMat
Copy link
Contributor Author

I think you are right for the chain creation.

My first thoughts were to also use the gateway response handlers and make assertion on the responses. But decided against it as it will be better as integration and snapshot tests later.

I updated the tests to use only the handler, I think it is much cleaner that way! Thanks for the comment 🚀

@cloutierMat cloutierMat force-pushed the apigw-ng-create-method-request-handler branch from 45dd6be to 15f8768 Compare June 26, 2024 17:58
@cloutierMat cloutierMat merged commit cb71770 into master Jun 26, 2024
31 checks passed
@cloutierMat cloutierMat deleted the apigw-ng-create-method-request-handler branch June 26, 2024 18:20
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: minor Non-breaking changes which can be included in minor releases, but not in patch releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants