From 1671f667467e634be80029873e57749f6503b054 Mon Sep 17 00:00:00 2001
From: Peter Chovanec
Date: Thu, 25 Mar 2021 22:52:15 +0100
Subject: [PATCH 1/4] better configuration options for API base URLs
---
.../api/client/config/BinanceApiConfig.java | 43 ++++++++-----------
1 file changed, 18 insertions(+), 25 deletions(-)
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;
+ }
}
From febfea4a39241d94654bda616f60839dbdae4549 Mon Sep 17 00:00:00 2001
From: Peter Chovanec
Date: Sun, 23 May 2021 03:46:35 +0200
Subject: [PATCH 2/4] new version 1.0.2
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 863f5c234..89ebaf66d 100755
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.binance.api
binance-api-client
- 1.0.1
+ 1.0.2
The MIT License
From c3f163230cfc1cc1bea370e322e895f2eb9ecfb6 Mon Sep 17 00:00:00 2001
From: Peter Chovanec
Date: Sun, 23 May 2021 03:49:44 +0200
Subject: [PATCH 3/4] enhanced callback for websocket, new version 1.0.3
---
pom.xml | 2 +-
.../client/BinanceApiWebSocketCallback.java | 14 ++++++
.../api/client/BinanceApiWebSocketClient.java | 4 +-
.../impl/BinanceApiWebSocketClientImpl.java | 7 +++
.../impl/BinanceApiWebSocketListener.java | 43 +++++++++++++++++--
5 files changed, 64 insertions(+), 6 deletions(-)
create mode 100755 src/main/java/com/binance/api/client/BinanceApiWebSocketCallback.java
diff --git a/pom.xml b/pom.xml
index 89ebaf66d..1dce0d906 100755
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.binance.api
binance-api-client
- 1.0.2
+ 1.0.3
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/impl/BinanceApiWebSocketClientImpl.java b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java
index be0dcf518..8daf0103c 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.*;
@@ -52,10 +53,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(","))
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
+}
From 751b815020061a2515cde8b8f0fd8f39151fd726 Mon Sep 17 00:00:00 2001
From: Peter Chovanec
Date: Tue, 25 May 2021 01:06:59 +0200
Subject: [PATCH 4/4] fixed initialization of streaming api url, new version
1.0.4
---
pom.xml | 2 +-
.../api/client/impl/BinanceApiWebSocketClientImpl.java | 4 +++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/pom.xml b/pom.xml
index 1dce0d906..e8d5ac588 100755
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
com.binance.api
binance-api-client
- 1.0.3
+ 1.0.4
The MIT License
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 8daf0103c..4e5f4a4f8 100755
--- a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java
+++ b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java
@@ -22,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
@@ -100,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 () -> {