forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
b577366
commit 82f8de2
Showing
10 changed files
with
131 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
docs/examples/utilities/jmespath_functions/custom_jmespath_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from jmespath.functions import signature | ||
|
||
from aws_lambda_powertools.utilities.jmespath_utils import PowertoolsFunctions, extract_data_from_envelope | ||
|
||
|
||
class CustomFunctions(PowertoolsFunctions): | ||
@signature({"types": ["string"]}) # Only decode if value is a string | ||
def _func_special_decoder(self, s): | ||
return my_custom_decoder_logic(s) | ||
|
||
|
||
custom_jmespath_options = {"custom_functions": CustomFunctions()} | ||
|
||
|
||
def handler(event, context): | ||
# use the custom name after `_func_` | ||
extract_data_from_envelope( | ||
data=event, | ||
envelope="special_decoder(body)", | ||
jmespath_options=custom_jmespath_options, | ||
) | ||
... |
8 changes: 8 additions & 0 deletions
8
docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from aws_lambda_powertools.utilities.jmespath_utils import envelopes, extract_data_from_envelope | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
def handler(event: dict, context: LambdaContext): | ||
payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS) | ||
customer = payload.get("customerId") # now deserialized | ||
... |
8 changes: 8 additions & 0 deletions
8
docs/examples/utilities/jmespath_functions/extract_data_jmespath.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
|
||
def handler(event: dict, context: LambdaContext): | ||
payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)") | ||
customer = payload.get("customerId") # now deserialized | ||
... |
13 changes: 13 additions & 0 deletions
13
docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import schemas | ||
|
||
from aws_lambda_powertools.utilities.validation import validate | ||
|
||
sample_event = { | ||
"data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA==" | ||
} | ||
|
||
validate( | ||
event=sample_event, | ||
schema=schemas.INPUT, | ||
envelope="powertools_base64_gzip(data) | powertools_json(@)", | ||
) |
13 changes: 13 additions & 0 deletions
13
docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import schemas | ||
|
||
from aws_lambda_powertools.utilities.validation import validate | ||
|
||
sample_event = { | ||
"data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9=", | ||
} | ||
|
||
validate( | ||
event=sample_event, | ||
schema=schemas.INPUT, | ||
envelope="powertools_json(powertools_base64(data))", | ||
) |
9 changes: 9 additions & 0 deletions
9
docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import schemas | ||
|
||
from aws_lambda_powertools.utilities.validation import validate | ||
|
||
sample_event = { | ||
"data": '{"payload": {"message": "hello hello", "username": "blah blah"}}', | ||
} | ||
|
||
validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)") |
21 changes: 21 additions & 0 deletions
21
docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import json | ||
|
||
from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, IdempotencyConfig, idempotent | ||
|
||
persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable") | ||
config = IdempotencyConfig(event_key_jmespath="powertools_json(body)") | ||
|
||
|
||
@idempotent(config=config, persistence_store=persistence_layer) | ||
def handler(event: dict, context): | ||
body = json.loads(event["body"]) | ||
payment = create_subscription_payment( | ||
user=body["user"], | ||
product=body["product_id"], | ||
) | ||
... | ||
return { | ||
"payment_id": payment.id, | ||
"message": "success", | ||
"statusCode": 200, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters