-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCall.java
More file actions
170 lines (131 loc) · 6.75 KB
/
Copy pathCall.java
File metadata and controls
170 lines (131 loc) · 6.75 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package com.bitmart.api;
import com.bitmart.api.common.*;
import com.bitmart.api.key.CloudSignature;
import com.bitmart.api.request.Auth;
import com.bitmart.api.request.CloudRequest;
import com.bitmart.api.request.Method;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Map;
import java.util.StringJoiner;
import java.util.concurrent.TimeUnit;
public final class Call {
private static final Logger log = LoggerFactory.getLogger(Call.class);
private final CloudContext cloudContext;
private final OkHttpClient okHttpClient;
private static final String USER_AGENT = "bitmart-java-sdk-api/2.8.0";
private static OkHttpClient createOkHttpClient(CloudContext cloudContext) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return new OkHttpClient.Builder()
.connectTimeout(cloudContext.getConnectTimeoutMilliSeconds(), TimeUnit.MILLISECONDS)
.readTimeout(cloudContext.getReadTimeoutMilliSeconds(), TimeUnit.MILLISECONDS)
.writeTimeout(cloudContext.getWriteTimeoutMilliSeconds(), TimeUnit.MILLISECONDS)
// .addInterceptor(interceptor) // debug
.build();
}
public Call(CloudContext cloudContext) {
this.cloudContext = cloudContext;
this.okHttpClient = createOkHttpClient(cloudContext);
}
public CloudResponse callCloud(CloudRequest cloudRequest) throws CloudException {
return Method.POST.equals(cloudRequest.getMethod()) ? post(cloudRequest) : get(cloudRequest);
}
private CloudResponse post(CloudRequest cloudRequest) throws CloudException {
if (cloudRequest == null) {
throw new CloudException("request can not null");
} else {
Map<String, Object> paraMap = CommonUtils.genRequestMap(cloudRequest);
String json = JsonUtils.toJson(paraMap);
MediaType parse = MediaType.Companion.parse("application/json; charset=utf-8");
RequestBody requestBody = RequestBody.Companion.create(json, parse);
Headers header = setHeaders(cloudRequest, json, this.cloudContext.getCustomHeaders());
if (log.isDebugEnabled()) {
log.debug("URL:{}\nHeader:{}\nBody:{}", this.cloudContext.getCloudUrl() + cloudRequest.getPath(),
header.toMultimap(), json);
}
Request request = (new okhttp3.Request.Builder()).url(this.cloudContext.getCloudUrl() + cloudRequest.getPath())
.headers(header)
.post(requestBody).build();
okhttp3.Call okCall = okHttpClient.newCall(request);
return getResponse(okCall);
}
}
private CloudResponse get(CloudRequest cloudRequest) throws CloudException {
if (cloudRequest == null) {
throw new CloudException("request can not null");
} else {
StringJoiner url = new StringJoiner("");
Map<String, Object> paraMap = CommonUtils.genRequestMap(cloudRequest);
url.add(this.cloudContext.getCloudUrl() + cloudRequest.getPath());
if (!paraMap.isEmpty()) {
url.add("?");
}
String queryString = getQueryString(paraMap);
if (log.isDebugEnabled()) {
log.debug("URL:{}", url + queryString);
}
Headers header = setHeaders(cloudRequest, queryString, this.cloudContext.getCustomHeaders());
Request request = (new okhttp3.Request.Builder()).url(url.toString() + queryString)
.headers(header)
.get().build();
okhttp3.Call okCall = okHttpClient.newCall(request);
return getResponse(okCall);
}
}
private String getQueryString(Map<String, Object> paraMap){
StringJoiner fromData = new StringJoiner("");
for (Map.Entry<String, Object> entry : paraMap.entrySet()) {
fromData.add(entry.getKey()).add("=").add(entry.getValue().toString()).add("&");
}
String queryString = fromData.toString();
if (queryString.endsWith("&")) {
queryString = queryString.substring(0, queryString.length() - 1);
}
return queryString;
}
private Headers setHeaders(CloudRequest cloudRequest, String queryString, Map<String, String> customHeaders) throws CloudException {
Headers.Builder headerBuilder = new Headers.Builder();
headerBuilder.add(GlobalConst.USER_AGENT, USER_AGENT);
if (Auth.KEYED == cloudRequest.getAuth()) {
headerBuilder.add(GlobalConst.X_BM_KEY, this.cloudContext.getCloudKey().getApiKey());
} else if (Auth.SIGNED == cloudRequest.getAuth()) {
CloudSignature.Signature signature = CloudSignature.create(queryString, this.cloudContext.getCloudKey().getApiSecret(), this.cloudContext.getCloudKey().getMemo());
headerBuilder.add(GlobalConst.X_BM_KEY, this.cloudContext.getCloudKey().getApiKey());
headerBuilder.add(GlobalConst.X_BM_TIMESTAMP, signature.getTimestamp());
headerBuilder.add(GlobalConst.X_BM_SIGN, signature.getSign());
}
// add custom headers
if (customHeaders != null && !customHeaders.isEmpty()) {
for (Map.Entry<String, String> entry : customHeaders.entrySet()) {
headerBuilder.add(entry.getKey(), entry.getValue());
}
}
return headerBuilder.build();
}
private CloudResponse getResponse(okhttp3.Call okCall) throws CloudException {
try {
Response response = okCall.execute();
final CloudResponse cloudResponse = new CloudResponse()
.setResponseContent(response.body().string())
.setResponseHttpStatus(response.code())
.setCloudLimit(new CloudLimit()
.setLimit(CommonUtils.getRateLimitValue(response.header("X-BM-RateLimit-Limit")))
.setRemaining(CommonUtils.getRateLimitValue(response.header("X-BM-RateLimit-Remaining")))
.setReset(CommonUtils.getRateLimitValue(response.header("X-BM-RateLimit-Reset")))
.setMode(response.header("X-BM-RateLimit-Mode"))
);
if (log.isDebugEnabled()) {
log.debug("Response:{}", cloudResponse);
}
return cloudResponse;
} catch (IOException var18) {
throw new CloudException("Error: " + var18.getMessage());
} catch (Exception var19) {
throw new CloudException("Error: " + var19.getMessage());
}
}
}