Skip to content

Commit ba2ebc6

Browse files
committed
add more docs and examples
1 parent cc5a1bd commit ba2ebc6

File tree

3 files changed

+94
-8
lines changed

3 files changed

+94
-8
lines changed

docs/utilities/idempotency.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,30 @@ When using `idempotent_function`, you must tell us which keyword parameter in yo
156156

157157
By supplying an output serializer, you can control the return type of the function, allowing cleaner integration with the rest of your code base.
158158

159-
=== "Using A Custom Type (Dataclasses)"
159+
=== "Using Pydantic"
160+
Explicitly passing the model type
161+
```python hl_lines="3-8 24-25 32-35 55"
162+
--8<-- "examples/idempotency/src/working_with_idempotent_function_pydantic_output_serializer.py"
163+
```
164+
Deducing the model type from the return type annotation
165+
```python hl_lines="3-8 24-25 42-46 55"
166+
--8<-- "examples/idempotency/src/working_with_idempotent_function_pydantic_output_serializer.py"
167+
```
160168

161-
```python hl_lines="3-8 26-28 32-44 51"
162-
--8<-- "examples/idempotency/src/working_with_idempotent_function_custom_output_serializer.py"
169+
=== "Using Dataclasses"
170+
Explicitly passing the model type
171+
```python hl_lines="1 5-8 27-29 36-39 59"
172+
--8<-- "examples/idempotency/src/working_with_idempotent_function_dataclass_output_serializer.py"
173+
```
174+
Deducing the model type from the return type annotation
175+
```python hl_lines="1 5-8 27-29 46-50 60"
176+
--8<-- "examples/idempotency/src/working_with_idempotent_function_dataclass_output_serializer.py"
163177
```
164178

165-
=== "Using Pydantic"
179+
=== "Using A Custom Type (Dataclasses)"
166180

167-
```python hl_lines="3-8 23-24 32 35"
168-
--8<-- "examples/idempotency/src/working_with_idempotent_function_pydantic_output_serializer.py"
181+
```python hl_lines="3-8 26-28 32-44 51"
182+
--8<-- "examples/idempotency/src/working_with_idempotent_function_custom_output_serializer.py"
169183
```
170184

171185
#### Batch integration
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from dataclasses import dataclass
2+
3+
from aws_lambda_powertools.utilities.idempotency import (
4+
DynamoDBPersistenceLayer,
5+
IdempotencyConfig,
6+
idempotent_function,
7+
)
8+
from aws_lambda_powertools.utilities.idempotency.serialization.dataclass import DataclassSerializer
9+
from aws_lambda_powertools.utilities.typing import LambdaContext
10+
11+
dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
12+
config = IdempotencyConfig(event_key_jmespath="order_id") # see Choosing a payload subset section
13+
14+
15+
@dataclass
16+
class OrderItem:
17+
sku: str
18+
description: str
19+
20+
21+
@dataclass
22+
class Order:
23+
item: OrderItem
24+
order_id: int
25+
26+
27+
@dataclass
28+
class OrderOutput:
29+
order_id: int
30+
31+
32+
@idempotent_function(
33+
data_keyword_argument="order",
34+
config=config,
35+
persistence_store=dynamodb,
36+
output_serializer=DataclassSerializer(model=OrderOutput),
37+
)
38+
def explicit_order_output_serializer(order: Order):
39+
return OrderOutput(order_id=order.order_id)
40+
41+
42+
@idempotent_function(
43+
data_keyword_argument="order",
44+
config=config,
45+
persistence_store=dynamodb,
46+
output_serializer=DataclassSerializer,
47+
)
48+
# order output is deduced from return type
49+
def deduced_order_output_serializer(order: Order) -> OrderOutput:
50+
return OrderOutput(order_id=order.order_id)
51+
52+
53+
def lambda_handler(event: dict, context: LambdaContext):
54+
config.register_lambda_context(context) # see Lambda timeouts section
55+
order_item = OrderItem(sku="fake", description="sample")
56+
order = Order(item=order_item, order_id=1)
57+
58+
# `order` parameter must be called as a keyword argument to work
59+
explicit_order_output_serializer(order=order)
60+
deduced_order_output_serializer(order=order)

examples/idempotency/src/working_with_idempotent_function_pydantic_output_serializer.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,18 @@ class OrderOutput(BaseModel):
3131
persistence_store=dynamodb,
3232
output_serializer=PydanticSerializer(model=OrderOutput),
3333
)
34-
def process_order(order: Order) -> OrderOutput:
34+
def explicit_order_output_serializer(order: Order):
35+
return OrderOutput(order_id=order.order_id)
36+
37+
38+
@idempotent_function(
39+
data_keyword_argument="order",
40+
config=config,
41+
persistence_store=dynamodb,
42+
output_serializer=PydanticSerializer,
43+
)
44+
# order output is deduced from return type
45+
def deduced_order_output_serializer(order: Order) -> OrderOutput:
3546
return OrderOutput(order_id=order.order_id)
3647

3748

@@ -41,4 +52,5 @@ def lambda_handler(event: dict, context: LambdaContext):
4152
order = Order(item=order_item, order_id=1)
4253

4354
# `order` parameter must be called as a keyword argument to work
44-
process_order(order=order)
55+
explicit_order_output_serializer(order=order)
56+
deduced_order_output_serializer(order=order)

0 commit comments

Comments
 (0)