55import com .binance .api .client .exception .BinanceApiException ;
66import com .binance .api .client .security .AuthenticationInterceptor ;
77import okhttp3 .OkHttpClient ;
8+ import okhttp3 .RequestBody ;
9+ import okhttp3 .ResponseBody ;
810import org .apache .commons .lang3 .StringUtils ;
911import retrofit2 .Call ;
12+ import retrofit2 .Converter ;
1013import retrofit2 .Response ;
1114import retrofit2 .Retrofit ;
1215import retrofit2 .converter .jackson .JacksonConverterFactory ;
1821 * Generates a Binance API implementation based on @see {@link BinanceApiService}.
1922 */
2023public class BinanceApiServiceGenerator {
24+ private static final OkHttpClient sharedClient = new OkHttpClient .Builder ().build ();
25+ private static final Converter .Factory converterFactory = JacksonConverterFactory .create ();
2126
22- static OkHttpClient .Builder httpClient = new OkHttpClient .Builder ();
23-
24- private static Retrofit .Builder builder =
25- new Retrofit .Builder ()
26- .baseUrl (BinanceApiConstants .API_BASE_URL )
27- .addConverterFactory (JacksonConverterFactory .create ());
28-
29- private static Retrofit retrofit = builder .build ();
27+ @ SuppressWarnings ("unchecked" )
28+ private static final Converter <ResponseBody , BinanceApiError > errorBodyConverter =
29+ (Converter <ResponseBody , BinanceApiError >)converterFactory .responseBodyConverter (
30+ BinanceApiError .class , new Annotation [0 ], null );
3031
3132 public static <S > S createService (Class <S > serviceClass ) {
3233 return createService (serviceClass , null , null );
3334 }
3435
3536 public static <S > S createService (Class <S > serviceClass , String apiKey , String secret ) {
36- if (!StringUtils .isEmpty (apiKey ) && !StringUtils .isEmpty (secret )) {
37+ Retrofit .Builder retrofitBuilder = new Retrofit .Builder ()
38+ .baseUrl (BinanceApiConstants .API_BASE_URL )
39+ .addConverterFactory (converterFactory );
40+
41+ if (StringUtils .isEmpty (apiKey ) || StringUtils .isEmpty (secret )) {
42+ retrofitBuilder .client (sharedClient );
43+ } else {
44+ // `adaptedClient` will use its own interceptor, but share thread pool etc with the 'parent' client
3745 AuthenticationInterceptor interceptor = new AuthenticationInterceptor (apiKey , secret );
38- if (!httpClient .interceptors ().contains (interceptor )) {
39- httpClient .addInterceptor (interceptor );
40- builder .client (httpClient .build ());
41- retrofit = builder .build ();
42- }
46+ OkHttpClient adaptedClient = sharedClient .newBuilder ().addInterceptor (interceptor ).build ();
47+ retrofitBuilder .client (adaptedClient );
4348 }
49+
50+ Retrofit retrofit = retrofitBuilder .build ();
4451 return retrofit .create (serviceClass );
4552 }
4653
@@ -65,7 +72,6 @@ public static <T> T executeSync(Call<T> call) {
6572 * Extracts and converts the response error body into an object.
6673 */
6774 public static BinanceApiError getBinanceApiError (Response <?> response ) throws IOException , BinanceApiException {
68- return (BinanceApiError )retrofit .responseBodyConverter (BinanceApiError .class , new Annotation [0 ])
69- .convert (response .errorBody ());
75+ return errorBodyConverter .convert (response .errorBody ());
7076 }
7177}
0 commit comments