Skip to content

Commit b947e1d

Browse files
committed
Updated Idempotency examples and highlight importance of JMESPath for JSON APIs
1 parent fce12c1 commit b947e1d

File tree

3 files changed

+9
-124
lines changed

3 files changed

+9
-124
lines changed

docs/utilities/idempotency.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,11 @@ In this example, we have a Lambda handler that creates a payment for a user subs
206206

207207
Imagine the function executes successfully, but the client never receives the response due to a connection issue. It is safe to retry in this instance, as the idempotent decorator will return a previously saved response.
208208

209+
!!! warning "Making JSON APIs Idempotent"
210+
To ensure JSON structured APIs are fully idempotent we must ensure the idempotent key is a valid JSON object.
211+
212+
To do this we can [customize the default behaviour](#customizing-the-default-behavior) with *event_key_jmespath* and the [JMESPath built-in function](/utilities/jmespath_functions) *powertools_json()*.
213+
209214
=== "payment.py"
210215

211216
```python hl_lines="2-4 10 12 15 20"
@@ -218,7 +223,7 @@ Imagine the function executes successfully, but the client never receives the re
218223

219224
# Treat everything under the "body" key
220225
# in the event json object as our payload
221-
config = IdempotencyConfig(event_key_jmespath="body")
226+
config = IdempotencyConfig(event_key_jmespath="powertools_json(body)")
222227

223228
@idempotent(config=config, persistence_store=persistence_layer)
224229
def handler(event, context):
@@ -270,6 +275,7 @@ Imagine the function executes successfully, but the client never receives the re
270275
}
271276
```
272277

278+
273279
### Idempotency request flow
274280

275281
This sequence diagram shows an example flow of what happens in the payment scenario:

docs/utilities/validation.md

+1-123
Original file line numberDiff line numberDiff line change
@@ -429,129 +429,7 @@ For each format defined in a dictionary key, you must use a regex, or a function
429429

430430
You might have events or responses that contain non-encoded JSON, where you need to decode before validating them.
431431

432-
You can use our built-in JMESPath functions within your expressions to do exactly that to decode JSON Strings, base64, and uncompress gzip data.
432+
You can use our built-in [JMESPath functions](/utilities/jmespath_functions) within your expressions to do exactly that to decode JSON Strings, base64, and uncompress gzip data.
433433

434434
!!! info
435435
We use these for built-in envelopes to easily to decode and unwrap events from sources like Kinesis, CloudWatch Logs, etc.
436-
437-
#### powertools_json function
438-
439-
Use `powertools_json` function to decode any JSON String.
440-
441-
This sample will decode the value within the `data` key into a valid JSON before we can validate it.
442-
443-
=== "powertools_json_jmespath_function.py"
444-
445-
```python hl_lines="9"
446-
from aws_lambda_powertools.utilities.validation import validate
447-
448-
import schemas
449-
450-
sample_event = {
451-
'data': '{"payload": {"message": "hello hello", "username": "blah blah"}}'
452-
}
453-
454-
validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)")
455-
```
456-
457-
=== "schemas.py"
458-
459-
```python hl_lines="7 14 16 23 39 45 47 52"
460-
--8<-- "docs/shared/validation_basic_jsonschema.py"
461-
```
462-
463-
#### powertools_base64 function
464-
465-
Use `powertools_base64` function to decode any base64 data.
466-
467-
This sample will decode the base64 value within the `data` key, and decode the JSON string into a valid JSON before we can validate it.
468-
469-
=== "powertools_json_jmespath_function.py"
470-
471-
```python hl_lines="12"
472-
from aws_lambda_powertools.utilities.validation import validate
473-
474-
import schemas
475-
476-
sample_event = {
477-
"data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9="
478-
}
479-
480-
validate(
481-
event=sample_event,
482-
schema=schemas.INPUT,
483-
envelope="powertools_json(powertools_base64(data))"
484-
)
485-
```
486-
487-
=== "schemas.py"
488-
489-
```python hl_lines="7 14 16 23 39 45 47 52"
490-
--8<-- "docs/shared/validation_basic_jsonschema.py"
491-
```
492-
493-
#### powertools_base64_gzip function
494-
495-
Use `powertools_base64_gzip` function to decompress and decode base64 data.
496-
497-
This sample will decompress and decode base64 data, then use JMESPath pipeline expression to pass the result for decoding its JSON string.
498-
499-
=== "powertools_json_jmespath_function.py"
500-
501-
```python hl_lines="12"
502-
from aws_lambda_powertools.utilities.validation import validate
503-
504-
import schemas
505-
506-
sample_event = {
507-
"data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA=="
508-
}
509-
510-
validate(
511-
event=sample_event,
512-
schema=schemas.INPUT,
513-
envelope="powertools_base64_gzip(data) | powertools_json(@)"
514-
)
515-
```
516-
517-
=== "schemas.py"
518-
519-
```python hl_lines="7 14 16 23 39 45 47 52"
520-
--8<-- "docs/shared/validation_basic_jsonschema.py"
521-
```
522-
523-
### Bring your own JMESPath function
524-
525-
!!! warning
526-
This should only be used for advanced use cases where you have special formats not covered by the built-in functions.
527-
528-
This will **replace all provided built-in functions such as `powertools_json`, so you will no longer be able to use them**.
529-
530-
For special binary formats that you want to decode before applying JSON Schema validation, you can bring your own [JMESPath function](https://github.com/jmespath/jmespath.py#custom-functions){target="_blank"} and any additional option via `jmespath_options` param.
531-
532-
=== "custom_jmespath_function.py"
533-
534-
```python hl_lines="2 6-10 14"
535-
from aws_lambda_powertools.utilities.validation import validator
536-
from jmespath import functions
537-
538-
import schemas
539-
540-
class CustomFunctions(functions.Functions):
541-
542-
@functions.signature({'types': ['string']})
543-
def _func_special_decoder(self, s):
544-
return my_custom_decoder_logic(s)
545-
546-
custom_jmespath_options = {"custom_functions": CustomFunctions()}
547-
548-
@validator(schema=schemas.INPUT, jmespath_options=**custom_jmespath_options)
549-
def handler(event, context):
550-
return event
551-
```
552-
553-
=== "schemas.py"
554-
555-
```python hl_lines="7 14 16 23 39 45 47 52"
556-
--8<-- "docs/shared/validation_basic_jsonschema.py"
557-
```

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ nav:
2626
- utilities/parser.md
2727
- utilities/idempotency.md
2828
- utilities/feature_flags.md
29+
- utilities/jmespath_functions.md
2930

3031
theme:
3132
name: material

0 commit comments

Comments
 (0)