Skip to content

Commit 32cea2c

Browse files
author
george soler
committed
Add connectivity to Binance Exchange Spot Test Network endpoints.
1 parent 7781ee0 commit 32cea2c

File tree

3 files changed

+186
-96
lines changed

3 files changed

+186
-96
lines changed
Lines changed: 138 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,138 @@
1-
package com.binance.api.client;
2-
3-
import com.binance.api.client.impl.*;
4-
5-
import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;
6-
7-
/**
8-
* A factory for creating BinanceApi client objects.
9-
*/
10-
public class BinanceApiClientFactory {
11-
12-
/**
13-
* API Key
14-
*/
15-
private String apiKey;
16-
17-
/**
18-
* Secret.
19-
*/
20-
private String secret;
21-
22-
/**
23-
* Instantiates a new binance api client factory.
24-
*
25-
* @param apiKey the API key
26-
* @param secret the Secret
27-
*/
28-
private BinanceApiClientFactory(String apiKey, String secret) {
29-
this.apiKey = apiKey;
30-
this.secret = secret;
31-
}
32-
33-
/**
34-
* New instance.
35-
*
36-
* @param apiKey the API key
37-
* @param secret the Secret
38-
*
39-
* @return the binance api client factory
40-
*/
41-
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
42-
return new BinanceApiClientFactory(apiKey, secret);
43-
}
44-
45-
/**
46-
* New instance without authentication.
47-
*
48-
* @return the binance api client factory
49-
*/
50-
public static BinanceApiClientFactory newInstance() {
51-
return new BinanceApiClientFactory(null, null);
52-
}
53-
54-
/**
55-
* Creates a new synchronous/blocking REST client.
56-
*/
57-
public BinanceApiRestClient newRestClient() {
58-
return new BinanceApiRestClientImpl(apiKey, secret);
59-
}
60-
61-
/**
62-
* Creates a new asynchronous/non-blocking REST client.
63-
*/
64-
public BinanceApiAsyncRestClient newAsyncRestClient() {
65-
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
66-
}
67-
68-
/**
69-
* Creates a new asynchronous/non-blocking Margin REST client.
70-
*/
71-
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
72-
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
73-
}
74-
75-
/**
76-
* Creates a new synchronous/blocking Margin REST client.
77-
*/
78-
public BinanceApiMarginRestClient newMarginRestClient() {
79-
return new BinanceApiMarginRestClientImpl(apiKey, secret);
80-
}
81-
82-
/**
83-
* Creates a new web socket client used for handling data streams.
84-
*/
85-
public BinanceApiWebSocketClient newWebSocketClient() {
86-
return new BinanceApiWebSocketClientImpl(getSharedClient());
87-
}
88-
89-
/**
90-
* Creates a new synchronous/blocking Swap REST client.
91-
*/
92-
public BinanceApiSwapRestClient newSwapRestClient() {
93-
return new BinanceApiSwapRestClientImpl(apiKey, secret);
94-
}
95-
}
1+
package com.binance.api.client;
2+
3+
import com.binance.api.client.impl.*;
4+
import com.binance.api.client.config.BinanceApiConfig;
5+
import static com.binance.api.client.impl.BinanceApiServiceGenerator.getSharedClient;
6+
7+
/**
8+
* A factory for creating BinanceApi client objects.
9+
*/
10+
public class BinanceApiClientFactory {
11+
12+
/**
13+
* API Key
14+
*/
15+
private String apiKey;
16+
17+
/**
18+
* Secret.
19+
*/
20+
private String secret;
21+
22+
/**
23+
* Instantiates a new binance api client factory.
24+
*
25+
* @param apiKey the API key
26+
* @param secret the Secret
27+
*/
28+
private BinanceApiClientFactory(String apiKey, String secret) {
29+
this.apiKey = apiKey;
30+
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; }
48+
}
49+
50+
/**
51+
* New instance.
52+
*
53+
* @param apiKey the API key
54+
* @param secret the Secret
55+
*
56+
* @return the binance api client factory
57+
*/
58+
public static BinanceApiClientFactory newInstance(String apiKey, String secret) {
59+
return new BinanceApiClientFactory(apiKey, secret);
60+
}
61+
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+
76+
/**
77+
* New instance without authentication.
78+
*
79+
* @return the binance api client factory
80+
*/
81+
public static BinanceApiClientFactory newInstance() {
82+
return new BinanceApiClientFactory(null, null);
83+
}
84+
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+
97+
/**
98+
* Creates a new synchronous/blocking REST client.
99+
*/
100+
public BinanceApiRestClient newRestClient() {
101+
return new BinanceApiRestClientImpl(apiKey, secret);
102+
}
103+
104+
/**
105+
* Creates a new asynchronous/non-blocking REST client.
106+
*/
107+
public BinanceApiAsyncRestClient newAsyncRestClient() {
108+
return new BinanceApiAsyncRestClientImpl(apiKey, secret);
109+
}
110+
111+
/**
112+
* Creates a new asynchronous/non-blocking Margin REST client.
113+
*/
114+
public BinanceApiAsyncMarginRestClient newAsyncMarginRestClient() {
115+
return new BinanceApiAsyncMarginRestClientImpl(apiKey, secret);
116+
}
117+
118+
/**
119+
* Creates a new synchronous/blocking Margin REST client.
120+
*/
121+
public BinanceApiMarginRestClient newMarginRestClient() {
122+
return new BinanceApiMarginRestClientImpl(apiKey, secret);
123+
}
124+
125+
/**
126+
* Creates a new web socket client used for handling data streams.
127+
*/
128+
public BinanceApiWebSocketClient newWebSocketClient() {
129+
return new BinanceApiWebSocketClientImpl(getSharedClient());
130+
}
131+
132+
/**
133+
* Creates a new synchronous/blocking Swap REST client.
134+
*/
135+
public BinanceApiSwapRestClient newSwapRestClient() {
136+
return new BinanceApiSwapRestClientImpl(apiKey, secret);
137+
}
138+
}

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)