Skip to content

Commit

Permalink
feat(idempotency): simplify access to expiration time in DataRecord
Browse files Browse the repository at this point in the history
… class (#5082)

Accessing expiration time
  • Loading branch information
leandrodamascena authored Aug 27, 2024
1 parent 550fe3d commit 6acc5fa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,24 @@ def response_json_as_dict(self) -> dict | None:
previous response data deserialized
"""
return json.loads(self.response_data) if self.response_data else None

def get_expiration_datetime(self) -> datetime.datetime | None:
"""
Converts the expiry timestamp to a datetime object.
This method checks if an expiry timestamp exists and converts it to a
datetime object. If no timestamp is present, it returns None.
Returns:
-------
datetime.datetime | None
A datetime object representing the expiration time, or None if no expiry timestamp is set.
Note:
----
The returned datetime object is timezone-naive and assumes the timestamp
is in the system's local timezone. Lambda default timezone is UTC.
"""
if self.expiry_timestamp:
return datetime.datetime.fromtimestamp(int(self.expiry_timestamp))
return None
8 changes: 3 additions & 5 deletions examples/idempotency/src/working_with_response_hook.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import uuid
from typing import Dict

Expand All @@ -20,11 +19,10 @@ 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
# expiry_timestamp can be None so include if set
expiry_timestamp = idempotent_data.get_expiration_datetime()
if expiry_timestamp:
expiry_time = datetime.datetime.fromtimestamp(int(expiry_timestamp))
response["x-idempotent-expiration"] = expiry_time.isoformat()
response["x-idempotent-expiration"] = expiry_timestamp.isoformat()

# Must return the response here
return response
Expand Down
2 changes: 2 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,7 @@ def test_idempotent_lambda_already_completed_response_hook_is_called(
def idempotent_response_hook(response: Any, idempotent_data: DataRecord) -> Any:
"""Modify the response provided by adding a new key"""
response["idempotent_response"] = True
response["idempotent_expiration"] = idempotent_data.get_expiration_datetime()

return response

Expand All @@ -2070,6 +2071,7 @@ def lambda_handler(event, context):

# Then idempotent_response value will be added to the response
assert lambda_resp["idempotent_response"]
assert lambda_resp["idempotent_expiration"] == datetime.datetime.fromtimestamp(int(timestamp_future))

stubber.assert_no_pending_responses()
stubber.deactivate()

0 comments on commit 6acc5fa

Please sign in to comment.