package com.xcong.excoin.modules.newPrice;
|
|
import com.alibaba.fastjson.JSON;
|
import com.xcong.excoin.common.exception.FebsException;
|
import com.xcong.excoin.modules.newPrice.enums.HttpMethod;
|
import com.xcong.excoin.modules.newPrice.enums.RequestType;
|
import com.xcong.excoin.modules.newPrice.utils.DateUtils;
|
import com.xcong.excoin.modules.newPrice.utils.RequestBuilder;
|
import com.xcong.excoin.modules.newPrice.utils.SignUtils;
|
import com.xcong.excoin.modules.newPrice.utils.UrlBuilder;
|
import lombok.extern.slf4j.Slf4j;
|
import okhttp3.Request;
|
|
import java.util.Date;
|
import java.util.LinkedHashMap;
|
|
@Slf4j
|
public class RequestHandler {
|
private final String apiKey;
|
private final String secretKey;
|
private final String passphrase;
|
|
public RequestHandler(String apiKey) {
|
this.apiKey = apiKey;
|
this.secretKey = null;
|
this.passphrase = null;
|
}
|
|
public RequestHandler(String apiKey, String secretKey, String passphrase) {
|
this.apiKey = apiKey;
|
this.secretKey = secretKey;
|
this.passphrase = passphrase;
|
}
|
|
public static void main(String[] args) {
|
LinkedHashMap<String, Object> balanceParameters = new LinkedHashMap<>();
|
String queryString = UrlBuilder.joinQueryParameters(new StringBuilder("/api/v5/account/balance"), balanceParameters).toString();
|
String balanceParameters1 = UrlBuilder.buildFullUrl("/api/v5/account/balance","" , balanceParameters, null);
|
System.out.println(queryString);
|
System.out.println(balanceParameters1);
|
}
|
|
/**
|
* Build request based on request type and send the requests to server.
|
*
|
* @param baseUrl
|
* @param urlPath
|
* @param parameters
|
* @param httpMethod
|
* @param requestType
|
* @return String - response from server
|
*/
|
private String sendApiRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
|
HttpMethod httpMethod, RequestType requestType, boolean isSimluate) {
|
String fullUrl = UrlBuilder.buildFullUrl(baseUrl, urlPath, parameters, null);
|
log.debug("{} {}", httpMethod, fullUrl);
|
//System.out.println("sendApiRequest:fullUrl"+fullUrl);
|
Request request;
|
switch (requestType) {
|
case PUBLIC:
|
request = RequestBuilder.buildPublicRequest(fullUrl, httpMethod, isSimluate).build();
|
break;
|
case WITH_API_KEY:
|
case SIGNED:
|
// 获取签名
|
String timestamp = DateUtils.format(DateUtils.FORMAT_UTC_ISO8601, new Date(), 0);
|
String queryString = UrlBuilder.joinQueryParameters(new StringBuilder(urlPath), parameters).toString();
|
// String timestamp = System.currentTimeMillis()+"";
|
// System.out.println("timestamp:"+timestamp);
|
// System.out.println("timestamp:"+timestamp);
|
// System.out.println("secretKey:"+secretKey);
|
// System.out.println("httpMethod.toString():"+httpMethod.toString());
|
// System.out.println("queryString:"+queryString);
|
// System.out.println("passphrase:"+passphrase);
|
// 组装body
|
String body = "";
|
if (HttpMethod.POST.equals(httpMethod)) {
|
body = JSON.toJSONString(parameters);
|
queryString = UrlBuilder.joinQueryParameters(new StringBuilder(urlPath), null).toString();
|
fullUrl = UrlBuilder.buildFullUrl(baseUrl, urlPath, null, null);
|
}
|
if (HttpMethod.GET.equals(httpMethod)) {
|
queryString = UrlBuilder.buildFullUrl(urlPath,"" , parameters, null);
|
// queryString = UrlBuilder.buildFullUrl(null, urlPath, parameters, null);
|
}
|
String sign = SignUtils.signRest(secretKey,
|
timestamp,
|
httpMethod.toString(),
|
queryString, body);
|
|
|
request = RequestBuilder.buildApiKeyRequest(fullUrl, body, passphrase, sign, timestamp, httpMethod, apiKey,isSimluate);
|
|
|
break;
|
default:
|
throw new FebsException("[RequestHandler] Invalid request type: " + requestType);
|
}
|
return ResponseHandler.handleResponse(request, isSimluate);
|
}
|
|
public String sendPublicRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
|
HttpMethod httpMethod, boolean isSimluate) {
|
return sendApiRequest(baseUrl, urlPath, parameters, httpMethod, RequestType.PUBLIC, isSimluate);
|
}
|
|
public String sendWithApiKeyRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
|
HttpMethod httpMethod, boolean isSimluate) {
|
if (null == apiKey || apiKey.isEmpty()) {
|
throw new FebsException("[RequestHandler] API key cannot be null or empty!");
|
}
|
return sendApiRequest(baseUrl, urlPath, parameters, httpMethod, RequestType.WITH_API_KEY, isSimluate);
|
}
|
|
public String sendSignedRequest(String baseUrl, String urlPath, LinkedHashMap<String, Object> parameters,
|
HttpMethod httpMethod, boolean isSimluate) {
|
if (null == secretKey || secretKey.isEmpty() || null == apiKey || apiKey.isEmpty()) {
|
throw new FebsException("[RequestHandler] Secret key/API key cannot be null or empty!");
|
}
|
//parameters.put("timestamp", UrlBuilder.buildTimestamp());
|
//String queryString = UrlBuilder.joinQueryParameters(parameters);
|
//String signature = SignatureGenerator.getSignature(queryString, secretKey);
|
return sendApiRequest(baseUrl, urlPath, parameters, httpMethod, RequestType.SIGNED, isSimluate);
|
}
|
}
|