You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| **EventKeyJMESPath** | `""` | JMESPath expression to extract the idempotency key from the event record. See available [built-in functions](serialization) |
595
+
| **EventKeyJMESPath** | `""` | JMESPath expression to extract the idempotency key from the event record. See available [built-in functions](serialization) |
595
596
| **PayloadValidationJMESPath** | `""` | JMESPath expression to validate whether certain parameters have changed in the event |
596
-
| **ThrowOnNoIdempotencyKey** | `False` | Throw exception if no idempotency key was found in the request |
597
+
| **ThrowOnNoIdempotencyKey** | `false` | Throw exception if no idempotency key was found in the request |
597
598
| **ExpirationInSeconds** | 3600 | The number of seconds to wait before a record is expired |
| **LocalCacheMaxItems** | 256 | Max number of items to store in local cache |
600
601
| **HashFunction** | `MD5` | Algorithm to use for calculating hashes, as supported by `java.security.MessageDigest` (eg. SHA-1, SHA-256, ...) |
602
+
| **ResponseHook** | `null` | Response hook to apply modifications to idempotent responses |
601
603
602
604
These features are detailed below.
603
605
@@ -855,6 +857,58 @@ You can extend the `BasePersistenceStore` class and implement the abstract metho
855
857
856
858
For example, the `putRecord` method needs to throw an exception if a non-expired record already exists in the data store with a matching key.
857
859
860
+
### Manipulating the Idempotent Response
861
+
862
+
You can set up a response hook in the Idempotency configuration to manipulate the returned data when an operation is idempotent. The hook function will be called with the current de-serialized response `Object` and the Idempotency `DataRecord`.
863
+
864
+
The example below shows how to append an HTTP header to an `APIGatewayProxyResponseEvent`.
865
+
866
+
=== "Using an Idempotent Response Hook"
867
+
868
+
```java hl_lines="3-20"
869
+
Idempotency.config().withConfig(
870
+
IdempotencyConfig.builder()
871
+
.withResponseHook((responseData, dataRecord) -> {
872
+
if (responseData instanceof APIGatewayProxyResponseEvent) {
873
+
APIGatewayProxyResponseEvent proxyResponse =
874
+
(APIGatewayProxyResponseEvent) responseData;
875
+
final Map<String, String> headers = new HashMap<>();
The response hook is called after de-serialization so the payload you process will be the de-serialized Java object.
900
+
901
+
#### Being a good citizen
902
+
903
+
When using response hooks to manipulate returned data from idempotent operations, it's important to follow best practices to avoid introducing complexity or issues. Keep these guidelines in mind:
904
+
905
+
1. **Response hook works exclusively when operations are idempotent.** The hook will not be called when an operation is not idempotent, or when the idempotent logic fails.
906
+
907
+
2. **Catch and Handle Exceptions.** Your response hook code should catch and handle any exceptions that may arise from your logic. Unhandled exceptions will cause the Lambda function to fail unexpectedly.
908
+
909
+
3. **Keep Hook Logic Simple** Response hooks should consist of minimal and straightforward logic for manipulating response data. Avoid complex conditional branching and aim for hooks that are easy to reason about.
0 commit comments