Skip to content

Commit b81fd49

Browse files
committed
feat(idempotent): Add include_function_name option
1 parent d697b55 commit b81fd49

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

aws_lambda_powertools/utilities/idempotency/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def __init__(
1212
use_local_cache: bool = False,
1313
local_cache_max_items: int = 256,
1414
hash_function: str = "md5",
15+
include_function_name: bool = True,
1516
):
1617
"""
1718
Initialize the base persistence layer
@@ -32,6 +33,8 @@ def __init__(
3233
Max number of items to store in local cache, by default 1024
3334
hash_function: str, optional
3435
Function to use for calculating hashes, by default md5.
36+
include_function_name: bool, optional
37+
Whether to include the function name in the idempotent key
3538
"""
3639
self.event_key_jmespath = event_key_jmespath
3740
self.payload_validation_jmespath = payload_validation_jmespath
@@ -41,3 +44,4 @@ def __init__(
4144
self.use_local_cache = use_local_cache
4245
self.local_cache_max_items = local_cache_max_items
4346
self.hash_function = hash_function
47+
self.include_function_name = include_function_name

aws_lambda_powertools/utilities/idempotency/persistence/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def __init__(self):
122122
self.use_local_cache = False
123123
self._cache: Optional[LRUDict] = None
124124
self.hash_function = None
125+
self.include_function_name = True
125126

126127
def configure(self, config: IdempotencyConfig) -> None:
127128
"""
@@ -152,6 +153,7 @@ def configure(self, config: IdempotencyConfig) -> None:
152153
if self.use_local_cache:
153154
self._cache = LRUDict(max_items=config.local_cache_max_items)
154155
self.hash_function = getattr(hashlib, config.hash_function)
156+
self.include_function_name = config.include_function_name
155157

156158
def _get_hashed_idempotency_key(self, event: Dict[str, Any], context: LambdaContext) -> str:
157159
"""
@@ -180,7 +182,11 @@ def _get_hashed_idempotency_key(self, event: Dict[str, Any], context: LambdaCont
180182
raise IdempotencyKeyError("No data found to create a hashed idempotency_key")
181183
warnings.warn(f"No value found for idempotency_key. jmespath: {self.event_key_jmespath}")
182184

183-
return f"{context.function_name}#{self._generate_hash(data)}"
185+
generated_hard = self._generate_hash(data)
186+
if self.include_function_name:
187+
return f"{context.function_name}#{generated_hard}"
188+
else:
189+
return generated_hard
184190

185191
@staticmethod
186192
def is_missing_idempotency_key(data) -> bool:

tests/functional/idempotency/test_idempotency.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,16 @@ def lambda_handler(event, context):
828828
stubber.assert_no_pending_responses()
829829
stubber.deactivate()
830830
assert "Failed to save in progress record to idempotency store" == e.value.args[0]
831+
832+
833+
def test_include_function_name_false(persistence_store: DynamoDBPersistenceLayer):
834+
# GIVEN include_function_name=False
835+
persistence_store.configure(IdempotencyConfig(event_key_jmespath="body", include_function_name=False))
836+
value = "true"
837+
api_gateway_proxy_event = {"body": value}
838+
839+
# WHEN
840+
result = persistence_store._get_hashed_idempotency_key(api_gateway_proxy_event, None)
841+
842+
# THEN
843+
assert result == persistence_store._generate_hash(value)

0 commit comments

Comments
 (0)