99import warnings
1010from abc import ABC , abstractmethod
1111from types import MappingProxyType
12- from typing import Any , Dict
12+ from typing import Any , Dict , Optional
1313
1414import jmespath
1515
1616from aws_lambda_powertools .shared .cache_dict import LRUDict
1717from aws_lambda_powertools .shared .jmespath_functions import PowertoolsFunctions
1818from aws_lambda_powertools .shared .json_encoder import Encoder
19+ from aws_lambda_powertools .utilities .idempotency .config import IdempotencyConfig
1920from aws_lambda_powertools .utilities .idempotency .exceptions import (
2021 IdempotencyInvalidStatusError ,
2122 IdempotencyItemAlreadyExistsError ,
@@ -107,55 +108,49 @@ class BasePersistenceLayer(ABC):
107108 Abstract Base Class for Idempotency persistence layer.
108109 """
109110
110- def __init__ (
111- self ,
112- event_key_jmespath : str = "" ,
113- payload_validation_jmespath : str = "" ,
114- expires_after_seconds : int = 60 * 60 , # 1 hour default
115- use_local_cache : bool = False ,
116- local_cache_max_items : int = 256 ,
117- hash_function : str = "md5" ,
118- raise_on_no_idempotency_key : bool = False ,
119- jmespath_options : Dict = None ,
120- ) -> None :
111+ def __init__ (self ):
112+ """Initialize the defaults """
113+ self .configured = False
114+ self .event_key_jmespath : Optional [str ] = None
115+ self .event_key_compiled_jmespath = None
116+ self .jmespath_options : Optional [dict ] = None
117+ self .payload_validation_enabled = False
118+ self .validation_key_jmespath = None
119+ self .raise_on_no_idempotency_key = False
120+ self .expires_after_seconds : int = 60 * 60 # 1 hour default
121+ self .use_local_cache = False
122+ self ._cache : Optional [LRUDict ] = None
123+ self .hash_function = None
124+
125+ def configure (self , config : IdempotencyConfig ) -> None :
121126 """
122- Initialize the base persistence layer
127+ Initialize the base persistence layer from the configuration settings
123128
124129 Parameters
125130 ----------
126- event_key_jmespath: str
127- A jmespath expression to extract the idempotency key from the event record
128- payload_validation_jmespath: str
129- A jmespath expression to extract the payload to be validated from the event record
130- expires_after_seconds: int
131- The number of seconds to wait before a record is expired
132- use_local_cache: bool, optional
133- Whether to locally cache idempotency results, by default False
134- local_cache_max_items: int, optional
135- Max number of items to store in local cache, by default 1024
136- hash_function: str, optional
137- Function to use for calculating hashes, by default md5.
138- raise_on_no_idempotency_key: bool, optional
139- Raise exception if no idempotency key was found in the request, by default False
140- jmespath_options : Dict
141- Alternative JMESPath options to be included when filtering expr
142- """
143- self .event_key_jmespath = event_key_jmespath
144- if self .event_key_jmespath :
145- self .event_key_compiled_jmespath = jmespath .compile (event_key_jmespath )
146- self .expires_after_seconds = expires_after_seconds
147- self .use_local_cache = use_local_cache
148- if self .use_local_cache :
149- self ._cache = LRUDict (max_items = local_cache_max_items )
150- self .payload_validation_enabled = False
151- if payload_validation_jmespath :
152- self .validation_key_jmespath = jmespath .compile (payload_validation_jmespath )
131+ config: IdempotencyConfig
132+ Idempotency configuration settings
133+ """
134+ if self .configured :
135+ # Prevent being reconfigured multiple times
136+ return
137+ self .configured = True
138+
139+ self .event_key_jmespath = config .event_key_jmespath
140+ if config .event_key_jmespath :
141+ self .event_key_compiled_jmespath = jmespath .compile (config .event_key_jmespath )
142+ self .jmespath_options = config .jmespath_options
143+ if not self .jmespath_options :
144+ self .jmespath_options = {"custom_functions" : PowertoolsFunctions ()}
145+ if config .payload_validation_jmespath :
146+ self .validation_key_jmespath = jmespath .compile (config .payload_validation_jmespath )
153147 self .payload_validation_enabled = True
154- self .hash_function = getattr (hashlib , hash_function )
155- self .raise_on_no_idempotency_key = raise_on_no_idempotency_key
156- if not jmespath_options :
157- jmespath_options = {"custom_functions" : PowertoolsFunctions ()}
158- self .jmespath_options = jmespath_options
148+ self .raise_on_no_idempotency_key = config .raise_on_no_idempotency_key
149+ self .expires_after_seconds = config .expires_after_seconds
150+ self .use_local_cache = config .use_local_cache
151+ if self .use_local_cache :
152+ self ._cache = LRUDict (max_items = config .local_cache_max_items )
153+ self .hash_function = getattr (hashlib , config .hash_function )
159154
160155 def _get_hashed_idempotency_key (self , lambda_event : Dict [str , Any ]) -> str :
161156 """
@@ -180,9 +175,9 @@ def _get_hashed_idempotency_key(self, lambda_event: Dict[str, Any]) -> str:
180175 )
181176
182177 if self .is_missing_idempotency_key (data ):
183- warnings .warn (f"No value found for idempotency_key. jmespath: { self .event_key_jmespath } " )
184178 if self .raise_on_no_idempotency_key :
185179 raise IdempotencyKeyError ("No data found to create a hashed idempotency_key" )
180+ warnings .warn (f"No value found for idempotency_key. jmespath: { self .event_key_jmespath } " )
186181
187182 return self ._generate_hash (data )
188183
0 commit comments