Class IdempotencyConfig.Builder

java.lang.Object
software.amazon.lambda.powertools.idempotency.IdempotencyConfig.Builder
Enclosing class:
IdempotencyConfig

public static class IdempotencyConfig.Builder extends Object
  • Constructor Details

    • Builder

      public Builder()
  • Method Details

    • build

      public IdempotencyConfig build()
      Initialize and return an instance of IdempotencyConfig.
      Example:
       IdempotencyConfig.builder().withUseLocalCache().build();
       
      This instance must then be passed to the Idempotency.Config:
       Idempotency.config().withConfig(config).configure();
       
      Returns:
      an instance of IdempotencyConfig.
    • withEventKeyJMESPath

      public IdempotencyConfig.Builder withEventKeyJMESPath(String eventKeyJMESPath)
      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 APIGatewayV2HTTPEvent
      • Records[*].powertools_json(body) for SQSEvent
      • Records[0].Sns.Message | powertools_json(@) for SNSEvent
      • detail for ScheduledEvent (EventBridge / CloudWatch events)
      • Records[*].kinesis.powertools_json(powertools_base64(data)) for KinesisEvent
      • Records[*].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

      public IdempotencyConfig.Builder withLocalCacheMaxItems(int localCacheMaxItems)
      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

      public IdempotencyConfig.Builder withUseLocalCache(boolean useLocalCache)
      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 the LRUCache
      Returns:
      the instance of the builder (to chain operations)
    • withExpiration

      public IdempotencyConfig.Builder withExpiration(Duration expiration)
      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

      public IdempotencyConfig.Builder withPayloadValidationJMESPath(String payloadValidationJMESPath)
      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

      public IdempotencyConfig.Builder withThrowOnNoIdempotencyKey(boolean throwOnNoIdempotencyKey)
      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

      public IdempotencyConfig.Builder withThrowOnNoIdempotencyKey()
      Throw an exception if no idempotency key was found in the request. Shortcut for withThrowOnNoIdempotencyKey(boolean), forced as true
      Returns:
      the instance of the builder (to chain operations)
    • withHashFunction

      public IdempotencyConfig.Builder withHashFunction(String hashFunction)
      Function to use for calculating hashes, by default MD5.
      Parameters:
      hashFunction - Can be any algorithm supported by MessageDigest, 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: