Administrator
17 hours ago 75ffb97146a0a15d76ae4603ccf328eb9716d798
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
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);
    }
}