22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .fasterxml .jackson .datatype .joda .JodaModule ;
5+ import okhttp3 .Dispatcher ;
6+ import okhttp3 .Interceptor ;
57import okhttp3 .OkHttpClient ;
68import okhttp3 .ResponseBody ;
79import retrofit2 .Call ;
1517import java .util .concurrent .TimeUnit ;
1618
1719public 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