Skip to content

Commit e4fc10e

Browse files
committed
allow custome interceptors be added to shared http client
1 parent 924d5f0 commit e4fc10e

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/main/java/com/binance/dex/api/client/BinanceDexApiClientGenerator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import com.fasterxml.jackson.datatype.joda.JodaModule;
5+
import okhttp3.Dispatcher;
6+
import okhttp3.Interceptor;
57
import okhttp3.OkHttpClient;
68
import okhttp3.ResponseBody;
79
import retrofit2.Call;
@@ -15,10 +17,6 @@
1517
import java.util.concurrent.TimeUnit;
1618

1719
public class BinanceDexApiClientGenerator {
18-
private static final OkHttpClient sharedClient = new OkHttpClient.Builder()
19-
.pingInterval(20, TimeUnit.SECONDS)
20-
.build();
21-
2220
private static final Converter.Factory converterFactory =
2321
JacksonConverterFactory.create(new ObjectMapper().registerModule(new JodaModule()));
2422

@@ -27,6 +25,17 @@ public class BinanceDexApiClientGenerator {
2725
(Converter<ResponseBody, BinanceDexApiError>) converterFactory.responseBodyConverter(
2826
BinanceDexApiError.class, new Annotation[0], null);
2927

28+
private static OkHttpClient sharedClient;
29+
static {
30+
Dispatcher dispatcher = new Dispatcher();
31+
dispatcher.setMaxRequestsPerHost(500);
32+
dispatcher.setMaxRequests(500);
33+
sharedClient = new OkHttpClient.Builder()
34+
.dispatcher(dispatcher)
35+
.pingInterval(20, TimeUnit.SECONDS)
36+
.build();
37+
}
38+
3039
public static <S> S createService(Class<S> serviceClass, String baseUrl) {
3140
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
3241
.baseUrl(baseUrl)
@@ -73,4 +82,11 @@ public static BinanceDexApiError getBinanceApiError(Response<?> response) throws
7382
public static OkHttpClient getSharedClient() {
7483
return sharedClient;
7584
}
85+
86+
/**
87+
* Add interceptor to shared client
88+
*/
89+
public static void addInterceptor(Interceptor interceptor) {
90+
sharedClient = sharedClient.newBuilder().addInterceptor(interceptor).build();
91+
}
7692
}

0 commit comments

Comments
 (0)