Class IdempotencyConfig.Builder
java.lang.Object
software.amazon.lambda.powertools.idempotency.IdempotencyConfig.Builder
- Enclosing class:
IdempotencyConfig
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbuild()withEventKeyJMESPath(String eventKeyJMESPath) A JMESPath expression to extract the idempotency key from the event record.withExpiration(Duration expiration) The number of seconds to wait before a record is expiredwithHashFunction(String hashFunction) Function to use for calculating hashes, by default MD5.withLocalCacheMaxItems(int localCacheMaxItems) Set the maximum number of items to store in local cache, by default 256withPayloadValidationJMESPath(String payloadValidationJMESPath) A JMESPath expression to extract the payload to be validated from the event record.withResponseHook(BiFunction<Object, DataRecord, Object> responseHook) Response hook that will be called for each idempotent response.Throw an exception if no idempotency key was found in the request.withThrowOnNoIdempotencyKey(boolean throwOnNoIdempotencyKey) Whether to throw an exception if no idempotency key was found in the request, by default falsewithUseLocalCache(boolean useLocalCache) Whether to locally cache idempotency results, by default false
-
Constructor Details
-
Builder
public Builder()
-
-
Method Details
-
build
Initialize and return an instance ofIdempotencyConfig.
Example:
IdempotencyConfig.builder().withUseLocalCache().build();
This instance must then be passed to theIdempotency.Config:Idempotency.config().withConfig(config).configure();
- Returns:
- an instance of
IdempotencyConfig.
-
withEventKeyJMESPath
A JMESPath expression to extract the idempotency key from the event record.
See https://jmespath.org/ for more details.
Common paths are:powertools_json(body)for APIGatewayProxyRequestEvent and APIGatewayV2HTTPEventRecords[*].powertools_json(body)for SQSEventRecords[0].Sns.Message | powertools_json(@)for SNSEventdetailfor ScheduledEvent (EventBridge / CloudWatch events)Records[*].kinesis.powertools_json(powertools_base64(data))for KinesisEventRecords[*].powertools_json(powertools_base64(data))for KinesisFirehoseEvent- ...
- Parameters:
eventKeyJMESPath- path of the key in the Lambda event- Returns:
- the instance of the builder (to chain operations)
-
withLocalCacheMaxItems
Set the maximum number of items to store in local cache, by default 256- Parameters:
localCacheMaxItems- maximum number of items to store in local cache- Returns:
- the instance of the builder (to chain operations)
-
withUseLocalCache
Whether to locally cache idempotency results, by default false- Parameters:
useLocalCache- boolean that indicate if a local cache must be used in addition to the persistence store. If set to true, will use theLRUCache- Returns:
- the instance of the builder (to chain operations)
-
withExpiration
The number of seconds to wait before a record is expired- Parameters:
expiration- expiration of the record in the store- Returns:
- the instance of the builder (to chain operations)
-
withPayloadValidationJMESPath
A JMESPath expression to extract the payload to be validated from the event record.
See https://jmespath.org/ for more details.- Parameters:
payloadValidationJMESPath- JMES Path of a part of the payload to be used for validation- Returns:
- the instance of the builder (to chain operations)
-
withThrowOnNoIdempotencyKey
Whether to throw an exception if no idempotency key was found in the request, by default false- Parameters:
throwOnNoIdempotencyKey- boolean to indicate if we must throw an Exception when idempotency key could not be found in the payload.- Returns:
- the instance of the builder (to chain operations)
-
withThrowOnNoIdempotencyKey
Throw an exception if no idempotency key was found in the request. Shortcut forwithThrowOnNoIdempotencyKey(boolean), forced as true- Returns:
- the instance of the builder (to chain operations)
-
withHashFunction
Function to use for calculating hashes, by default MD5.- Parameters:
hashFunction- Can be any algorithm supported byMessageDigest, most commons are- MD5
- SHA-1
- SHA-256
- Returns:
- the instance of the builder (to chain operations)
-
withResponseHook
public IdempotencyConfig.Builder withResponseHook(BiFunction<Object, DataRecord, Object> responseHook) Response hook that will be called for each idempotent response. This hook will receive the de-serialized response data from the persistence store as first argument and the original DataRecord from the persistence store as second argument. Usage:IdempotencyConfig.builder().withResponseHook((responseData, dataRecord) -> { // do something with the response data, for example: if(responseData instanceof APIGatewayProxyRequestEvent) { ((APIGatewayProxyRequestEvent) responseData).setHeaders(Map.of("x-idempotency-response", "true") } return responseData; })- Parameters:
responseHook-- Returns:
-