Skip to content
This repository was archived by the owner on Oct 30, 2023. It is now read-only.
Merged
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.binance.api.client;

import com.binance.api.client.impl.*;

import com.binance.api.client.config.BinanceApiConfig;
import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;

/**
Expand All @@ -28,6 +28,23 @@ public class BinanceApiClientFactory {
private BinanceApiClientFactory(String apiKey, String secret) {
this.apiKey = apiKey;
this.secret = secret;
BinanceApiConfig.useTestnet = false;
BinanceApiConfig.useTestnetStreaming = false;
}

/**
* Instantiates a new binance api client factory.
*
* @param apiKey the API key
* @param secret the Secret
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
*/
private BinanceApiClientFactory(String apiKey, String secret, boolean useTestnet, boolean useTestnetStreaming) {
this(apiKey, secret);
if (useTestnet) {
BinanceApiConfig.useTestnet = true;
BinanceApiConfig.useTestnetStreaming = useTestnetStreaming; }
}

/**
Expand All @@ -42,6 +59,20 @@ public static BinanceApiClientFactory newInstance(String apiKey, String secret)
return new BinanceApiClientFactory(apiKey, secret);
}

/**
* New instance with optional Spot Test Network endpoint.
*
* @param apiKey the API key
* @param secret the Secret
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
*
* @return the binance api client factory.
*/
public static BinanceApiClientFactory newInstance(String apiKey, String secret, boolean useTestnet, boolean useTestnetStreaming) {
return new BinanceApiClientFactory(apiKey, secret, useTestnet, useTestnetStreaming);
}

/**
* New instance without authentication.
*
Expand All @@ -51,6 +82,18 @@ public static BinanceApiClientFactory newInstance() {
return new BinanceApiClientFactory(null, null);
}

/**
* New instance without authentication and with optional Spot Test Network endpoint.
*
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
*
* @return the binance api client factory.
*/
public static BinanceApiClientFactory newInstance(boolean useTestnet, boolean useTestnetStreaming) {
return new BinanceApiClientFactory(null, null, useTestnet, useTestnetStreaming);
}

/**
* Creates a new synchronous/blocking REST client.
*/
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/binance/api/client/config/BinanceApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ public class BinanceApiConfig {
*/
private static String BASE_DOMAIN = "binance.com";

/**
* Spot Test Network URL.
*/
private static final String TESTNET_DOMAIN = "testnet.binance.vision";

/**
* Binance Spot Test Network option:
* true if endpoint is spot test network URL; false if endpoint is production spot API URL.
*/
public static boolean useTestnet;

/**
* Binance Spot Test Network option:
* true for websocket streaming; false for no streaming.
*/
public static boolean useTestnetStreaming;

/**
* Set the URL base domain name (e.g., binance.com).
*
Expand Down Expand Up @@ -49,4 +66,17 @@ public static String getAssetInfoApiBaseUrl() {
return String.format("https://%s/", getBaseDomain());
}

/**
* Spot Test Network API base URL.
*/
public static String getTestNetBaseUrl() {
return String.format("https://%s", TESTNET_DOMAIN);
}

/**
* Streaming Spot Test Network base URL.
*/
public static String getStreamTestNetBaseUrl() {
return String.format("wss://%s", TESTNET_DOMAIN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,26 @@ public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}

/**
* Create a Binance API service.
*
* @param serviceClass the type of service.
* @param apiKey Binance API key.
* @param secret Binance secret.
*
* @return a new implementation of the API endpoints for the Binance API service.
*/
public static <S> S createService(Class<S> serviceClass, String apiKey, String secret) {
String baseUrl = null;
if (!BinanceApiConfig.useTestnet) { baseUrl = BinanceApiConfig.getApiBaseUrl(); }
else {
baseUrl = BinanceApiConfig.useTestnetStreaming ?
BinanceApiConfig.getStreamTestNetBaseUrl() :
BinanceApiConfig.getTestNetBaseUrl();
}

Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.baseUrl(BinanceApiConfig.getApiBaseUrl())
.baseUrl(baseUrl)
.addConverterFactory(converterFactory);

if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(secret)) {
Expand Down