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

feat(data-classes): decorator to instantiate data_classes and docs updates #442

Merged
merged 12 commits into from
Jun 5, 2021
Merged
2 changes: 2 additions & 0 deletions aws_lambda_powertools/utilities/data_classes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .connect_contact_flow_event import ConnectContactFlowEvent
from .dynamo_db_stream_event import DynamoDBStreamEvent
from .event_bridge_event import EventBridgeEvent
from .event_source import event_source
from .kinesis_stream_event import KinesisStreamEvent
from .s3_event import S3Event
from .ses_event import SESEvent
Expand All @@ -31,4 +32,5 @@
"SESEvent",
"SNSEvent",
"SQSEvent",
"event_source",
]
2 changes: 2 additions & 0 deletions aws_lambda_powertools/utilities/data_classes/alb_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class ALBEventRequestContext(DictWrapper):
@property
def elb_target_group_arn(self) -> str:
"""Target group arn for your Lambda function"""
return self["requestContext"]["elb"]["targetGroupArn"]


Expand All @@ -15,6 +16,7 @@ class ALBEvent(BaseProxyEvent):
Documentation:
--------------
- https://docs.aws.amazon.com/lambda/latest/dg/services-alb.html
- https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html
"""

@property
Expand Down
39 changes: 39 additions & 0 deletions aws_lambda_powertools/utilities/data_classes/event_source.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Any, Callable, Dict, Type

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
from aws_lambda_powertools.utilities.typing import LambdaContext


@lambda_handler_decorator
def event_source(
handler: Callable[[Any, LambdaContext], Any],
event: Dict[str, Any],
context: LambdaContext,
data_class: Type[DictWrapper],
):
"""Middleware to create an instance of the passed in event source data class

Parameters
----------
handler: Callable
Lambda's handler
event: Dict
Lambda's Event
context: Dict
Lambda's Context
data_class: Type[DictWrapper]
Data class type to instantiate

Example
--------

**Sample usage**

from aws_lambda_powertools.utilities.data_classes import S3Event, event_source

@event_source(data_class=S3Event)
def handler(event: S3Event, context):
return {"key": event.object_key}
"""
return handler(data_class(event), context)
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ def _generate_hash(self, data: Any) -> str:
Hashed representation of the provided data

"""
if hasattr(data, "raw_event"):
data = data.raw_event
michaelbrewer marked this conversation as resolved.
Show resolved Hide resolved
hashed_data = self.hash_function(json.dumps(data, cls=Encoder).encode())
return hashed_data.hexdigest()

Expand Down
Loading