Skip to content

Commit baec69b

Browse files
refactor(event_handler): refactoring proxy events (#8125)
* chore: refactoring proxy events * chore: refactoring proxy events
1 parent c47ab63 commit baec69b

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ class ProxyEventType(Enum):
113113
LambdaFunctionUrlEvent = "LambdaFunctionUrlEvent"
114114

115115

116+
_PROXY_EVENT_MAP: dict[Enum, tuple[type[BaseProxyEvent], str]] = {
117+
ProxyEventType.APIGatewayProxyEvent: (APIGatewayProxyEvent, "API Gateway REST API"),
118+
ProxyEventType.APIGatewayProxyEventV2: (APIGatewayProxyEventV2, "API Gateway HTTP API"),
119+
ProxyEventType.BedrockAgentEvent: (BedrockAgentEvent, "Bedrock Agent"),
120+
ProxyEventType.LambdaFunctionUrlEvent: (LambdaFunctionUrlEvent, "Lambda Function URL"),
121+
ProxyEventType.VPCLatticeEvent: (VPCLatticeEvent, "VPC Lattice"),
122+
ProxyEventType.VPCLatticeEventV2: (VPCLatticeEventV2, "VPC LatticeV2"),
123+
ProxyEventType.ALBEvent: (ALBEvent, "ALB"),
124+
}
125+
126+
116127
class CORSConfig:
117128
"""CORS Config
118129
@@ -2498,28 +2509,11 @@ def _compile_regex(rule: str, base_regex: str = _ROUTE_REGEX):
24982509
rule_regex: str = re.sub(_DYNAMIC_ROUTE_PATTERN, _NAMED_GROUP_BOUNDARY_PATTERN, rule)
24992510
return re.compile(base_regex.format(rule_regex))
25002511

2501-
def _to_proxy_event(self, event: dict) -> BaseProxyEvent: # noqa: PLR0911 # ignore many returns
2512+
def _to_proxy_event(self, event: dict) -> BaseProxyEvent:
25022513
"""Convert the event dict to the corresponding data class"""
2503-
if self._proxy_type == ProxyEventType.APIGatewayProxyEvent:
2504-
logger.debug("Converting event to API Gateway REST API contract")
2505-
return APIGatewayProxyEvent(event, self._json_body_deserializer)
2506-
if self._proxy_type == ProxyEventType.APIGatewayProxyEventV2:
2507-
logger.debug("Converting event to API Gateway HTTP API contract")
2508-
return APIGatewayProxyEventV2(event, self._json_body_deserializer)
2509-
if self._proxy_type == ProxyEventType.BedrockAgentEvent:
2510-
logger.debug("Converting event to Bedrock Agent contract")
2511-
return BedrockAgentEvent(event, self._json_body_deserializer)
2512-
if self._proxy_type == ProxyEventType.LambdaFunctionUrlEvent:
2513-
logger.debug("Converting event to Lambda Function URL contract")
2514-
return LambdaFunctionUrlEvent(event, self._json_body_deserializer)
2515-
if self._proxy_type == ProxyEventType.VPCLatticeEvent:
2516-
logger.debug("Converting event to VPC Lattice contract")
2517-
return VPCLatticeEvent(event, self._json_body_deserializer)
2518-
if self._proxy_type == ProxyEventType.VPCLatticeEventV2:
2519-
logger.debug("Converting event to VPC LatticeV2 contract")
2520-
return VPCLatticeEventV2(event, self._json_body_deserializer)
2521-
logger.debug("Converting event to ALB contract")
2522-
return ALBEvent(event, self._json_body_deserializer)
2514+
event_class, label = _PROXY_EVENT_MAP.get(self._proxy_type, (ALBEvent, "ALB"))
2515+
logger.debug("Converting event to %s contract", label)
2516+
return event_class(event, self._json_body_deserializer)
25232517

25242518
def _resolve(self) -> ResponseBuilder:
25252519
"""Resolves the response or return the not found response"""

0 commit comments

Comments
 (0)