Skip to content

Commit 675cc24

Browse files
author
Michael Brewer
authored
feat(data-classes): decorator to instantiate data_classes and docs updates (#442)
1 parent 31dc88c commit 675cc24

File tree

7 files changed

+339
-217
lines changed

7 files changed

+339
-217
lines changed

aws_lambda_powertools/utilities/data_classes/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .connect_contact_flow_event import ConnectContactFlowEvent
1111
from .dynamo_db_stream_event import DynamoDBStreamEvent
1212
from .event_bridge_event import EventBridgeEvent
13+
from .event_source import event_source
1314
from .kinesis_stream_event import KinesisStreamEvent
1415
from .s3_event import S3Event
1516
from .ses_event import SESEvent
@@ -31,4 +32,5 @@
3132
"SESEvent",
3233
"SNSEvent",
3334
"SQSEvent",
35+
"event_source",
3436
]

aws_lambda_powertools/utilities/data_classes/alb_event.py

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class ALBEventRequestContext(DictWrapper):
77
@property
88
def elb_target_group_arn(self) -> str:
9+
"""Target group arn for your Lambda function"""
910
return self["requestContext"]["elb"]["targetGroupArn"]
1011

1112

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

2022
@property
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Any, Callable, Dict, Type
2+
3+
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
4+
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
5+
from aws_lambda_powertools.utilities.typing import LambdaContext
6+
7+
8+
@lambda_handler_decorator
9+
def event_source(
10+
handler: Callable[[Any, LambdaContext], Any],
11+
event: Dict[str, Any],
12+
context: LambdaContext,
13+
data_class: Type[DictWrapper],
14+
):
15+
"""Middleware to create an instance of the passed in event source data class
16+
17+
Parameters
18+
----------
19+
handler: Callable
20+
Lambda's handler
21+
event: Dict
22+
Lambda's Event
23+
context: Dict
24+
Lambda's Context
25+
data_class: Type[DictWrapper]
26+
Data class type to instantiate
27+
28+
Example
29+
--------
30+
31+
**Sample usage**
32+
33+
from aws_lambda_powertools.utilities.data_classes import S3Event, event_source
34+
35+
@event_source(data_class=S3Event)
36+
def handler(event: S3Event, context):
37+
return {"key": event.object_key}
38+
"""
39+
return handler(data_class(event), context)

aws_lambda_powertools/utilities/idempotency/persistence/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ def _generate_hash(self, data: Any) -> str:
224224
Hashed representation of the provided data
225225
226226
"""
227+
data = getattr(data, "raw_event", data) # could be a data class depending on decorator order
227228
hashed_data = self.hash_function(json.dumps(data, cls=Encoder).encode())
228229
return hashed_data.hexdigest()
229230

0 commit comments

Comments
 (0)