Skip to content

Commit 131bde4

Browse files
author
Michael Brewer
committed
docs: Update docs
1 parent 2512cda commit 131bde4

File tree

1 file changed

+38
-35
lines changed

1 file changed

+38
-35
lines changed

docs/utilities/idempotency.md

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,18 @@ this parameter, the entire event will be used as the key.
8383

8484
=== "app.py"
8585

86-
```python hl_lines="2 6-9 11"
86+
```python hl_lines="2-4 8-9 11"
8787
import json
88-
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
88+
from aws_lambda_powertools.utilities.idempotency import (
89+
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
90+
)
8991

9092
# Treat everything under the "body" key in
9193
# the event json object as our payload
92-
persistence_layer = DynamoDBPersistenceLayer(
93-
table_name="IdempotencyTable",
94-
event_key_jmespath="body",
95-
)
94+
config = IdempotencyConfig(event_key_jmespath="body")
95+
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
9696

97-
@idempotent(persistence_store=persistence_layer)
97+
@idempotent(config=config, persistence_store=persistence_layer)
9898
def handler(event, context):
9999
body = json.loads(event['body'])
100100
payment = create_subscription_payment(
@@ -174,9 +174,8 @@ change this window with the `expires_after_seconds` parameter:
174174

175175
=== "app.py"
176176

177-
```python hl_lines="4"
178-
DynamoDBPersistenceLayer(
179-
table_name="IdempotencyTable",
177+
```python hl_lines="3"
178+
IdempotencyConfig(
180179
event_key_jmespath="body",
181180
expires_after_seconds=5*60, # 5 minutes
182181
)
@@ -203,9 +202,8 @@ execution environment. You can change this with the `local_cache_max_items` para
203202

204203
=== "app.py"
205204

206-
```python hl_lines="4 5"
207-
DynamoDBPersistenceLayer(
208-
table_name="IdempotencyTable",
205+
```python hl_lines="3 4"
206+
IdempotencyConfig(
209207
event_key_jmespath="body",
210208
use_local_cache=True,
211209
local_cache_max_items=1000
@@ -224,16 +222,18 @@ idempotent invocations.
224222

225223
=== "app.py"
226224

227-
```python hl_lines="6"
228-
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
225+
```python hl_lines="7"
226+
from aws_lambda_powertools.utilities.idempotency import (
227+
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
228+
)
229229

230-
persistence_layer = DynamoDBPersistenceLayer(
231-
table_name="IdempotencyTable",
230+
config = IdempotencyConfig(
232231
event_key_jmespath="[userDetail, productId]",
233232
payload_validation_jmespath="amount"
234233
)
234+
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
235235

236-
@idempotent(persistence_store=persistence_layer)
236+
@idempotent(config=config, persistence_store=persistence_layer)
237237
def handler(event, context):
238238
# Creating a subscription payment is a side
239239
# effect of calling this function!
@@ -274,7 +274,7 @@ and we will raise `IdempotencyKeyError` if none was found.
274274
=== "app.py"
275275

276276
```python hl_lines="4"
277-
DynamoDBPersistenceLayer(
277+
IdempotencyConfig(
278278
table_name="IdempotencyTable",
279279
event_key_jmespath="body",
280280
raise_on_no_idempotency_key=True,
@@ -298,10 +298,9 @@ This example demonstrates changing the attribute names to custom values:
298298

299299
=== "app.py"
300300

301-
```python hl_lines="4-8"
301+
```python hl_lines="3-7"
302302
persistence_layer = DynamoDBPersistenceLayer(
303303
table_name="IdempotencyTable",
304-
event_key_jmespath="[userDetail, productId]",
305304
key_attr="idempotency_key",
306305
expiry_attr="expires_at",
307306
status_attr="current_status",
@@ -316,35 +315,39 @@ or `boto3_session` parameters when constructing the persistence store.
316315

317316
=== "Custom session"
318317

319-
```python hl_lines="1 4 8"
318+
```python hl_lines="1 7 10"
320319
import boto3
321-
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
320+
from aws_lambda_powertools.utilities.idempotency import (
321+
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
322+
)
322323

324+
config = IdempotencyConfig(event_key_jmespath="body")
323325
boto3_session = boto3.session.Session()
324326
persistence_layer = DynamoDBPersistenceLayer(
325327
table_name="IdempotencyTable",
326-
event_key_jmespath="body",
327328
boto3_session=boto3_session
328329
)
329330

330-
@idempotent(persistence_store=persistence_layer)
331+
@idempotent(config=config, persistence_store=persistence_layer)
331332
def handler(event, context):
332333
...
333334
```
334335
=== "Custom config"
335336

336-
```python hl_lines="1 4 8"
337+
```python hl_lines="1 7 10"
337338
from botocore.config import Config
338-
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, idempotent
339+
from aws_lambda_powertools.utilities.idempotency import (
340+
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
341+
)
339342

343+
config = IdempotencyConfig(event_key_jmespath="body")
340344
boto_config = Config()
341345
persistence_layer = DynamoDBPersistenceLayer(
342346
table_name="IdempotencyTable",
343-
event_key_jmespath="body",
344347
boto_config=boto_config
345348
)
346349

347-
@idempotent(persistence_store=persistence_layer)
350+
@idempotent(config=config, persistence_store=persistence_layer)
348351
def handler(event, context):
349352
...
350353
```
@@ -372,15 +375,15 @@ The idempotency utility can be used with the `validator` decorator. Ensure that
372375

373376
```python hl_lines="9 10"
374377
from aws_lambda_powertools.utilities.validation import validator, envelopes
375-
from aws_lambda_powertools.utilities.idempotency.idempotency import idempotent
376-
377-
persistence_layer = DynamoDBPersistenceLayer(
378-
table_name="IdempotencyTable",
379-
event_key_jmespath="[message, username]",
378+
from aws_lambda_powertools.utilities.idempotency import (
379+
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
380380
)
381381

382+
config = IdempotencyConfig(event_key_jmespath="[message, username]")
383+
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
384+
382385
@validator(envelope=envelopes.API_GATEWAY_HTTP)
383-
@idempotent(persistence_store=persistence_layer)
386+
@idempotent(config=config, persistence_store=persistence_layer)
384387
def lambda_handler(event, context):
385388
cause_some_side_effects(event['username')
386389
return {"message": event['message'], "statusCode": 200}

0 commit comments

Comments
 (0)