Administrator
2026-06-02 85e1673c4683f4699106afe79405877903ce4eb5
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
package com.xcong.excoin.modules.okxNewPrice.okxpi.config;
 
import com.alibaba.fastjson.JSON;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.FebsException;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.HttpMethod;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.enums.RequestType;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.DateUtils;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.RequestBuilder;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.utils.SignUtils;
import com.xcong.excoin.modules.okxNewPrice.okxpi.config.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!");
        }
        return sendApiRequest(baseUrl, urlPath, parameters, httpMethod, RequestType.SIGNED, isSimluate);
    }
 
    public String sendSignedRequestRaw(String baseUrl, String urlPath, String rawBody,
                                        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!");
        }
        String fullUrl = UrlBuilder.buildFullUrl(baseUrl, urlPath, null, null);
        log.debug("{} {}", httpMethod, fullUrl);
        String timestamp = DateUtils.format(DateUtils.FORMAT_UTC_ISO8601, new Date(), 0);
        String queryString = urlPath;
        String sign = SignUtils.signRest(secretKey, timestamp, httpMethod.toString(), queryString, rawBody);
        Request request = RequestBuilder.buildApiKeyRequest(fullUrl, rawBody, passphrase, sign, timestamp, httpMethod, apiKey, isSimluate);
        return ResponseHandler.handleResponse(request, isSimluate);
    }
}