Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(idempotency): simplify access to expiration time in DataRecord class #5082

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading