-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDapiRequest.java
More file actions
156 lines (126 loc) · 6.09 KB
/
DapiRequest.java
File metadata and controls
156 lines (126 loc) · 6.09 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package co.dapi;
import co.dapi.types.UserInput;
import com.google.gson.*;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class DapiRequest {
public final static String Dapi_URL = "https://api.dapi.com";
public final static String DD_URL = "https://dd.dapi.com";
public final static String SECURE_DD_URL = "https://dd.secure.dapi.com";
private final static String LIBRARY_VERSION = "1.6.0";
private final static String LIBRARY = "dapi-java";
final static Gson jsonAgent = new Gson().newBuilder()
.disableHtmlEscaping()
.registerTypeAdapter(HashMap.class, new SDKRequestSerializer())
.create();
private static OkHttpClient httpClient = null;
private static OkHttpClient getHttpClient(){
if(httpClient == null){
HttpLoggingInterceptor logger = new HttpLoggingInterceptor();
logger.setLevel(HttpLoggingInterceptor.Level.BODY);
httpClient = new OkHttpClient().newBuilder()
.addInterceptor(logger)
.readTimeout(2, TimeUnit.MINUTES)
.build();
}
return httpClient;
}
public static Response HandleSDK(String bodyJson, HashMap<String, String> headersMap) throws IOException {
headersMap.put("host", "dd.dapi.com");
return doRequest(bodyJson, DD_URL, headersMap);
}
public static Response HandleSecureSDK(String bodyJson, HashMap<String, String> headersMap) throws IOException {
headersMap.put("host", "dd.secure.dapi.com");
return doRequest(bodyJson, SECURE_DD_URL, headersMap);
}
public static String Do(String bodyJson, String url) throws IOException {
return Do(bodyJson, url, new HashMap<>());
}
public static String Do(String bodyJson, String url, HashMap<String, String> headersMap) throws IOException {
// Execute the request and get the response
Response resp = doRequest(bodyJson, url, headersMap);
// Return the response body if it's there, otherwise return an empty string
ResponseBody respBody = resp.body();
if (respBody == null)
return "{}";
return respBody.string();
}
private static Response doRequest(String bodyJson, String url, HashMap<String, String> headersMap) throws IOException {
// Build the headers from the default values, plus the passed ones
Headers.Builder headersBuilder = new Headers.Builder();
for (Map.Entry<String, String> header : headersMap.entrySet()) {
headersBuilder.set(header.getKey(), header.getValue());
}
headersBuilder.set("content-type", "application/json");
headersBuilder.set("X-Dapi-Library-Version", LIBRARY_VERSION);
headersBuilder.set("X-Dapi-Library", LIBRARY);
// Create the request
RequestBody reqBody = RequestBody.create(bodyJson, MediaType.parse("application/json"));
Request req = new Request.Builder()
.url(url)
.method("POST", reqBody)
.headers(headersBuilder.build())
.build();
// Execute the request and return the response
return getHttpClient().newCall(req).execute();
}
static class BaseRequest {
private final String appSecret;
private final String userSecret;
private final String operationID;
private final UserInput[] userInputs;
public BaseRequest(String appSecret, String userSecret, String operationID, UserInput[] userInputs) {
this.appSecret = appSecret;
this.userSecret = userSecret;
this.operationID = operationID;
this.userInputs = userInputs;
}
}
static class SDKRequestSerializer implements JsonSerializer<HashMap<String, Object>> {
@Override
public JsonElement serialize(HashMap<String, Object> src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject output = new JsonObject();
// loop over each field in the request
for (Map.Entry<String, Object> kv : src.entrySet()) {
String fieldName = kv.getKey();
Object fieldValue = kv.getValue();
// if the current field is not the userInputs, or the type of userInputs is not
// as expected, serialize it using the default serializer.
if (!fieldName.equals("userInputs") || !(fieldValue instanceof List)) {
output.add(fieldName, context.serialize(fieldValue));
continue;
}
JsonArray userInputsOutput = new JsonArray();
// loop over each element in the userInputs array
for (Object ui : (List<?>) fieldValue) {
if (!(ui instanceof Map)) {
userInputsOutput.add(context.serialize(ui));
continue;
}
JsonObject outputUiObj = new JsonObject();
// loop over each field of the current userInput object
for (Map.Entry<?, ?> uiEntry : ((Map<?, ?>) ui).entrySet()) {
String uiFieldName = (String) uiEntry.getKey();
Object uiFieldValue = uiEntry.getValue();
// handle only the index field, so use the default serializer otherwise
if (!uiFieldName.equals("index")) {
outputUiObj.add(uiFieldName, context.serialize(uiFieldValue));
} else {
int index = ((int) ((double) uiFieldValue));
outputUiObj.add(uiFieldName, new JsonPrimitive(index));
}
}
userInputsOutput.add(outputUiObj);
}
output.add(fieldName, userInputsOutput);
}
return output;
}
}
}