|
|
@@ -0,0 +1,118 @@
|
|
|
+package top.lvzhiqiang;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
|
|
|
+import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
|
|
+import org.jsoup.Connection;
|
|
|
+import org.junit.Test;
|
|
|
+import org.junit.runner.RunWith;
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
+import top.lvzhiqiang.config.InitRunner;
|
|
|
+import top.lvzhiqiang.entity.CoinWatchlist;
|
|
|
+import top.lvzhiqiang.mapper.CoinMapper;
|
|
|
+import top.lvzhiqiang.service.CoinService;
|
|
|
+import top.lvzhiqiang.service.CoinService2;
|
|
|
+import top.lvzhiqiang.service.CoinYoutubeService;
|
|
|
+import top.lvzhiqiang.util.CheckSign4Bitget;
|
|
|
+import top.lvzhiqiang.util.DateUtils;
|
|
|
+import top.lvzhiqiang.util.JsoupUtil;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.net.InetSocketAddress;
|
|
|
+import java.net.Proxy;
|
|
|
+import java.security.InvalidKeyException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 单元测试类
|
|
|
+ *
|
|
|
+ * @author lvzhiqiang
|
|
|
+ * @since 11:19 2022/5/2
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
+@SpringBootTest(properties = {
|
|
|
+ "spring.profiles.active=dev",
|
|
|
+ "logging.level.top.lvzhiqiang=DEBUG"
|
|
|
+}
|
|
|
+)
|
|
|
+public class TestCoin2 {
|
|
|
+ private static final Map<String, String> basicHeaderMap = new HashMap<>();
|
|
|
+
|
|
|
+ private static final String secretKey = "1fdd0fc2976bea80189ba13710e12825ca3ef6c5e25a0d76fd03f8f6cd4a61d9";
|
|
|
+ private static final String mainUrl = "https://api.bitget.com";
|
|
|
+
|
|
|
+ static {
|
|
|
+ // API KEY作为一个字符串。
|
|
|
+ basicHeaderMap.put("ACCESS-KEY", "bg_433d37306df0e8901c6d107c6d9e9111");
|
|
|
+ // 使用base64编码签名(请参阅签名消息)。
|
|
|
+ basicHeaderMap.put("ACCESS-SIGN", "");
|
|
|
+ // 您请求的时间戳。
|
|
|
+ basicHeaderMap.put("ACCESS-TIMESTAMP", "");
|
|
|
+ // 您在创建API KEY时设置的口令。
|
|
|
+ basicHeaderMap.put("ACCESS-PASSPHRASE", "7f934f62f2701bee932204580d115228");
|
|
|
+ // 统一设置为application/json。
|
|
|
+ basicHeaderMap.put("Content-Type", "application/json");
|
|
|
+ // 支持多语言, 如:中文(zh-CN),英语(en-US)
|
|
|
+ basicHeaderMap.put("locale", "zh-CN");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testBitgetMixOrder() {
|
|
|
+ LocalDateTime endTime = LocalDateTime.now();
|
|
|
+ // 全部历史委托列表
|
|
|
+ Map<String, String> paramMap = new LinkedHashMap<>();
|
|
|
+ paramMap.put("productType", "umcbl");
|
|
|
+ paramMap.put("startTime", String.valueOf(DateUtils.localDateTimeToMilliseconds(endTime.minusMinutes(1))));
|
|
|
+ paramMap.put("startTime", String.valueOf(DateUtils.localDateTimeToMilliseconds(endTime.minusMonths(1))));
|
|
|
+ paramMap.put("endTime", String.valueOf(DateUtils.localDateTimeToMilliseconds(endTime)));
|
|
|
+ paramMap.put("pageSize", "100");
|
|
|
+
|
|
|
+ String signQueryString = paramMap.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining("&"));
|
|
|
+ JSONObject response = requestApi4Common("/api/mix/v1/order/historyProductType", signQueryString, null, JsoupUtil.HTTP_GET, paramMap);
|
|
|
+ JSONArray orderList = response.getJSONObject("data").getJSONArray("orderList");
|
|
|
+ System.out.println(orderList);
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject requestApi4Common(String requestPath, String signQueryString, String signBody, String httpMethod, Map<String, String> paramMap) {
|
|
|
+ String timestamp = String.valueOf(System.currentTimeMillis());
|
|
|
+
|
|
|
+ Map<String, String> headerMap = new HashMap<>();
|
|
|
+ headerMap.putAll(basicHeaderMap);
|
|
|
+ try {
|
|
|
+ String accessSign = CheckSign4Bitget.generate(timestamp, httpMethod, requestPath, signQueryString, signBody, secretKey);
|
|
|
+ headerMap.put("ACCESS-TIMESTAMP", timestamp);
|
|
|
+ headerMap.put("ACCESS-SIGN", accessSign);
|
|
|
+ } catch (CloneNotSupportedException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (InvalidKeyException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ String requestUrl = mainUrl + requestPath;
|
|
|
+
|
|
|
+ if (httpMethod.equals(JsoupUtil.HTTP_GET)) {
|
|
|
+ Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 10808));
|
|
|
+ Connection.Response response = JsoupUtil.requestBody(requestUrl, httpMethod, proxy, headerMap, paramMap);
|
|
|
+ return JSONObject.parseObject(response.body());
|
|
|
+ } else {
|
|
|
+ Connection.Response response = JsoupUtil.requestBodyJSON(requestUrl, httpMethod, InitRunner.proxy, null, headerMap, paramMap);
|
|
|
+ return JSONObject.parseObject(response.body());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|