Skip to content

Commit 82f8de2

Browse files
committed
fix(docs): Extract jmespath code examples
Changes: - Extract code examples - Run isort, black - Fix python syntax errors - Fix line highlights - Add make task Related to: - aws-powertools#1064
1 parent b577366 commit 82f8de2

10 files changed

+131
-103
lines changed

Diff for: Makefile

+10
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,13 @@ changelog:
9090

9191
mypy:
9292
poetry run mypy --pretty aws_lambda_powertools
93+
94+
format-examples:
95+
poetry run isort docs/shared
96+
poetry run black docs/shared/*.py
97+
poetry run isort docs/examples
98+
poetry run black docs/examples/*/*/*.py
99+
100+
lint-examples:
101+
poetry run python3 -m py_compile docs/shared/*.py
102+
poetry run python3 -m py_compile docs/examples/*/*/*.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from jmespath.functions import signature
2+
3+
from aws_lambda_powertools.utilities.jmespath_utils import PowertoolsFunctions, extract_data_from_envelope
4+
5+
6+
class CustomFunctions(PowertoolsFunctions):
7+
@signature({"types": ["string"]}) # Only decode if value is a string
8+
def _func_special_decoder(self, s):
9+
return my_custom_decoder_logic(s)
10+
11+
12+
custom_jmespath_options = {"custom_functions": CustomFunctions()}
13+
14+
15+
def handler(event, context):
16+
# use the custom name after `_func_`
17+
extract_data_from_envelope(
18+
data=event,
19+
envelope="special_decoder(body)",
20+
jmespath_options=custom_jmespath_options,
21+
)
22+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from aws_lambda_powertools.utilities.jmespath_utils import envelopes, extract_data_from_envelope
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
5+
def handler(event: dict, context: LambdaContext):
6+
payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS)
7+
customer = payload.get("customerId") # now deserialized
8+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope
2+
from aws_lambda_powertools.utilities.typing import LambdaContext
3+
4+
5+
def handler(event: dict, context: LambdaContext):
6+
payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)")
7+
customer = payload.get("customerId") # now deserialized
8+
...
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validate
4+
5+
sample_event = {
6+
"data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA=="
7+
}
8+
9+
validate(
10+
event=sample_event,
11+
schema=schemas.INPUT,
12+
envelope="powertools_base64_gzip(data) | powertools_json(@)",
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validate
4+
5+
sample_event = {
6+
"data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9=",
7+
}
8+
9+
validate(
10+
event=sample_event,
11+
schema=schemas.INPUT,
12+
envelope="powertools_json(powertools_base64(data))",
13+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import schemas
2+
3+
from aws_lambda_powertools.utilities.validation import validate
4+
5+
sample_event = {
6+
"data": '{"payload": {"message": "hello hello", "username": "blah blah"}}',
7+
}
8+
9+
validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, IdempotencyConfig, idempotent
4+
5+
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
6+
config = IdempotencyConfig(event_key_jmespath="powertools_json(body)")
7+
8+
9+
@idempotent(config=config, persistence_store=persistence_layer)
10+
def handler(event: dict, context):
11+
body = json.loads(event["body"])
12+
payment = create_subscription_payment(
13+
user=body["user"],
14+
product=body["product_id"],
15+
)
16+
...
17+
return {
18+
"payment_id": payment.id,
19+
"message": "success",
20+
"statusCode": 200,
21+
}

Diff for: docs/shared/validation_basic_jsonschema.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,15 @@
3333
"examples": [{"statusCode": 200, "body": "response"}],
3434
"required": ["statusCode", "body"],
3535
"properties": {
36-
"statusCode": {"$id": "#/properties/statusCode", "type": "integer", "title": "The statusCode"},
37-
"body": {"$id": "#/properties/body", "type": "string", "title": "The response"},
36+
"statusCode": {
37+
"$id": "#/properties/statusCode",
38+
"type": "integer",
39+
"title": "The statusCode",
40+
},
41+
"body": {
42+
"$id": "#/properties/body",
43+
"type": "string",
44+
"title": "The response",
45+
},
3846
},
3947
}

Diff for: docs/utilities/jmespath_functions.md

+17-101
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,8 @@ You can use the `extract_data_from_envelope` function along with any [JMESPath e
2828

2929
=== "app.py"
3030

31-
```python hl_lines="1 7"
32-
from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope
33-
34-
from aws_lambda_powertools.utilities.typing import LambdaContext
35-
36-
37-
def handler(event: dict, context: LambdaContext):
38-
payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)")
39-
customer = payload.get("customerId") # now deserialized
40-
...
31+
```python hl_lines="1 6"
32+
--8<-- "docs/examples/utilities/jmespath_functions/extract_data_jmespath.py"
4133
```
4234

4335
=== "event.json"
@@ -54,16 +46,8 @@ We provide built-in envelopes for popular JMESPath expressions used when looking
5446

5547
=== "app.py"
5648

57-
```python hl_lines="1 7"
58-
from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope, envelopes
59-
60-
from aws_lambda_powertools.utilities.typing import LambdaContext
61-
62-
63-
def handler(event: dict, context: LambdaContext):
64-
payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS)
65-
customer = payload.get("customerId") # now deserialized
66-
...
49+
```python hl_lines="1 6"
50+
--8<-- "docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py"
6751
```
6852

6953
=== "event.json"
@@ -107,6 +91,7 @@ Envelope | JMESPath expression
10791
## Advanced
10892

10993
### Built-in JMESPath functions
94+
11095
You can use our built-in JMESPath functions within your expressions to do exactly that to decode JSON Strings, base64, and uncompress gzip data.
11196

11297
???+ info
@@ -123,49 +108,21 @@ This sample will decode the value within the `data` key into a valid JSON before
123108
=== "powertools_json_jmespath_function.py"
124109

125110
```python hl_lines="9"
126-
from aws_lambda_powertools.utilities.validation import validate
127-
128-
import schemas
129-
130-
sample_event = {
131-
'data': '{"payload": {"message": "hello hello", "username": "blah blah"}}'
132-
}
133-
134-
validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)")
111+
--8<-- "docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py"
135112
```
136113

137114
=== "schemas.py"
138115

139-
```python hl_lines="7 14 16 23 39 45 47 52"
116+
```python hl_lines="8 10 17 34 36 41"
140117
--8<-- "docs/shared/validation_basic_jsonschema.py"
141118
```
142119

143120
> **Idempotency scenario**
144121
145122
This sample will decode the value within the `body` key of an API Gateway event into a valid JSON object to ensure the Idempotency utility processes a JSON object instead of a string.
146123

147-
```python hl_lines="7" title="Deserializing JSON before using as idempotency key"
148-
import json
149-
from aws_lambda_powertools.utilities.idempotency import (
150-
IdempotencyConfig, DynamoDBPersistenceLayer, idempotent
151-
)
152-
153-
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable")
154-
config = IdempotencyConfig(event_key_jmespath="powertools_json(body)")
155-
156-
@idempotent(config=config, persistence_store=persistence_layer)
157-
def handler(event:APIGatewayProxyEvent, context):
158-
body = json.loads(event['body'])
159-
payment = create_subscription_payment(
160-
user=body['user'],
161-
product=body['product_id']
162-
)
163-
...
164-
return {
165-
"payment_id": payment.id,
166-
"message": "success",
167-
"statusCode": 200
168-
}
124+
```python hl_lines="6" title="Deserializing JSON before using as idempotency key"
125+
--8<-- "docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py"
169126
```
170127

171128
#### powertools_base64 function
@@ -174,27 +131,15 @@ Use `powertools_base64` function to decode any base64 data.
174131

175132
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.
176133

177-
=== "powertools_json_jmespath_function.py"
134+
=== "powertools_base64_jmespath_function.py"
178135

179136
```python hl_lines="12"
180-
from aws_lambda_powertools.utilities.validation import validate
181-
182-
import schemas
183-
184-
sample_event = {
185-
"data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9="
186-
}
187-
188-
validate(
189-
event=sample_event,
190-
schema=schemas.INPUT,
191-
envelope="powertools_json(powertools_base64(data))"
192-
)
137+
--8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py"
193138
```
194139

195140
=== "schemas.py"
196141

197-
```python hl_lines="7 14 16 23 39 45 47 52"
142+
```python hl_lines="8 10 17 34 36 41"
198143
--8<-- "docs/shared/validation_basic_jsonschema.py"
199144
```
200145

@@ -204,27 +149,15 @@ Use `powertools_base64_gzip` function to decompress and decode base64 data.
204149

205150
This sample will decompress and decode base64 data, then use JMESPath pipeline expression to pass the result for decoding its JSON string.
206151

207-
=== "powertools_json_jmespath_function.py"
152+
=== "powertools_base64_gzip_jmespath_function.py"
208153

209154
```python hl_lines="12"
210-
from aws_lambda_powertools.utilities.validation import validate
211-
212-
import schemas
213-
214-
sample_event = {
215-
"data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA=="
216-
}
217-
218-
validate(
219-
event=sample_event,
220-
schema=schemas.INPUT,
221-
envelope="powertools_base64_gzip(data) | powertools_json(@)"
222-
)
155+
--8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py"
223156
```
224157

225158
=== "schemas.py"
226159

227-
```python hl_lines="7 14 16 23 39 45 47 52"
160+
```python hl_lines="8 10 17 34 36 41"
228161
--8<-- "docs/shared/validation_basic_jsonschema.py"
229162
```
230163

@@ -239,25 +172,8 @@ In order to keep the built-in functions from Powertools, you can subclass from `
239172

240173
=== "custom_jmespath_function.py"
241174

242-
```python hl_lines="2-3 6-9 11 17"
243-
from aws_lambda_powertools.utilities.jmespath_utils import (
244-
PowertoolsFunctions, extract_data_from_envelope)
245-
from jmespath.functions import signature
246-
247-
248-
class CustomFunctions(PowertoolsFunctions):
249-
@signature({'types': ['string']}) # Only decode if value is a string
250-
def _func_special_decoder(self, s):
251-
return my_custom_decoder_logic(s)
252-
253-
custom_jmespath_options = {"custom_functions": CustomFunctions()}
254-
255-
def handler(event, context):
256-
# use the custom name after `_func_`
257-
extract_data_from_envelope(data=event,
258-
envelope="special_decoder(body)",
259-
jmespath_options=**custom_jmespath_options)
260-
...
175+
```python hl_lines="1 3 6-9 12 20"
176+
--8<-- "docs/examples/utilities/jmespath_functions/custom_jmespath_function.py"
261177
```
262178

263179
=== "event.json"

0 commit comments

Comments
 (0)