forked from cloudbees-oss/zendesk-java-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdempotencyUtil.java
More file actions
87 lines (75 loc) · 3.22 KB
/
IdempotencyUtil.java
File metadata and controls
87 lines (75 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package org.zendesk.client.v2;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;
import org.zendesk.client.v2.model.IdempotentResult;
/**
* Utility class for handling Zendesk API idempotency keys.
*
* <p>Provides methods to add idempotency headers to requests and process idempotency-related
* response headers. Supports the Zendesk API's idempotency feature which allows safe retries of
* create operations without creating duplicate resources.
*
* @see <a href="https://developer.zendesk.com/api-reference/ticketing/introduction/#idempotency">
* Zendesk API Idempotency</a>
* @since 1.5.0
*/
public class IdempotencyUtil {
static final String IDEMPOTENCY_KEY_HEADER = "Idempotency-Key";
static final String IDEMPOTENCY_LOOKUP_HEADER = "x-idempotency-lookup";
static final String IDEMPOTENCY_LOOKUP_HIT = "hit";
static final String IDEMPOTENCY_LOOKUP_MISS = "miss";
static final String IDEMPOTENCY_ERROR_NAME = "IdempotentRequestError";
public static RequestBuilder addIdempotencyHeader(RequestBuilder builder, String idempotencyKey) {
// https://developer.zendesk.com/api-reference/ticketing/introduction/#idempotency
return builder.setHeader(IDEMPOTENCY_KEY_HEADER, idempotencyKey);
}
public static <T> AsyncCompletionHandler<IdempotentResult<T>> wrapHandler(
AsyncCompletionHandler<T> handler) {
return new AsyncCompletionHandler<>() {
@Override
public IdempotentResult<T> onCompleted(Response response) throws Exception {
T entity = handler.onCompleted(response);
boolean duplicateRequest = isDuplicateResponse(response);
return new IdempotentResult<>(entity, duplicateRequest);
}
@Override
public void onThrowable(Throwable t) {
handler.onThrowable(t);
}
};
}
public static boolean isIdempotencyConflict(Response response, ObjectMapper mapper)
throws JsonProcessingException {
if (response.getStatusCode() != 400) {
return false;
}
// Note: Jackson's own docs are a bit outdated in that `readTree` returns
// `MissingNode.getInstance()` and not `null` when given an essentially empty string.
JsonNode error = mapper.readTree(response.getResponseBody()).path("error");
return IDEMPOTENCY_ERROR_NAME.equals(error.textValue());
}
private static boolean isDuplicateResponse(Response response) {
// https://developer.zendesk.com/api-reference/ticketing/introduction/#idempotency
String idempotencyLookup = response.getHeader(IDEMPOTENCY_LOOKUP_HEADER);
if (idempotencyLookup == null) {
idempotencyLookup = "<absent>";
}
switch (idempotencyLookup) {
case IDEMPOTENCY_LOOKUP_HIT:
return true;
case IDEMPOTENCY_LOOKUP_MISS:
return false;
default:
throw new IllegalArgumentException(
String.format(
"Unexpected value of the idempotency lookup header: %s", idempotencyLookup));
}
}
private IdempotencyUtil() {
throw new UnsupportedOperationException("Utility class");
}
}