diff --git a/src/main/java/com/huobi/client/impl/AccountsInfoMap.java b/src/main/java/com/huobi/client/impl/AccountsInfoMap.java new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/com/huobi/client/impl/RestApiInvoker.java b/src/main/java/com/huobi/client/impl/RestApiInvoker.java new file mode 100644 index 00000000..e69de29b diff --git a/src/main/java/com/huobi/constant/HuobiOptions.java b/src/main/java/com/huobi/constant/HuobiOptions.java index 2c90b904..8f5c25a1 100644 --- a/src/main/java/com/huobi/constant/HuobiOptions.java +++ b/src/main/java/com/huobi/constant/HuobiOptions.java @@ -7,6 +7,8 @@ import com.huobi.constant.enums.ExchangeEnum; +import java.net.Proxy; + @Builder @AllArgsConstructor @NoArgsConstructor @@ -22,7 +24,7 @@ public class HuobiOptions implements Options { private String apiKey; private String secretKey; - + private Proxy proxy; @Builder.Default private boolean websocketAutoConnect = true; @@ -56,4 +58,8 @@ public boolean isWebSocketAutoConnect() { return this.websocketAutoConnect; } + @Override + public Proxy getProxy(){ + return this.proxy; + }; } diff --git a/src/main/java/com/huobi/constant/Options.java b/src/main/java/com/huobi/constant/Options.java index 9a4b1683..3fc8d789 100644 --- a/src/main/java/com/huobi/constant/Options.java +++ b/src/main/java/com/huobi/constant/Options.java @@ -2,6 +2,8 @@ import com.huobi.constant.enums.ExchangeEnum; +import java.net.Proxy; + public interface Options { String getApiKey(); @@ -16,4 +18,6 @@ public interface Options { boolean isWebSocketAutoConnect(); + Proxy getProxy(); + } diff --git a/src/main/java/com/huobi/service/huobi/connection/HuobiRestConnection.java b/src/main/java/com/huobi/service/huobi/connection/HuobiRestConnection.java index 7abd8956..ac779ef7 100644 --- a/src/main/java/com/huobi/service/huobi/connection/HuobiRestConnection.java +++ b/src/main/java/com/huobi/service/huobi/connection/HuobiRestConnection.java @@ -26,6 +26,7 @@ public Options getOptions() { public HuobiRestConnection(Options options) { this.options = options; + ConnectionFactory.initFactory(options.getProxy()); try { this.host = new URL(this.options.getRestHost()).getHost(); } catch (MalformedURLException e) { diff --git a/src/main/java/com/huobi/utils/ConnectionFactory.java b/src/main/java/com/huobi/utils/ConnectionFactory.java index 8751c14d..769e520d 100644 --- a/src/main/java/com/huobi/utils/ConnectionFactory.java +++ b/src/main/java/com/huobi/utils/ConnectionFactory.java @@ -1,13 +1,12 @@ package com.huobi.utils; import java.io.IOException; +import java.net.Proxy; +import java.util.Objects; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import okhttp3.ConnectionPool; import okhttp3.Interceptor; import okhttp3.OkHttpClient; @@ -24,94 +23,112 @@ public class ConnectionFactory { - private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE; + private static Boolean LATENCY_DEBUG_SWATCH = Boolean.FALSE; - private static LinkedBlockingQueue LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>(); + private static LinkedBlockingQueue LATENCY_DEBUG_QUEUE = new LinkedBlockingQueue<>(); - private static ConnectionPool connectionPool = - new ConnectionPool(20, 300, TimeUnit.SECONDS); + private static ConnectionPool connectionPool = + new ConnectionPool(20, 300, TimeUnit.SECONDS); - private static final OkHttpClient client = new OkHttpClient.Builder() - .followSslRedirects(false) - .followRedirects(false) - .connectTimeout(5000, TimeUnit.MILLISECONDS) - .readTimeout(5000, TimeUnit.MILLISECONDS) - .writeTimeout(5000, TimeUnit.MILLISECONDS) - .connectionPool(connectionPool) - .addNetworkInterceptor(new Interceptor() { - @NotNull - @Override - public Response intercept(@NotNull Chain chain) throws IOException { - Request request = chain.request(); - - Long startNano = System.nanoTime(); - - Response response = chain.proceed(request); + private ConnectionFactory(){} + public static void initFactory(Proxy proxy) { + init(proxy); + } - Long endNano = System.nanoTime(); - if (LATENCY_DEBUG_SWATCH) { - LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano)); - } + private static OkHttpClient client; + @Getter + public static OkHttpClient.Builder builder; +// static { +// } - return response; + private static void init(Proxy proxy) { + builder = new OkHttpClient.Builder(); + if (!Objects.isNull(proxy)) { + builder.proxy(proxy); } - }) - .build(); - - private static final Logger log = LoggerFactory.getLogger(ConnectionFactory.class); - - public static String execute(Request request) { - - Response response = null; - String str = null; - try { - log.debug("[Request URL]{}", request.url()); - response = client.newCall(request).execute(); - if (response.code() != 200) { - throw new SDKException(SDKException.EXEC_ERROR, "[Execute] Response Status Error : " + response.code() + " message:" + response.message()); - } - if (response != null && response.body() != null) { - str = response.body().string(); - response.close(); - } else { - throw new SDKException(SDKException.ENV_ERROR, "[Execute] Cannot get the response from server"); - } - log.debug("[Response]{}", str); - return str; - } catch (IOException e) { - e.printStackTrace(); - throw new SDKException(SDKException.RUNTIME_ERROR, "[Execute] Cannot get the response from server"); + client = builder + .followSslRedirects(false) + .followRedirects(false) + .connectTimeout(5000, TimeUnit.MILLISECONDS) + .readTimeout(5000, TimeUnit.MILLISECONDS) + .writeTimeout(5000, TimeUnit.MILLISECONDS) + .connectionPool(connectionPool) + .addNetworkInterceptor(new Interceptor() { + @NotNull + @Override + public Response intercept(@NotNull Chain chain) throws IOException { + Request request = chain.request(); + + Long startNano = System.nanoTime(); + + Response response = chain.proceed(request); + + Long endNano = System.nanoTime(); + + if (LATENCY_DEBUG_SWATCH) { + LATENCY_DEBUG_QUEUE.add(new NetworkLatency(request.url().url().getPath(), startNano, endNano)); + } + + return response; + } + }) + .build(); } - } + private static final Logger log = LoggerFactory.getLogger(ConnectionFactory.class); + + public static String execute(Request request) { + + Response response = null; + String str = null; + try { + log.debug("[Request URL]{}", request.url()); + response = client.newCall(request).execute(); + if (response.code() != 200) { + throw new SDKException(SDKException.EXEC_ERROR, "[Execute] Response Status Error : " + response.code() + " message:" + response.message()); + } + if (response != null && response.body() != null) { + str = response.body().string(); + response.close(); + } else { + throw new SDKException(SDKException.ENV_ERROR, "[Execute] Cannot get the response from server"); + } + log.debug("[Response]{}", str); + return str; + } catch (IOException e) { + e.printStackTrace(); + throw new SDKException(SDKException.RUNTIME_ERROR, "[Execute] Cannot get the response from server"); + } - public static WebSocket createWebSocket(Request request, WebSocketListener listener) { - return client.newWebSocket(request, listener); - } + } - public static void setLatencyDebug() { - LATENCY_DEBUG_SWATCH = Boolean.TRUE; - } + public static WebSocket createWebSocket(Request request, WebSocketListener listener) { + return client.newWebSocket(request, listener); + } - public static LinkedBlockingQueue getLatencyDebugQueue() { - return LATENCY_DEBUG_QUEUE; - } + public static void setLatencyDebug() { + LATENCY_DEBUG_SWATCH = Boolean.TRUE; + } - public static void clearLatencyDebugQueue() { - LATENCY_DEBUG_QUEUE.clear(); - } + public static LinkedBlockingQueue getLatencyDebugQueue() { + return LATENCY_DEBUG_QUEUE; + } - @Data - @Builder - @AllArgsConstructor - @NoArgsConstructor - public static class NetworkLatency { + public static void clearLatencyDebugQueue() { + LATENCY_DEBUG_QUEUE.clear(); + } - private String path; + @Data + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class NetworkLatency { - private Long startNanoTime; + private String path; - private Long endNanoTime; - } + private Long startNanoTime; + + private Long endNanoTime; + } } diff --git a/src/test/java/com/huobi/client/examples/SubscribeTradeEvent.java b/src/test/java/com/huobi/client/examples/SubscribeTradeEvent.java new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/com/huobi/client/impl/MockWebsocketConnection.java b/src/test/java/com/huobi/client/impl/MockWebsocketConnection.java new file mode 100644 index 00000000..e69de29b diff --git a/src/test/java/com/huobi/examples/AccountClientExample.java b/src/test/java/com/huobi/examples/AccountClientExample.java index e93a8822..756cefab 100644 --- a/src/test/java/com/huobi/examples/AccountClientExample.java +++ b/src/test/java/com/huobi/examples/AccountClientExample.java @@ -1,6 +1,8 @@ package com.huobi.examples; import java.math.BigDecimal; +import java.net.InetSocketAddress; +import java.net.Proxy; import java.util.List; import com.huobi.Constants; @@ -34,87 +36,90 @@ public class AccountClientExample { - public static void main(String[] args) { - - Long accountId = 123L; - AccountClient accountService = AccountClient.create(HuobiOptions.builder() - .apiKey(Constants.API_KEY) - .secretKey(Constants.SECRET_KEY) - .build()); - - List accountList = accountService.getAccounts(); - accountList.forEach(account -> { - System.out.println(account.toString()); - }); - - - AccountBalance accountBalance = accountService.getAccountBalance(AccountBalanceRequest.builder() - .accountId(accountId) - .build()); - - System.out.println(accountBalance.getId()); - System.out.println(accountBalance.getType()); - System.out.println(accountBalance.getState()); - accountBalance.getList().forEach(balance -> { - System.out.println(balance.toString()); - }); - - List historyList = accountService.getAccountHistory(AccountHistoryRequest.builder().accountId(accountId).build()); - historyList.forEach(history->{ - System.out.println(history); - }); - - AccountLedgerResult accountLedgerResult = accountService.getAccountLedger(AccountLedgerRequest.builder() - .accountId(accountId) - .limit(2) - .build()); - System.out.println("leger nextId: " + accountLedgerResult.getNextId()); - accountLedgerResult.getLedgerList().forEach(ledger -> { - System.out.println(ledger); - }); - - - accountService.subAccountsUpdate(SubAccountUpdateRequest.builder() - .accountUpdateMode(AccountUpdateModeEnum.ACCOUNT_CHANGE).build(), event -> { - System.out.println(event.toString()); - }); - - - AccountTransferResult accountTransferResult = accountService.accountTransfer(AccountTransferRequest.builder() - .fromUser(123L) - .fromAccount(456L) - .fromAccountType(AccountTransferAccountTypeEnum.SPOT) - .toUser(678L) - .toAccount(789L) - .toAccountType(AccountTransferAccountTypeEnum.MARGIN) - .currency("usdt") - .amount(new BigDecimal("10")) - .build()); - - System.out.println("account transfer result:"+accountTransferResult.toString()); - - AccountFuturesTransferResult accountFuturesTransferResult = accountService.accountFuturesTransfer(AccountFuturesTransferRequest.builder() - .currency("xrp") - .amount(new BigDecimal("5")) - .type(AccountFuturesTransferTypeEnum.PRO_TO_FUTURES) - .build()); - - System.out.println("account futures result:"+accountFuturesTransferResult.toString()); - - Point point = accountService.getPoint(PointRequest.builder().build()); - System.out.println("get point: " + point); - - - PointTransferResult pointTransferResult = accountService.pointTransfer(PointTransferRequest.builder() - .fromUid(123L) - .toUid(123L) - .groupId(123L) - .amount(BigDecimal.ONE) - .build()); - System.out.println(pointTransferResult); - - AccountAssetValuationResult accountAssetValuationResult = accountService.accountAssetValuation(AccountAssetValuationRequest.builder().accountType(AccountTypeEnum.SPOT).build()); - System.out.println(accountAssetValuationResult); - } + public static void main(String[] args) { + + Long accountId = 123L; + Proxy proxyTest = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3189)); + + AccountClient accountService = AccountClient.create(HuobiOptions.builder() + .apiKey(Constants.API_KEY) + .secretKey(Constants.SECRET_KEY) + .proxy(proxyTest) + .build()); + + List accountList = accountService.getAccounts(); + accountList.forEach(account -> { + System.out.println(account.toString()); + }); + + + AccountBalance accountBalance = accountService.getAccountBalance(AccountBalanceRequest.builder() + .accountId(accountId) + .build()); + + System.out.println(accountBalance.getId()); + System.out.println(accountBalance.getType()); + System.out.println(accountBalance.getState()); + accountBalance.getList().forEach(balance -> { + System.out.println(balance.toString()); + }); + + List historyList = accountService.getAccountHistory(AccountHistoryRequest.builder().accountId(accountId).build()); + historyList.forEach(history -> { + System.out.println(history); + }); + + AccountLedgerResult accountLedgerResult = accountService.getAccountLedger(AccountLedgerRequest.builder() + .accountId(accountId) + .limit(2) + .build()); + System.out.println("leger nextId: " + accountLedgerResult.getNextId()); + accountLedgerResult.getLedgerList().forEach(ledger -> { + System.out.println(ledger); + }); + + + accountService.subAccountsUpdate(SubAccountUpdateRequest.builder() + .accountUpdateMode(AccountUpdateModeEnum.ACCOUNT_CHANGE).build(), event -> { + System.out.println(event.toString()); + }); + + + AccountTransferResult accountTransferResult = accountService.accountTransfer(AccountTransferRequest.builder() + .fromUser(123L) + .fromAccount(456L) + .fromAccountType(AccountTransferAccountTypeEnum.SPOT) + .toUser(678L) + .toAccount(789L) + .toAccountType(AccountTransferAccountTypeEnum.MARGIN) + .currency("usdt") + .amount(new BigDecimal("10")) + .build()); + + System.out.println("account transfer result:" + accountTransferResult.toString()); + + AccountFuturesTransferResult accountFuturesTransferResult = accountService.accountFuturesTransfer(AccountFuturesTransferRequest.builder() + .currency("xrp") + .amount(new BigDecimal("5")) + .type(AccountFuturesTransferTypeEnum.PRO_TO_FUTURES) + .build()); + + System.out.println("account futures result:" + accountFuturesTransferResult.toString()); + + Point point = accountService.getPoint(PointRequest.builder().build()); + System.out.println("get point: " + point); + + + PointTransferResult pointTransferResult = accountService.pointTransfer(PointTransferRequest.builder() + .fromUid(123L) + .toUid(123L) + .groupId(123L) + .amount(BigDecimal.ONE) + .build()); + System.out.println(pointTransferResult); + + AccountAssetValuationResult accountAssetValuationResult = accountService.accountAssetValuation(AccountAssetValuationRequest.builder().accountType(AccountTypeEnum.SPOT).build()); + System.out.println(accountAssetValuationResult); + } }