From 13392789967e0e04e9973a07aa57e379cef765bc Mon Sep 17 00:00:00 2001 From: Justice Sidhu Date: Fri, 5 Jan 2024 13:45:40 -0800 Subject: [PATCH 1/4] remove version from rest api payload --- samcli/local/apigw/event_constructor.py | 8 +++++-- samcli/local/apigw/local_apigw_service.py | 1 + samcli/local/events/api_event.py | 12 ++++++++-- .../local/apigw/test_local_apigw_service.py | 2 ++ tests/unit/local/events/test_api_event.py | 22 ++++++++++++++++++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/samcli/local/apigw/event_constructor.py b/samcli/local/apigw/event_constructor.py index 441d94d20e..b9e07ecb8d 100644 --- a/samcli/local/apigw/event_constructor.py +++ b/samcli/local/apigw/event_constructor.py @@ -9,6 +9,7 @@ from typing import Any, Dict from samcli.local.apigw.path_converter import PathConverter +from samcli.local.apigw.route import Route from samcli.local.events.api_event import ( ApiGatewayLambdaEvent, ApiGatewayV2LambdaEvent, @@ -22,16 +23,18 @@ def construct_v1_event( - flask_request, port, binary_types, stage_name=None, stage_variables=None, operation_name=None + flask_request, port, binary_types, stage_name=None, stage_variables=None, operation_name=None, api_type=Route.API ) -> Dict[str, Any]: """ - Helper method that constructs the Event to be passed to Lambda + Helper method that constructs the Event to be passed to Lambda. + Used for Http apis with payload v1 and Rest apis because the payloads are almost identical :param request flask_request: Flask Request :param port: the port number :param binary_types: list of binary types :param stage_name: Optional, the stage name string :param stage_variables: Optional, API Gateway Stage Variables + :param api_type: Optional, the type of api payload being constructed :return: JSON object """ @@ -86,6 +89,7 @@ def construct_v1_event( path=flask_request.path, is_base_64_encoded=is_base_64, stage_variables=stage_variables, + api_type=api_type, ) event_dict = event.to_dict() diff --git a/samcli/local/apigw/local_apigw_service.py b/samcli/local/apigw/local_apigw_service.py index 80da1f4480..df82b68ae4 100644 --- a/samcli/local/apigw/local_apigw_service.py +++ b/samcli/local/apigw/local_apigw_service.py @@ -477,6 +477,7 @@ def _generate_lambda_event(self, flask_request: Request, route: Route, method: s stage_name=self.api.stage_name, stage_variables=self.api.stage_variables, operation_name=route_key, + api_type=route.event_type, ) def _build_v1_context(self, route: Route) -> Dict[str, Any]: diff --git a/samcli/local/events/api_event.py b/samcli/local/events/api_event.py index 1b82c7caea..17e70d9f50 100644 --- a/samcli/local/events/api_event.py +++ b/samcli/local/events/api_event.py @@ -4,6 +4,8 @@ from time import time from typing import Any, Dict +from samcli.local.apigw.route import Route + class ContextIdentity: def __init__( @@ -169,6 +171,7 @@ def __init__( stage_variables=None, path=None, is_base_64_encoded=False, + api_type=Route.API, ): """ Constructs an ApiGatewayLambdaEvent @@ -205,7 +208,9 @@ def __init__( if not isinstance(stage_variables, dict) and stage_variables is not None: raise TypeError("'stage_variables' must be of type dict or None") - self.version = "1.0" + # v1 payloads and rest api payloads are identical save for the version field + if api_type == Route.HTTP: + self.version = "1.0" self.http_method = http_method self.body = body self.resource = resource @@ -218,6 +223,7 @@ def __init__( self.stage_variables = stage_variables self.path = path self.is_base_64_encoded = is_base_64_encoded + self.api_type = api_type def to_dict(self) -> Dict[str, Any]: """ @@ -233,7 +239,6 @@ def to_dict(self) -> Dict[str, Any]: request_context_dict = self.request_context.to_dict() json_dict = { - "version": self.version, "httpMethod": self.http_method, "body": self.body if self.body else None, "resource": self.resource, @@ -250,6 +255,9 @@ def to_dict(self) -> Dict[str, Any]: "isBase64Encoded": self.is_base_64_encoded, } + if self.api_type == Route.HTTP: + json_dict["version"] = "1.0" + return json_dict diff --git a/tests/unit/local/apigw/test_local_apigw_service.py b/tests/unit/local/apigw/test_local_apigw_service.py index e77ce996ac..c3442aaf06 100644 --- a/tests/unit/local/apigw/test_local_apigw_service.py +++ b/tests/unit/local/apigw/test_local_apigw_service.py @@ -123,6 +123,7 @@ def test_api_request_must_invoke_lambda(self, v2_event_mock, v1_event_mock, requ stage_name=ANY, stage_variables=ANY, operation_name="getRestApi", + api_type=Route.API, ) @patch.object(LocalApigwService, "get_request_methods_endpoints") @@ -190,6 +191,7 @@ def test_http_v1_payload_request_must_invoke_lambda(self, v2_event_mock, v1_even stage_name=ANY, stage_variables=ANY, operation_name=None, + api_type=Route.HTTP, ) @patch.object(LocalApigwService, "get_request_methods_endpoints") diff --git a/tests/unit/local/events/test_api_event.py b/tests/unit/local/events/test_api_event.py index e4e3d26e65..6cd3e6c3b0 100644 --- a/tests/unit/local/events/test_api_event.py +++ b/tests/unit/local/events/test_api_event.py @@ -3,6 +3,7 @@ from time import time from datetime import datetime +from samcli.local.apigw.route import Route from samcli.local.events.api_event import ( ContextIdentity, @@ -335,7 +336,6 @@ def test_to_dict(self): ) expected = { - "version": "1.0", "httpMethod": "request_method", "body": "request_data", "resource": "resource", @@ -355,6 +355,26 @@ def test_to_dict(self): def test_to_dict_with_defaults(self): event = ApiGatewayLambdaEvent() + expected = { + "httpMethod": None, + "body": None, + "resource": None, + "requestContext": {}, + "queryStringParameters": None, + "multiValueQueryStringParameters": None, + "headers": None, + "multiValueHeaders": None, + "pathParameters": None, + "stageVariables": None, + "path": None, + "isBase64Encoded": False, + } + + self.assertEqual(event.to_dict(), expected) + + def test_to_dict_with_http_v1(self): + event = ApiGatewayLambdaEvent(api_type=Route.HTTP) + expected = { "version": "1.0", "httpMethod": None, From c5c9d475346dfe1fa021479ebab89487f88118be Mon Sep 17 00:00:00 2001 From: Justice Sidhu Date: Fri, 5 Jan 2024 16:00:43 -0800 Subject: [PATCH 2/4] remove unused variable --- samcli/local/events/api_event.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samcli/local/events/api_event.py b/samcli/local/events/api_event.py index 17e70d9f50..7c8e9e182b 100644 --- a/samcli/local/events/api_event.py +++ b/samcli/local/events/api_event.py @@ -208,9 +208,6 @@ def __init__( if not isinstance(stage_variables, dict) and stage_variables is not None: raise TypeError("'stage_variables' must be of type dict or None") - # v1 payloads and rest api payloads are identical save for the version field - if api_type == Route.HTTP: - self.version = "1.0" self.http_method = http_method self.body = body self.resource = resource @@ -255,6 +252,7 @@ def to_dict(self) -> Dict[str, Any]: "isBase64Encoded": self.is_base_64_encoded, } + # v1 payloads and rest api payloads are identical save for the version field if self.api_type == Route.HTTP: json_dict["version"] = "1.0" From 9f3923e7d92115262954b09868ddfea2fd4334a1 Mon Sep 17 00:00:00 2001 From: Justice Sidhu Date: Mon, 8 Jan 2024 16:10:28 -0800 Subject: [PATCH 3/4] update documentation --- samcli/local/events/api_event.py | 1 + 1 file changed, 1 insertion(+) diff --git a/samcli/local/events/api_event.py b/samcli/local/events/api_event.py index 7c8e9e182b..67dec47c55 100644 --- a/samcli/local/events/api_event.py +++ b/samcli/local/events/api_event.py @@ -188,6 +188,7 @@ def __init__( :param dict stage_variables: API Gateway Stage Variables :param str path: Path of the request :param bool is_base_64_encoded: True if the data is base64 encoded. + :param str api_type: The type of API the event is being generated for """ if not isinstance(query_string_params, dict) and query_string_params is not None: From 2815ba54bf2854adfd5583ba2e194c6bade5ab30 Mon Sep 17 00:00:00 2001 From: Justice Sidhu Date: Mon, 8 Jan 2024 16:15:28 -0800 Subject: [PATCH 4/4] fix format error --- samcli/local/events/api_event.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samcli/local/events/api_event.py b/samcli/local/events/api_event.py index 67dec47c55..f73e554d9a 100644 --- a/samcli/local/events/api_event.py +++ b/samcli/local/events/api_event.py @@ -188,7 +188,7 @@ def __init__( :param dict stage_variables: API Gateway Stage Variables :param str path: Path of the request :param bool is_base_64_encoded: True if the data is base64 encoded. - :param str api_type: The type of API the event is being generated for + :param str api_type: The type of API the event is being generated for """ if not isinstance(query_string_params, dict) and query_string_params is not None: