This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
forked from joaopsilva/binance-java-api
-
Notifications
You must be signed in to change notification settings - Fork 622
/
BinanceApiServiceGenerator.java
executable file
·96 lines (83 loc) · 3.48 KB
/
BinanceApiServiceGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.binance.api.client.impl;
import com.binance.api.client.BinanceApiError;
import com.binance.api.client.config.BinanceApiConfig;
import com.binance.api.client.exception.BinanceApiException;
import com.binance.api.client.security.AuthenticationInterceptor;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import retrofit2.Call;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.util.concurrent.TimeUnit;
/**
* Generates a Binance API implementation based on @see {@link BinanceApiService}.
*/
public class BinanceApiServiceGenerator {
private static final OkHttpClient sharedClient;
private static final Converter.Factory converterFactory = JacksonConverterFactory.create();
static {
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost(500);
dispatcher.setMaxRequests(500);
sharedClient = new OkHttpClient.Builder()
.dispatcher(dispatcher)
.pingInterval(20, TimeUnit.SECONDS)
.build();
}
@SuppressWarnings("unchecked")
private static final Converter<ResponseBody, BinanceApiError> errorBodyConverter =
(Converter<ResponseBody, BinanceApiError>)converterFactory.responseBodyConverter(
BinanceApiError.class, new Annotation[0], null);
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null, null);
}
public static <S> S createService(Class<S> serviceClass, String apiKey, String secret) {
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
.baseUrl(BinanceApiConfig.getApiBaseUrl())
.addConverterFactory(converterFactory);
if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(secret)) {
retrofitBuilder.client(sharedClient);
} else {
// `adaptedClient` will use its own interceptor, but share thread pool etc with the 'parent' client
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(apiKey, secret);
OkHttpClient adaptedClient = sharedClient.newBuilder().addInterceptor(interceptor).build();
retrofitBuilder.client(adaptedClient);
}
Retrofit retrofit = retrofitBuilder.build();
return retrofit.create(serviceClass);
}
/**
* Execute a REST call and block until the response is received.
*/
public static <T> T executeSync(Call<T> call) {
try {
Response<T> response = call.execute();
if (response.isSuccessful()) {
return response.body();
} else {
BinanceApiError apiError = getBinanceApiError(response);
throw new BinanceApiException(apiError);
}
} catch (IOException e) {
throw new BinanceApiException(e);
}
}
/**
* Extracts and converts the response error body into an object.
*/
public static BinanceApiError getBinanceApiError(Response<?> response) throws IOException, BinanceApiException {
return errorBodyConverter.convert(response.errorBody());
}
/**
* Returns the shared OkHttpClient instance.
*/
public static OkHttpClient getSharedClient() {
return sharedClient;
}
}