Skip to content

Commit 239f15c

Browse files
authored
Merge pull request binance-exchange#347 from georgesoler/master
Add connectivity to Binance Exchange Spot Test Network endpoints.
2 parents daeab32 + c9d52af commit 239f15c

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

src/main/java/com/binance/api/client/BinanceApiClientFactory.java

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.binance.api.client;
22

33
import com.binance.api.client.impl.*;
4-
4+
import com.binance.api.client.config.BinanceApiConfig;
55
import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;
66

77
/**
@@ -28,6 +28,23 @@ public class BinanceApiClientFactory {
2828
private BinanceApiClientFactory(String apiKey, String secret) {
2929
this.apiKey = apiKey;
3030
this.secret = secret;
31+
BinanceApiConfig.useTestnet = false;
32+
BinanceApiConfig.useTestnetStreaming = false;
33+
}
34+
35+
/**
36+
* Instantiates a new binance api client factory.
37+
*
38+
* @param apiKey the API key
39+
* @param secret the Secret
40+
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
41+
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
42+
*/
43+
private BinanceApiClientFactory(String apiKey, String secret, boolean useTestnet, boolean useTestnetStreaming) {
44+
this(apiKey, secret);
45+
if (useTestnet) {
46+
BinanceApiConfig.useTestnet = true;
47+
BinanceApiConfig.useTestnetStreaming = useTestnetStreaming; }
3148
}
3249

3350
/**
@@ -42,6 +59,20 @@ public static BinanceApiClientFactory newInstance(String apiKey, String secret)
4259
return new BinanceApiClientFactory(apiKey, secret);
4360
}
4461

62+
/**
63+
* New instance with optional Spot Test Network endpoint.
64+
*
65+
* @param apiKey the API key
66+
* @param secret the Secret
67+
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
68+
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
69+
*
70+
* @return the binance api client factory.
71+
*/
72+
public static BinanceApiClientFactory newInstance(String apiKey, String secret, boolean useTestnet, boolean useTestnetStreaming) {
73+
return new BinanceApiClientFactory(apiKey, secret, useTestnet, useTestnetStreaming);
74+
}
75+
4576
/**
4677
* New instance without authentication.
4778
*
@@ -51,6 +82,18 @@ public static BinanceApiClientFactory newInstance() {
5182
return new BinanceApiClientFactory(null, null);
5283
}
5384

85+
/**
86+
* New instance without authentication and with optional Spot Test Network endpoint.
87+
*
88+
* @param useTestnet true if endpoint is spot test network URL; false if endpoint is production spot API URL.
89+
* @param useTestnetStreaming true for spot test network websocket streaming; false for no streaming.
90+
*
91+
* @return the binance api client factory.
92+
*/
93+
public static BinanceApiClientFactory newInstance(boolean useTestnet, boolean useTestnetStreaming) {
94+
return new BinanceApiClientFactory(null, null, useTestnet, useTestnetStreaming);
95+
}
96+
5497
/**
5598
* Creates a new synchronous/blocking REST client.
5699
*/

src/main/java/com/binance/api/client/config/BinanceApiConfig.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ public class BinanceApiConfig {
1010
*/
1111
private static String BASE_DOMAIN = "binance.com";
1212

13+
/**
14+
* Spot Test Network URL.
15+
*/
16+
private static final String TESTNET_DOMAIN = "testnet.binance.vision";
17+
18+
/**
19+
* Binance Spot Test Network option:
20+
* true if endpoint is spot test network URL; false if endpoint is production spot API URL.
21+
*/
22+
public static boolean useTestnet;
23+
24+
/**
25+
* Binance Spot Test Network option:
26+
* true for websocket streaming; false for no streaming.
27+
*/
28+
public static boolean useTestnetStreaming;
29+
1330
/**
1431
* Set the URL base domain name (e.g., binance.com).
1532
*
@@ -49,4 +66,17 @@ public static String getAssetInfoApiBaseUrl() {
4966
return String.format("https://%s/", getBaseDomain());
5067
}
5168

69+
/**
70+
* Spot Test Network API base URL.
71+
*/
72+
public static String getTestNetBaseUrl() {
73+
return String.format("https://%s", TESTNET_DOMAIN);
74+
}
75+
76+
/**
77+
* Streaming Spot Test Network base URL.
78+
*/
79+
public static String getStreamTestNetBaseUrl() {
80+
return String.format("wss://%s", TESTNET_DOMAIN);
81+
}
5282
}

src/main/java/com/binance/api/client/impl/BinanceApiServiceGenerator.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,26 @@ public static <S> S createService(Class<S> serviceClass) {
4545
return createService(serviceClass, null, null);
4646
}
4747

48+
/**
49+
* Create a Binance API service.
50+
*
51+
* @param serviceClass the type of service.
52+
* @param apiKey Binance API key.
53+
* @param secret Binance secret.
54+
*
55+
* @return a new implementation of the API endpoints for the Binance API service.
56+
*/
4857
public static <S> S createService(Class<S> serviceClass, String apiKey, String secret) {
58+
String baseUrl = null;
59+
if (!BinanceApiConfig.useTestnet) { baseUrl = BinanceApiConfig.getApiBaseUrl(); }
60+
else {
61+
baseUrl = BinanceApiConfig.useTestnetStreaming ?
62+
BinanceApiConfig.getStreamTestNetBaseUrl() :
63+
BinanceApiConfig.getTestNetBaseUrl();
64+
}
65+
4966
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
50-
.baseUrl(BinanceApiConfig.getApiBaseUrl())
67+
.baseUrl(baseUrl)
5168
.addConverterFactory(converterFactory);
5269

5370
if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(secret)) {

0 commit comments

Comments
 (0)