Skip to content

Commit

Permalink
feat(data-classes): decorator to instantiate data_classes and docs up…
Browse files Browse the repository at this point in the history
…dates (#442)
  • Loading branch information
Michael Brewer authored Jun 5, 2021
1 parent 31dc88c commit 675cc24
Show file tree
Hide file tree
Showing 7 changed files with 339 additions and 217 deletions.
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,7 @@ def _generate_hash(self, data: Any) -> str:
Hashed representation of the provided data
"""
data = getattr(data, "raw_event", data) # could be a data class depending on decorator order
hashed_data = self.hash_function(json.dumps(data, cls=Encoder).encode())
return hashed_data.hexdigest()

Expand Down
Loading

0 comments on commit 675cc24

Please sign in to comment.