Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

better configuration options for API base URLs #338

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.binance.api</groupId>
<artifactId>binance-api-client</artifactId>
<version>1.0.1</version>
<version>1.0.4</version>
<licenses>
<license>
<name>The MIT License</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.binance.api.client;

public interface BinanceApiWebSocketCallback<T> {
void onOpen();

void onMessage(T response);

void onClosing(int code, String reason);

void onClosed(int code, String reason);

default void onFailure(Throwable cause) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public interface BinanceApiWebSocketClient extends Closeable {
*/
Closeable onUserDataUpdateEvent(String listenKey, BinanceApiCallback<UserDataUpdateEvent> callback);

/**
Closeable onUserDataUpdateEvent(String listenKey, BinanceApiWebSocketCallback<UserDataUpdateEvent> 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
Expand Down
43 changes: 18 additions & 25 deletions src/main/java/com/binance/api/client/config/BinanceApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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.*;
Expand All @@ -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
Expand Down Expand Up @@ -52,10 +55,16 @@ public Closeable onAggTradeEvent(String symbols, BinanceApiCallback<AggTradeEven
return createNewWebSocket(channel, new BinanceApiWebSocketListener<>(callback, AggTradeEvent.class));
}

@Override
public Closeable onUserDataUpdateEvent(String listenKey, BinanceApiCallback<UserDataUpdateEvent> callback) {
return createNewWebSocket(listenKey, new BinanceApiWebSocketListener<>(callback, UserDataUpdateEvent.class));
}

@Override
public Closeable onUserDataUpdateEvent(String listenKey, BinanceApiWebSocketCallback<UserDataUpdateEvent> callback) {
return createNewWebSocket(listenKey, new BinanceApiWebSocketListener<>(callback, UserDataUpdateEvent.class));
}

@Override
public Closeable onTickerEvent(String symbols, BinanceApiCallback<TickerEvent> callback) {
final String channel = Arrays.stream(symbols.split(","))
Expand Down Expand Up @@ -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 () -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,7 +17,8 @@
*/
public class BinanceApiWebSocketListener<T> extends WebSocketListener {

private BinanceApiCallback<T> callback;
private final BinanceApiCallback<T> callback;
private final BinanceApiWebSocketCallback<T> webSocketCallback;

private static final ObjectMapper mapper = new ObjectMapper();

Expand All @@ -26,19 +28,39 @@ public class BinanceApiWebSocketListener<T> extends WebSocketListener {

public BinanceApiWebSocketListener(BinanceApiCallback<T> callback, Class<T> eventClass) {
this.callback = callback;
this.webSocketCallback = null;
this.objectReader = mapper.readerFor(eventClass);
}

public BinanceApiWebSocketListener(BinanceApiCallback<T> callback, TypeReference<T> eventTypeReference) {
this.callback = callback;
this.webSocketCallback = null;
this.objectReader = mapper.readerFor(eventTypeReference);
}

public BinanceApiWebSocketListener(BinanceApiWebSocketCallback<T> callback, Class<T> 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);
}
Expand All @@ -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);
}
}
}
}