Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@

public class ApiServiceGenerator {

private static ApiGenerator generator;
private static final ApiGenerator generator;

static {
try {
generator = (ApiGenerator) Class.forName("com.binance.api.client.impl.BinanceApiServiceGenerator").newInstance();
/*
if (Boolean.getBoolean("binance.api.use.netty")) {
generator = (ApiGenerator) Class.forName("com.binance.api.client.impl.NettyBinanceApiServiceGenerator").newInstance();
} else {
generator = (ApiGenerator) Class.forName("com.binance.api.client.impl.BinanceApiServiceGenerator").newInstance();
}
*/
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -57,6 +60,8 @@ public static <T> T executeSync(Call<T> call) {
* Extracts and converts the response error body into an object.
*/
public static BinanceApiError getBinanceApiError(Response<?> response) throws IOException, BinanceApiException {
assert errorBodyConverter != null;
assert response.errorBody() != null;
return errorBodyConverter.convert(response.errorBody());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,72 @@
package com.binance.api.client.impl;

import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;

import com.binance.api.client.BinanceApiWebSocketClient;
import com.binance.api.client.config.BinanceApiConfig;
import com.binance.api.client.security.AuthenticationInterceptor;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import org.apache.commons.lang3.StringUtils;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;


/**
* Generates a Binance API implementation based on @see
* {@link BinanceApiService}.
*/
public class BinanceApiServiceGenerator implements ApiGenerator {

private final OkHttpClient sharedClient;
private final Converter.Factory converterFactory = JacksonConverterFactory.create();

{
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost(500);
dispatcher.setMaxRequests(500);
sharedClient = new OkHttpClient.Builder().dispatcher(dispatcher).pingInterval(20, TimeUnit.SECONDS).build();
}

@Override
public <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);
private final OkHttpClient sharedClient;
private final Converter.Factory converterFactory = JacksonConverterFactory.create();

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("45.76.144.167", 3128));

Authenticator proxyAuthenticator = (route, response) -> {
String credential = Credentials.basic("vexta", "vgm2022");
return response.request().newBuilder().header("Proxy-Authorization", credential).build();
};

{
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequestsPerHost(500);
dispatcher.setMaxRequests(500);
OkHttpClient.Builder builder = new OkHttpClient.Builder();
sharedClient = builder
.dispatcher(dispatcher)
.pingInterval(20, TimeUnit.SECONDS)
.build();
}

Retrofit retrofit = retrofitBuilder.build();
return retrofit.create(serviceClass);
}
@Override
public <S> S createService(Class<S> serviceClass, String apiKey, String secret) {
Retrofit.Builder retrofitBuilder = new Retrofit.Builder().baseUrl(BinanceApiConfig.getApiBaseUrl()).addConverterFactory(converterFactory);

@Override
public BinanceApiWebSocketClient createSocket() {
return new BinanceApiWebSocketClientImpl(sharedClient);
if (StringUtils.isEmpty(apiKey) || StringUtils.isEmpty(secret)) {
retrofitBuilder.client(sharedClient);
} else {
AuthenticationInterceptor interceptor = new AuthenticationInterceptor(apiKey, secret);
OkHttpClient adaptedClient = sharedClient
.newBuilder()
.addInterceptor(interceptor)
.build();
retrofitBuilder.client(adaptedClient);
}

}
Retrofit retrofit = retrofitBuilder.build();
return retrofit.create(serviceClass);
}

@Override
public BinanceApiWebSocketClient createSocket() {
return new BinanceApiWebSocketClientImpl(sharedClient);

}

}