Skip to content

refactor(data_classes): simplify nested data classes #6289

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions aws_lambda_powertools/utilities/data_classes/alb_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ALBEventRequestContext(DictWrapper):
@property
def elb_target_group_arn(self) -> str:
"""Target group arn for your Lambda function"""
return self["requestContext"]["elb"]["targetGroupArn"]
return self["elb"]["targetGroupArn"]


class ALBEvent(BaseProxyEvent):
Expand All @@ -32,7 +32,7 @@ class ALBEvent(BaseProxyEvent):

@property
def request_context(self) -> ALBEventRequestContext:
return ALBEventRequestContext(self._data)
return ALBEventRequestContext(self["requestContext"])

@property
def multi_value_query_string_parameters(self) -> dict[str, list[str]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ def stage_variables(self) -> dict[str, str]:

@property
def request_context(self) -> BaseRequestContext:
return BaseRequestContext(self._data)
return BaseRequestContext(self["requestContext"])

@overload
def get_header_value(
Expand Down Expand Up @@ -306,7 +306,7 @@ def query_string_parameters(self) -> dict[str, str]:

@property
def request_context(self) -> BaseRequestContextV2:
return BaseRequestContextV2(self._data)
return BaseRequestContextV2(self["requestContext"])

@property
def path_parameters(self) -> dict[str, str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,41 @@ class APIGatewayEventRequestContext(BaseRequestContext):
@property
def connected_at(self) -> int | None:
"""The Epoch-formatted connection time. (WebSocket API)"""
return self["requestContext"].get("connectedAt")
return self.get("connectedAt")

@property
def connection_id(self) -> str | None:
"""A unique ID for the connection that can be used to make a callback to the client. (WebSocket API)"""
return self["requestContext"].get("connectionId")
return self.get("connectionId")

@property
def event_type(self) -> str | None:
"""The event type: `CONNECT`, `MESSAGE`, or `DISCONNECT`. (WebSocket API)"""
return self["requestContext"].get("eventType")
return self.get("eventType")

@property
def message_direction(self) -> str | None:
"""Message direction (WebSocket API)"""
return self["requestContext"].get("messageDirection")
return self.get("messageDirection")

@property
def message_id(self) -> str | None:
"""A unique server-side ID for a message. Available only when the `eventType` is `MESSAGE`."""
return self["requestContext"].get("messageId")
return self.get("messageId")

@property
def operation_name(self) -> str | None:
"""The name of the operation being performed"""
return self["requestContext"].get("operationName")
return self.get("operationName")

@property
def route_key(self) -> str | None:
"""The selected route key."""
return self["requestContext"].get("routeKey")
return self.get("routeKey")

@property
def authorizer(self) -> APIGatewayEventAuthorizer:
authz_data = self._data.get("requestContext", {}).get("authorizer", {})
return APIGatewayEventAuthorizer(authz_data)
return APIGatewayEventAuthorizer(self.get("authorizer") or {})


class APIGatewayProxyEvent(BaseProxyEvent):
Expand Down Expand Up @@ -136,7 +135,7 @@ def resolved_headers_field(self) -> dict[str, Any]:

@property
def request_context(self) -> APIGatewayEventRequestContext:
return APIGatewayEventRequestContext(self._data)
return APIGatewayEventRequestContext(self["requestContext"])

@property
def path_parameters(self) -> dict[str, str]:
Expand Down Expand Up @@ -248,8 +247,7 @@ def iam(self) -> RequestContextV2AuthorizerIam:
class RequestContextV2(BaseRequestContextV2):
@property
def authorizer(self) -> RequestContextV2Authorizer:
ctx = self.get("requestContext") or {} # key might exist but can be `null`
return RequestContextV2Authorizer(ctx.get("authorizer", {}))
return RequestContextV2Authorizer(self.get("authorizer") or {})


class APIGatewayProxyEventV2(BaseProxyEvent):
Expand Down Expand Up @@ -291,7 +289,7 @@ def cookies(self) -> list[str]:

@property
def request_context(self) -> RequestContextV2:
return RequestContextV2(self._data)
return RequestContextV2(self["requestContext"])

@property
def path_parameters(self) -> dict[str, str]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ class AppSyncAuthorizerEventRequestContext(DictWrapper):
@property
def api_id(self) -> str:
"""AppSync API ID"""
return self["requestContext"]["apiId"]
return self["apiId"]

@property
def account_id(self) -> str:
"""AWS Account ID"""
return self["requestContext"]["accountId"]
return self["accountId"]

@property
def request_id(self) -> str:
"""Requestt ID"""
return self["requestContext"]["requestId"]
return self["requestId"]

@property
def query_string(self) -> str:
"""GraphQL query string"""
return self["requestContext"]["queryString"]
return self["queryString"]

@property
def operation_name(self) -> str | None:
"""GraphQL operation name, optional"""
return self["requestContext"].get("operationName")
return self.get("operationName")

@property
def variables(self) -> dict:
"""GraphQL variables"""
return self["requestContext"]["variables"]
return self["variables"]


class AppSyncAuthorizerEvent(DictWrapper):
Expand All @@ -57,7 +57,7 @@ def authorization_token(self) -> str:
@property
def request_context(self) -> AppSyncAuthorizerEventRequestContext:
"""Request context"""
return AppSyncAuthorizerEventRequestContext(self._data)
return AppSyncAuthorizerEventRequestContext(self["requestContext"])


class AppSyncAuthorizerResponse:
Expand Down
Loading
Loading