-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Idempotency): add feature for manipulating idempotent responses (#…
…4037) * feat(idempotent-response-manipulation): Added capability of providing an IdempotentHook functiont to be called when an idempotent response is being returned. * chore(mypy): resolve myopy static typing issues, make response+hook properly optional * feat(response_hook): added some documentation, call response_hook after custom de-serialization * feat(response_hook): review items * chore(mypy): resolve type erro r in example code - expiry_timestamp can be None * chore(docs): fix formatting error in markdown * chore(docs): fix highlighting of example code - lines moved * Improving doc * Improving doc * Addressing Ruben's feedback --------- Co-authored-by: Leandro Damascena <[email protected]>
- Loading branch information
1 parent
5a6ae52
commit b149b15
Showing
8 changed files
with
295 additions
and
47 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
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
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 @@ | ||
from typing import Any | ||
|
||
from aws_lambda_powertools.shared.types import Protocol | ||
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import DataRecord | ||
|
||
|
||
class IdempotentHookFunction(Protocol): | ||
""" | ||
The IdempotentHookFunction. | ||
This class defines the calling signature for IdempotentHookFunction callbacks. | ||
""" | ||
|
||
def __call__(self, response: Any, idempotent_data: DataRecord) -> Any: ... |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
import uuid | ||
from typing import Dict | ||
|
||
from aws_lambda_powertools import Logger | ||
from aws_lambda_powertools.utilities.idempotency import ( | ||
DynamoDBPersistenceLayer, | ||
IdempotencyConfig, | ||
idempotent_function, | ||
) | ||
from aws_lambda_powertools.utilities.idempotency.persistence.base import ( | ||
DataRecord, | ||
) | ||
from aws_lambda_powertools.utilities.typing import LambdaContext | ||
|
||
logger = Logger() | ||
|
||
|
||
def my_response_hook(response: Dict, idempotent_data: DataRecord) -> Dict: | ||
# Return inserted Header data into the Idempotent Response | ||
response["x-idempotent-key"] = idempotent_data.idempotency_key | ||
|
||
# expiry_timestamp could be None so include if set | ||
expiry_timestamp = idempotent_data.expiry_timestamp | ||
if expiry_timestamp: | ||
expiry_time = datetime.datetime.fromtimestamp(int(expiry_timestamp)) | ||
response["x-idempotent-expiration"] = expiry_time.isoformat() | ||
|
||
# Must return the response here | ||
return response | ||
|
||
|
||
dynamodb = DynamoDBPersistenceLayer(table_name="IdempotencyTable") | ||
config = IdempotencyConfig(response_hook=my_response_hook) | ||
|
||
|
||
@idempotent_function(data_keyword_argument="order", config=config, persistence_store=dynamodb) | ||
def process_order(order: dict) -> dict: | ||
# create the order_id | ||
order_id = str(uuid.uuid4()) | ||
|
||
# create your logic to save the order | ||
# append the order_id created | ||
order["order_id"] = order_id | ||
|
||
# return the order | ||
return {"order": order} | ||
|
||
|
||
def lambda_handler(event: dict, context: LambdaContext): | ||
config.register_lambda_context(context) # see Lambda timeouts section | ||
try: | ||
logger.info(f"Processing order id {event.get('order_id')}") | ||
return process_order(order=event.get("order")) | ||
except Exception as err: | ||
return {"status_code": 400, "error": f"Error processing {str(err)}"} |
8 changes: 8 additions & 0 deletions
8
examples/idempotency/src/working_with_response_hook_payload.json
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 @@ | ||
{ | ||
"order" : { | ||
"user_id": "xyz", | ||
"product_id": "123456789", | ||
"quantity": 2, | ||
"value": 30 | ||
} | ||
} |
Oops, something went wrong.