diff --git a/pom.xml b/pom.xml index 863f5c234..e8d5ac588 100755 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.binance.api binance-api-client - 1.0.1 + 1.0.4 The MIT License diff --git a/src/main/java/com/binance/api/client/BinanceApiWebSocketCallback.java b/src/main/java/com/binance/api/client/BinanceApiWebSocketCallback.java new file mode 100755 index 000000000..a19ac27cb --- /dev/null +++ b/src/main/java/com/binance/api/client/BinanceApiWebSocketCallback.java @@ -0,0 +1,14 @@ +package com.binance.api.client; + +public interface BinanceApiWebSocketCallback { + void onOpen(); + + void onMessage(T response); + + void onClosing(int code, String reason); + + void onClosed(int code, String reason); + + default void onFailure(Throwable cause) { + } +} diff --git a/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java b/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java index bc048d464..c456e2500 100755 --- a/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java +++ b/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java @@ -48,7 +48,9 @@ public interface BinanceApiWebSocketClient extends Closeable { */ Closeable onUserDataUpdateEvent(String listenKey, BinanceApiCallback callback); - /** + Closeable onUserDataUpdateEvent(String listenKey, BinanceApiWebSocketCallback callback); + + /** * Open a new web socket to receive {@link TickerEvent tickerEvents} on a callback. * * @param symbols market (one or coma-separated) symbol(s) to subscribe to diff --git a/src/main/java/com/binance/api/client/config/BinanceApiConfig.java b/src/main/java/com/binance/api/client/config/BinanceApiConfig.java index d05538efb..c1ab4e884 100755 --- a/src/main/java/com/binance/api/client/config/BinanceApiConfig.java +++ b/src/main/java/com/binance/api/client/config/BinanceApiConfig.java @@ -2,51 +2,44 @@ /** * Configuration used for Binance operations. + * For testnet check https://testnet.binance.vision */ public class BinanceApiConfig { - /** - * Base domain for URLs. - */ - private static String BASE_DOMAIN = "binance.com"; - - /** - * Set the URL base domain name (e.g., binance.com). - * - * @param baseDomain Base domain name - */ - public static void setBaseDomain(final String baseDomain) { - BASE_DOMAIN = baseDomain; - } - - /** - * Get the URL base domain name (e.g., binance.com). - * - * @return The base domain for URLs - */ - public static String getBaseDomain() { - return BASE_DOMAIN; - } + private static String REST_API_BASE_URL = "https://api.binance.com"; + private static String STREAM_API_BASE_URL = "wss://stream.binance.com:9443/ws"; + private static String ASSET_INFO_API_BASE_URL = "https://binance.com/"; /** * REST API base URL. */ public static String getApiBaseUrl() { - return String.format("https://api.%s", getBaseDomain()); + return REST_API_BASE_URL; + } + + public static void setRestApiBaseUrl(String restApiBaseUrl) { + REST_API_BASE_URL = restApiBaseUrl; } /** * Streaming API base URL. */ public static String getStreamApiBaseUrl() { - return String.format("wss://stream.%s:9443/ws", getBaseDomain()); + return STREAM_API_BASE_URL; + } + + public static void setStreamApiBaseUrl(String streamApiBaseUrl) { + STREAM_API_BASE_URL = streamApiBaseUrl; } /** * Asset info base URL. */ public static String getAssetInfoApiBaseUrl() { - return String.format("https://%s/", getBaseDomain()); + return ASSET_INFO_API_BASE_URL; } + public static void setAssetInfoApiBaseUrl(String assetInfoApiBaseUrl) { + ASSET_INFO_API_BASE_URL = assetInfoApiBaseUrl; + } } diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java index be0dcf518..4e5f4a4f8 100755 --- a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java @@ -1,6 +1,7 @@ package com.binance.api.client.impl; import com.binance.api.client.BinanceApiCallback; +import com.binance.api.client.BinanceApiWebSocketCallback; import com.binance.api.client.BinanceApiWebSocketClient; import com.binance.api.client.config.BinanceApiConfig; import com.binance.api.client.domain.event.*; @@ -21,9 +22,11 @@ public class BinanceApiWebSocketClientImpl implements BinanceApiWebSocketClient, Closeable { private final OkHttpClient client; + private final String streamApiBaseUrl; public BinanceApiWebSocketClientImpl(OkHttpClient client) { this.client = client; + this.streamApiBaseUrl = BinanceApiConfig.getStreamApiBaseUrl(); } @Override @@ -52,10 +55,16 @@ public Closeable onAggTradeEvent(String symbols, BinanceApiCallback(callback, AggTradeEvent.class)); } + @Override public Closeable onUserDataUpdateEvent(String listenKey, BinanceApiCallback callback) { return createNewWebSocket(listenKey, new BinanceApiWebSocketListener<>(callback, UserDataUpdateEvent.class)); } + @Override + public Closeable onUserDataUpdateEvent(String listenKey, BinanceApiWebSocketCallback callback) { + return createNewWebSocket(listenKey, new BinanceApiWebSocketListener<>(callback, UserDataUpdateEvent.class)); + } + @Override public Closeable onTickerEvent(String symbols, BinanceApiCallback callback) { final String channel = Arrays.stream(symbols.split(",")) @@ -93,7 +102,7 @@ public void close() { } private Closeable createNewWebSocket(String channel, BinanceApiWebSocketListener listener) { - String streamingUrl = String.format("%s/%s", BinanceApiConfig.getStreamApiBaseUrl(), channel); + String streamingUrl = String.format("%s/%s", streamApiBaseUrl, channel); Request request = new Request.Builder().url(streamingUrl).build(); final WebSocket webSocket = client.newWebSocket(request, listener); return () -> { diff --git a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketListener.java b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketListener.java index e2004090e..041dc9544 100755 --- a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketListener.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketListener.java @@ -1,6 +1,7 @@ package com.binance.api.client.impl; import com.binance.api.client.BinanceApiCallback; +import com.binance.api.client.BinanceApiWebSocketCallback; import com.binance.api.client.exception.BinanceApiException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; @@ -16,7 +17,8 @@ */ public class BinanceApiWebSocketListener extends WebSocketListener { - private BinanceApiCallback callback; + private final BinanceApiCallback callback; + private final BinanceApiWebSocketCallback webSocketCallback; private static final ObjectMapper mapper = new ObjectMapper(); @@ -26,19 +28,39 @@ public class BinanceApiWebSocketListener extends WebSocketListener { public BinanceApiWebSocketListener(BinanceApiCallback callback, Class eventClass) { this.callback = callback; + this.webSocketCallback = null; this.objectReader = mapper.readerFor(eventClass); } public BinanceApiWebSocketListener(BinanceApiCallback callback, TypeReference eventTypeReference) { this.callback = callback; + this.webSocketCallback = null; this.objectReader = mapper.readerFor(eventTypeReference); } + public BinanceApiWebSocketListener(BinanceApiWebSocketCallback callback, Class eventClass) { + this.callback = null; + this.webSocketCallback = callback; + this.objectReader = mapper.readerFor(eventClass); + } + + @Override + public void onOpen(WebSocket webSocket, Response response) { + if (webSocketCallback != null) { + webSocketCallback.onOpen(); + } + } + @Override public void onMessage(WebSocket webSocket, String text) { try { T event = objectReader.readValue(text); - callback.onResponse(event); + if (callback != null) { + callback.onResponse(event); + } + if (webSocketCallback != null) { + webSocketCallback.onMessage(event); + } } catch (IOException e) { throw new BinanceApiException(e); } @@ -47,12 +69,25 @@ public void onMessage(WebSocket webSocket, String text) { @Override public void onClosing(final WebSocket webSocket, final int code, final String reason) { closing = true; + if (webSocketCallback != null) { + webSocketCallback.onClosing(code, reason); + } + } + + @Override + public void onClosed(WebSocket webSocket, int code, String reason) { + if (webSocketCallback != null) { + webSocketCallback.onClosed(code, reason); + } } @Override public void onFailure(WebSocket webSocket, Throwable t, Response response) { - if (!closing) { + if (webSocketCallback != null) { + webSocketCallback.onFailure(t); + } + if (!closing && callback != null) { callback.onFailure(t); } } -} \ No newline at end of file +}