| | |
| | | return isSuccess(result, "设置杠杆"); |
| | | } |
| | | |
| | | public String fetchInstIdCode(String instType, String instId) { |
| | | String path = "/api/v5/account/instruments?instType=" + instType + "&instId=" + instId; |
| | | JSONObject result = get(path); |
| | | if (result == null || !"0".equals(result.getString("code"))) { |
| | | log.error("[REST] 获取instIdCode失败, code:{}, msg:{}", |
| | | result != null ? result.getString("code") : "null", |
| | | result != null ? result.getString("msg") : "no response"); |
| | | return null; |
| | | } |
| | | com.alibaba.fastjson.JSONArray data = result.getJSONArray("data"); |
| | | if (data == null || data.isEmpty()) { |
| | | log.error("[REST] 获取instIdCode失败: data为空"); |
| | | return null; |
| | | } |
| | | JSONObject first = data.getJSONObject(0); |
| | | String instIdCode = first.getString("instIdCode"); |
| | | log.info("[REST] 获取instIdCode成功, instId:{}, instIdCode:{}", instId, instIdCode); |
| | | return instIdCode; |
| | | } |
| | | |
| | | private JSONObject get(String path) { |
| | | HttpURLConnection conn = null; |
| | | try { |
| | | String timestamp = OkxWsUtil.getIso8601Timestamp(); |
| | | String sign = OkxWsUtil.signRest(timestamp, "GET", path, "", secretKey); |
| | | |
| | | URL url = new URL(baseUrl + path); |
| | | conn = (HttpURLConnection) url.openConnection(); |
| | | conn.setRequestMethod("GET"); |
| | | conn.setConnectTimeout(15000); |
| | | conn.setReadTimeout(15000); |
| | | conn.setRequestProperty("Content-Type", "application/json"); |
| | | conn.setRequestProperty("OK-ACCESS-KEY", apiKey); |
| | | conn.setRequestProperty("OK-ACCESS-SIGN", sign); |
| | | conn.setRequestProperty("OK-ACCESS-TIMESTAMP", timestamp); |
| | | conn.setRequestProperty("OK-ACCESS-PASSPHRASE", passphrase); |
| | | if (isSimulate) { |
| | | conn.setRequestProperty("x-simulated-trading", "1"); |
| | | } |
| | | |
| | | int code = conn.getResponseCode(); |
| | | String response = readResponse(conn); |
| | | if (code < 200 || code >= 300) { |
| | | log.error("[REST] GET {} → HTTP {} body:{}", path, code, response); |
| | | } |
| | | |
| | | return JSON.parseObject(response); |
| | | } catch (Exception e) { |
| | | log.error("[REST] GET {} 失败: {}", path, e.getMessage()); |
| | | return null; |
| | | } finally { |
| | | if (conn != null) { |
| | | conn.disconnect(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private JSONObject post(String path, JSONObject body) { |
| | | HttpURLConnection conn = null; |
| | | try { |
| | |
| | | |
| | | int code = conn.getResponseCode(); |
| | | String response = readResponse(conn); |
| | | log.info("[REST] POST {} → HTTP {} body:{}", path, code, response); |
| | | if (code < 200 || code >= 300) { |
| | | log.error("[REST] POST {} → HTTP {} body:{}", path, code, response); |
| | | } |
| | | |
| | | return JSON.parseObject(response); |
| | | } catch (Exception e) { |