From 220339700a37b5d5651b30c9af7190f825d66296 Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Tue, 7 Apr 2026 20:50:39 +0800 Subject: [PATCH 1/2] chore: refactoring proxy events --- .../event_handler/api_gateway.py | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index ad1e14d3122..d74463df1b7 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -113,6 +113,17 @@ class ProxyEventType(Enum): LambdaFunctionUrlEvent = "LambdaFunctionUrlEvent" +_PROXY_EVENT_MAP: dict[ProxyEventType, tuple[type[BaseProxyEvent], str]] = { + ProxyEventType.APIGatewayProxyEvent: (APIGatewayProxyEvent, "API Gateway REST API"), + ProxyEventType.APIGatewayProxyEventV2: (APIGatewayProxyEventV2, "API Gateway HTTP API"), + ProxyEventType.BedrockAgentEvent: (BedrockAgentEvent, "Bedrock Agent"), + ProxyEventType.LambdaFunctionUrlEvent: (LambdaFunctionUrlEvent, "Lambda Function URL"), + ProxyEventType.VPCLatticeEvent: (VPCLatticeEvent, "VPC Lattice"), + ProxyEventType.VPCLatticeEventV2: (VPCLatticeEventV2, "VPC LatticeV2"), + ProxyEventType.ALBEvent: (ALBEvent, "ALB"), +} + + class CORSConfig: """CORS Config @@ -2498,28 +2509,11 @@ def _compile_regex(rule: str, base_regex: str = _ROUTE_REGEX): rule_regex: str = re.sub(_DYNAMIC_ROUTE_PATTERN, _NAMED_GROUP_BOUNDARY_PATTERN, rule) return re.compile(base_regex.format(rule_regex)) - def _to_proxy_event(self, event: dict) -> BaseProxyEvent: # noqa: PLR0911 # ignore many returns + def _to_proxy_event(self, event: dict) -> BaseProxyEvent: """Convert the event dict to the corresponding data class""" - if self._proxy_type == ProxyEventType.APIGatewayProxyEvent: - logger.debug("Converting event to API Gateway REST API contract") - return APIGatewayProxyEvent(event, self._json_body_deserializer) - if self._proxy_type == ProxyEventType.APIGatewayProxyEventV2: - logger.debug("Converting event to API Gateway HTTP API contract") - return APIGatewayProxyEventV2(event, self._json_body_deserializer) - if self._proxy_type == ProxyEventType.BedrockAgentEvent: - logger.debug("Converting event to Bedrock Agent contract") - return BedrockAgentEvent(event, self._json_body_deserializer) - if self._proxy_type == ProxyEventType.LambdaFunctionUrlEvent: - logger.debug("Converting event to Lambda Function URL contract") - return LambdaFunctionUrlEvent(event, self._json_body_deserializer) - if self._proxy_type == ProxyEventType.VPCLatticeEvent: - logger.debug("Converting event to VPC Lattice contract") - return VPCLatticeEvent(event, self._json_body_deserializer) - if self._proxy_type == ProxyEventType.VPCLatticeEventV2: - logger.debug("Converting event to VPC LatticeV2 contract") - return VPCLatticeEventV2(event, self._json_body_deserializer) - logger.debug("Converting event to ALB contract") - return ALBEvent(event, self._json_body_deserializer) + event_class, label = _PROXY_EVENT_MAP.get(self._proxy_type, (ALBEvent, "ALB")) + logger.debug("Converting event to %s contract", label) + return event_class(event, self._json_body_deserializer) def _resolve(self) -> ResponseBuilder: """Resolves the response or return the not found response""" From 5c3b0ac550ac252306670e9968625e7b422ee92b Mon Sep 17 00:00:00 2001 From: Leandro Damascena Date: Tue, 7 Apr 2026 20:57:25 +0800 Subject: [PATCH 2/2] chore: refactoring proxy events --- aws_lambda_powertools/event_handler/api_gateway.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws_lambda_powertools/event_handler/api_gateway.py b/aws_lambda_powertools/event_handler/api_gateway.py index d74463df1b7..db341240132 100644 --- a/aws_lambda_powertools/event_handler/api_gateway.py +++ b/aws_lambda_powertools/event_handler/api_gateway.py @@ -113,7 +113,7 @@ class ProxyEventType(Enum): LambdaFunctionUrlEvent = "LambdaFunctionUrlEvent" -_PROXY_EVENT_MAP: dict[ProxyEventType, tuple[type[BaseProxyEvent], str]] = { +_PROXY_EVENT_MAP: dict[Enum, tuple[type[BaseProxyEvent], str]] = { ProxyEventType.APIGatewayProxyEvent: (APIGatewayProxyEvent, "API Gateway REST API"), ProxyEventType.APIGatewayProxyEventV2: (APIGatewayProxyEventV2, "API Gateway HTTP API"), ProxyEventType.BedrockAgentEvent: (BedrockAgentEvent, "Bedrock Agent"),