From 2b6eaf10c49564c0a1a1732ce64e60c8c46c5b54 Mon Sep 17 00:00:00 2001 From: Jose Miguel Resendiz Date: Sat, 2 May 2020 13:12:03 -0500 Subject: [PATCH] Add WebSocket BookTickerEvent --- .../api/client/BinanceApiWebSocketClient.java | 15 ++- .../client/domain/event/BookTickerEvent.java | 113 ++++++++++++++++++ .../impl/BinanceApiWebSocketClientImpl.java | 15 ++- 3 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/binance/api/client/domain/event/BookTickerEvent.java diff --git a/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java b/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java index 1e8421e79..cf8a34fde 100644 --- a/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java +++ b/src/main/java/com/binance/api/client/BinanceApiWebSocketClient.java @@ -1,10 +1,6 @@ package com.binance.api.client; -import com.binance.api.client.domain.event.AggTradeEvent; -import com.binance.api.client.domain.event.AllMarketTickersEvent; -import com.binance.api.client.domain.event.CandlestickEvent; -import com.binance.api.client.domain.event.DepthEvent; -import com.binance.api.client.domain.event.UserDataUpdateEvent; +import com.binance.api.client.domain.event.*; import com.binance.api.client.domain.market.CandlestickInterval; import java.io.Closeable; @@ -60,6 +56,15 @@ public interface BinanceApiWebSocketClient extends Closeable { */ Closeable onAllMarketTickersEvent(BinanceApiCallback> callback); + /** + * Open a new web socket to receive {@link BookTickerEvent bookTickerEvents} on a callback. + * + * @param symbols market (one or coma-separated) symbol(s) to subscribe to + * @param callback the callback to call on new events + * @return a {@link Closeable} that allows the underlying web socket to be closed. + */ + Closeable onBookTickerEvent(String symbols, BinanceApiCallback callback); + /** * @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket. */ diff --git a/src/main/java/com/binance/api/client/domain/event/BookTickerEvent.java b/src/main/java/com/binance/api/client/domain/event/BookTickerEvent.java new file mode 100644 index 000000000..326d29e9a --- /dev/null +++ b/src/main/java/com/binance/api/client/domain/event/BookTickerEvent.java @@ -0,0 +1,113 @@ +package com.binance.api.client.domain.event; + +import org.apache.commons.lang3.builder.ToStringBuilder; + +import com.binance.api.client.constant.BinanceApiConstants; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonProperty; + +/** + * BookTickerEvent event for a symbol. Pushes any update to the best bid or + * ask's price or quantity in real-time for a specified symbol. + */ +@JsonIgnoreProperties(ignoreUnknown = true) +public class BookTickerEvent { + + @JsonProperty("u") + private long updateId; + + @JsonProperty("s") + private String symbol; + + @JsonProperty("b") + private String bidPrice; + + @JsonProperty("B") + private String bidQuantity; + + @JsonProperty("a") + private String askPrice; + + @JsonProperty("A") + private String askQuantity; + + public BookTickerEvent() { + super(); + } + + public BookTickerEvent(long updateId, String symbol, String bidPrice, String bidQuantity, String askPrice, + String askQuantity) { + super(); + this.updateId = updateId; + this.symbol = symbol; + this.bidPrice = bidPrice; + this.bidQuantity = bidQuantity; + this.askPrice = askPrice; + this.askQuantity = askQuantity; + } + + public BookTickerEvent(String symbol, String bidPrice, String bidQuantity, String askPrice, String askQuantity) { + super(); + this.symbol = symbol; + this.bidPrice = bidPrice; + this.bidQuantity = bidQuantity; + this.askPrice = askPrice; + this.askQuantity = askQuantity; + } + + public long getUpdateId() { + return updateId; + } + + public void setUpdateId(long updateId) { + this.updateId = updateId; + } + + public String getSymbol() { + return symbol; + } + + public void setSymbol(String symbol) { + this.symbol = symbol; + } + + public String getBidPrice() { + return bidPrice; + } + + public void setBidPrice(String bidPrice) { + this.bidPrice = bidPrice; + } + + public String getBidQuantity() { + return bidQuantity; + } + + public void setBidQuantity(String bidQuantity) { + this.bidQuantity = bidQuantity; + } + + public String getAskPrice() { + return askPrice; + } + + public void setAskPrice(String askPrice) { + this.askPrice = askPrice; + } + + public String getAskQuantity() { + return askQuantity; + } + + public void setAskQuantity(String askQuantity) { + this.askQuantity = askQuantity; + } + + @Override + public String toString() { + return new ToStringBuilder(this, BinanceApiConstants.TO_STRING_BUILDER_STYLE).append("eventType", "BookTicker") + .append("updateId", updateId).append("symbol", symbol).append("bidPrice", bidPrice) + .append("bidQuantity", bidQuantity).append("askPrice", askPrice).append("askQuantity", askQuantity) + .toString(); + } +} \ No newline at end of file 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 e9be1f690..1ca78716e 100644 --- a/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java +++ b/src/main/java/com/binance/api/client/impl/BinanceApiWebSocketClientImpl.java @@ -4,11 +4,7 @@ import com.binance.api.client.BinanceApiWebSocketClient; import com.binance.api.client.config.BinanceApiConfig; import com.binance.api.client.constant.BinanceApiConstants; -import com.binance.api.client.domain.event.AggTradeEvent; -import com.binance.api.client.domain.event.AllMarketTickersEvent; -import com.binance.api.client.domain.event.CandlestickEvent; -import com.binance.api.client.domain.event.DepthEvent; -import com.binance.api.client.domain.event.UserDataUpdateEvent; +import com.binance.api.client.domain.event.*; import com.binance.api.client.domain.market.CandlestickInterval; import com.fasterxml.jackson.core.type.TypeReference; @@ -67,6 +63,15 @@ public Closeable onAllMarketTickersEvent(BinanceApiCallback(callback, new TypeReference>() {})); } + @Override + public Closeable onBookTickerEvent(String symbols, BinanceApiCallback callback) { + final String channel = Arrays.stream(symbols.split(",")) + .map(String::trim) + .map(s -> String.format("%s@bookTicker", s)) + .collect(Collectors.joining("/")); + return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, BookTickerEvent.class)); + } + /** * @deprecated This method is no longer functional. Please use the returned {@link Closeable} from any of the other methods to close the web socket. */