zj
2025-10-17 bbc4713b23778aebc1eb3d46cb04d539179d883d
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.yami.trading.huobi.websocket.service.huobi.connection;
 
import java.net.MalformedURLException;
import java.net.URL;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.Request;
 
import com.yami.trading.huobi.websocket.constant.EtfResult;
import com.yami.trading.huobi.websocket.constant.Options;
import com.yami.trading.huobi.websocket.exception.SDKException;
import com.yami.trading.huobi.websocket.service.huobi.signature.ApiSignature;
import com.yami.trading.huobi.websocket.service.huobi.signature.ApiSignatureED25519;
import com.yami.trading.huobi.websocket.service.huobi.signature.UrlParamsBuilder;
import com.yami.trading.huobi.websocket.utils.ConnectionFactory;
 
public class HuobiRestConnection {
 
    private Options options;
 
    String host;
 
    public Options getOptions() {
        return options;
    }
 
    public HuobiRestConnection(Options options) {
        this.options = options;
        try {
            this.host = new URL(this.options.getRestHost()).getHost();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
 
    public JSONObject executeGet(String path, UrlParamsBuilder paramsBuilder) {
 
        Options options = this.getOptions();
 
        String url = options.getRestHost() + path + paramsBuilder.buildUrl();
 
        Request executeRequest = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
 
        String resp = ConnectionFactory.execute(executeRequest);
        return checkAndGetResponse(resp);
    }
 
    public String executeGetString(String url, UrlParamsBuilder paramsBuilder) {
        String realUrl = url + paramsBuilder.buildUrl();
        Request executeRequest = new Request.Builder()
                .url(realUrl)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .build();
        String resp = ConnectionFactory.execute(executeRequest);
        return resp;
    }
 
  public JSONObject executeGetWithSignature(String path, UrlParamsBuilder paramsBuilder) {
 
 
    Options options = this.getOptions();
 
    String requestUrl =  options.getRestHost() + path;
    new ApiSignature().createSignature(options.getApiKey(), options.getSecretKey(), "GET", host, path, paramsBuilder);
    requestUrl += paramsBuilder.buildUrl();
    System.out.println(requestUrl);
 
    Request executeRequest = new Request.Builder().url(requestUrl)
        .addHeader("Content-Type", "application/x-www-form-urlencoded").build();
 
    String resp = ConnectionFactory.execute(executeRequest);
    return checkAndGetResponse(resp);
  }
 
 
  public JSONObject executePostWithSignature(String path, UrlParamsBuilder paramsBuilder){
    Options options = this.getOptions();
    String requestUrl =  options.getRestHost() + path;
    new ApiSignature().createSignature(options.getApiKey(), options.getSecretKey(), "POST", host, path, paramsBuilder);
    requestUrl += paramsBuilder.buildUrl();
    Request executeRequest = new Request.Builder().url(requestUrl).post(paramsBuilder.buildPostBody())
        .addHeader("Content-Type", "application/json").build();
 
    String resp = ConnectionFactory.execute(executeRequest);
    return checkAndGetResponse(resp);
  }
 
   /* public JSONObject executeGetWithSignature(String path, UrlParamsBuilder paramsBuilder) {
        Options options = this.getOptions();
        System.out.println("API Key: " + options.getApiKey());
        System.out.println("Secret Key: " + options.getSecretKey());
//        paramsBuilder.putToUrl("AccessKeyId", options.getApiKey());
// 创建 ApiSignatureED25519 实例并传递 Base64 编码的私钥和公钥
        ApiSignatureED25519 apiSignature = new ApiSignatureED25519();
        try {
            apiSignature.ApiSignature( options.getApiKey(),options.getSecretKey()); // 确保顺序正确
        } catch (Exception e) {
            // 抛出 SDKException 只使用 String 参数
            e.printStackTrace();
            throw new SDKException(SDKException.KEY_MISSING, "Invalid API key or secret key: " + e.getMessage());
        }
        String requestUrl = options.getRestHost() + path;
        apiSignature.createSignature("GET", host, path, paramsBuilder,options.getApiKey());
        requestUrl += paramsBuilder.buildUrl();
        System.out.println(requestUrl);
 
        Request executeRequest = new Request.Builder().url(requestUrl)
                .addHeader("Content-Type", "application/x-www-form-urlencoded").
                addHeader("x-connecting-ip", "192.2.12.19")
                .addHeader("HB-REAL-IP", "192.2.12.19")
                .addHeader("X-Forwarded-For", "192.2.12.19")
                .addHeader("X-Real-IP", "192.2.12.19")
        .build();
 
        String resp = ConnectionFactory.execute(executeRequest);
        return checkAndGetResponse(resp);
    }
 
 
    public JSONObject executePostWithSignature(String path, UrlParamsBuilder paramsBuilder) {
 
        Options options = this.getOptions();
        ApiSignatureED25519 apiSignature = new ApiSignatureED25519();
        try {
            apiSignature.ApiSignature(options.getSecretKey(), options.getApiKey()); // 确保顺序正确
        } catch (Exception e) {
            // 抛出 SDKException 只使用 String 参数
            throw new SDKException(SDKException.KEY_MISSING, "Invalid API key or secret key: " + e.getMessage());
        }
        String requestUrl = options.getRestHost() + path;
        apiSignature.createSignature("POST", host, path, paramsBuilder,options.getApiKey());
        requestUrl += paramsBuilder.buildUrl();
        Request executeRequest = new Request.Builder().url(requestUrl).post(paramsBuilder.buildPostBody())
                .addHeader("Content-Type", "application/json")
                .addHeader("x-connecting-ip", "192.2.12.19")
                .addHeader("HB-REAL-IP", "192.2.12.19")
                .addHeader("X-Forwarded-For", "192.2.12.19")
                .addHeader("X-Real-IP", "192.2.12.19").build();
 
        String resp = ConnectionFactory.execute(executeRequest);
        return checkAndGetResponse(resp);
    }*/
 
    JSONObject checkAndGetResponse(String resp) {
        JSONObject json = JSON.parseObject(resp);
        try {
            if (json.containsKey("status")) {
                String status = json.getString("status");
                if ("error".equals(status)) {
                    String err_code = json.getString("err-code");
                    String err_msg = json.getString("err-msg");
                    throw new SDKException(SDKException.EXEC_ERROR, "[Executing] " + err_code + ": " + err_msg);
                } else if (!"ok".equals(status)) {
                    throw new SDKException(SDKException.RUNTIME_ERROR, "[Invoking] Response is not expected: " + status);
                }
            } else if (json.containsKey("success")) {
                boolean success = json.getBoolean("success");
                if (!success) {
                    String err_code = EtfResult.checkResult(json.getInteger("code"));
                    String err_msg = json.getString("message");
                    if ("".equals(err_code)) {
                        throw new SDKException(SDKException.EXEC_ERROR, "[Executing] " + err_msg);
                    } else {
                        throw new SDKException(SDKException.EXEC_ERROR, "[Executing] " + err_code + ": " + err_msg);
                    }
                }
            } else if (json.containsKey("code")) {
                int code = json.getInteger("code");
                if (code != 200) {
                    String message = json.getString("message");
                    throw new SDKException(SDKException.EXEC_ERROR, "[Executing]" + message);
                }
            } else {
                throw new SDKException(SDKException.RUNTIME_ERROR, "[Invoking] Status cannot be found in response.");
            }
        } catch (SDKException e) {
            throw e;
        } catch (Exception e) {
            throw new SDKException(SDKException.RUNTIME_ERROR, "[Invoking] Unexpected error: " + e.getMessage());
        }
 
        return json;
    }
 
}