55import com .fasterxml .jackson .databind .DeserializationFeature ;
66import com .fasterxml .jackson .databind .ObjectMapper ;
77import com .fasterxml .jackson .databind .PropertyNamingStrategy ;
8- import com .volcengine .ark .runtime .* ;
8+ import com .volcengine .ark .runtime .Const ;
99import com .volcengine .ark .runtime .exception .ArkAPIError ;
1010import com .volcengine .ark .runtime .exception .ArkException ;
1111import com .volcengine .ark .runtime .exception .ArkHttpException ;
12- import com .volcengine .ark .runtime .interceptor .AuthenticationInterceptor ;
13- import com .volcengine .ark .runtime .interceptor .ArkResourceStsAuthenticationInterceptor ;
14- import com .volcengine .ark .runtime .interceptor .RequestIdInterceptor ;
15- import com .volcengine .ark .runtime .interceptor .RetryInterceptor ;
12+ import com .volcengine .ark .runtime .interceptor .*;
1613import com .volcengine .ark .runtime .model .content .generation .DeleteContentGenerationTaskResponse ;
1714import com .volcengine .ark .runtime .interceptor .*;
1815import com .volcengine .ark .runtime .model .bot .completion .chat .BotChatCompletionChunk ;
3936import retrofit2 .converter .jackson .JacksonConverterFactory ;
4037import retrofit2 .Retrofit ;
4138
39+
4240import java .io .IOException ;
4341import java .net .Proxy ;
4442import java .time .Duration ;
4543import java .util .*;
44+ import java .util .concurrent .Executor ;
4645import java .util .concurrent .ExecutorService ;
4746import java .util .concurrent .TimeUnit ;
4847
@@ -59,7 +58,7 @@ public ArkService(final String apiKey) {
5958 public ArkService (final String apiKey , final Duration timeout ) {
6059 ObjectMapper mapper = defaultObjectMapper ();
6160 OkHttpClient client = defaultApiKeyClient (apiKey , timeout );
62- Retrofit retrofit = defaultRetrofit (client , mapper , BASE_URL );
61+ Retrofit retrofit = defaultRetrofit (client , mapper , BASE_URL , null );
6362
6463 this .api = retrofit .create (ArkApi .class );
6564 this .executorService = client .dispatcher ().executorService ();
@@ -72,7 +71,7 @@ public ArkService(final String ak, final String sk) {
7271 public ArkService (final String ak , final String sk , final Duration timeout ) {
7372 ObjectMapper mapper = defaultObjectMapper ();
7473 OkHttpClient client = defaultResourceStsClient (ak , sk , timeout , BASE_REGION );
75- Retrofit retrofit = defaultRetrofit (client , mapper , BASE_URL );
74+ Retrofit retrofit = defaultRetrofit (client , mapper , BASE_URL , null );
7675
7776 this .api = retrofit .create (ArkApi .class );
7877 this .executorService = client .dispatcher ().executorService ();
@@ -116,13 +115,19 @@ public static OkHttpClient defaultResourceStsClient(String ak, String sk, Durati
116115 .build ();
117116 }
118117
119- public static Retrofit defaultRetrofit (OkHttpClient client , ObjectMapper mapper , String baseUrl ) {
120- return new Retrofit .Builder ()
118+ public static Retrofit defaultRetrofit (OkHttpClient client , ObjectMapper mapper , String baseUrl , Executor callbackExecutor ) {
119+ Retrofit . Builder builder = new Retrofit .Builder ()
121120 .baseUrl (baseUrl )
122121 .client (client )
123122 .addConverterFactory (JacksonConverterFactory .create (mapper ))
124- .addCallAdapterFactory (RxJava2CallAdapterFactory .create ())
125- .build ();
123+ .addCallAdapterFactory (RxJava2CallAdapterFactory .create ());
124+
125+ if (callbackExecutor != null ) {
126+ // to avoid NPE
127+ builder .callbackExecutor (callbackExecutor );
128+ }
129+
130+ return builder .build ();
126131 }
127132
128133 public static <T > T execute (Single <T > apiCall ) {
@@ -329,6 +334,7 @@ public static class Builder {
329334 private Proxy proxy ;
330335 private ConnectionPool connectionPool ;
331336 private Dispatcher dispatcher ;
337+ private Executor callbackExecutor ;
332338
333339 public ArkService .Builder ak (String ak ) {
334340 this .ak = ak ;
@@ -388,6 +394,11 @@ public ArkService.Builder dispatcher(Dispatcher dispatcher) {
388394 return this ;
389395 }
390396
397+ public ArkService .Builder callbackExecutor (Executor callbackExecutor ) {
398+ this .callbackExecutor = callbackExecutor ;
399+ return this ;
400+ }
401+
391402 public ArkService build () {
392403 ObjectMapper mapper = defaultObjectMapper ();
393404 OkHttpClient .Builder clientBuilder = new OkHttpClient .Builder ();
@@ -421,7 +432,7 @@ public ArkService build() {
421432 .callTimeout (timeout .toMillis (), TimeUnit .MILLISECONDS )
422433 .connectTimeout (connectTimeout )
423434 .build ();
424- Retrofit retrofit = defaultRetrofit (client , mapper , baseUrl );
435+ Retrofit retrofit = defaultRetrofit (client , mapper , baseUrl , callbackExecutor );
425436
426437 return new ArkService (
427438 retrofit .create (ArkApi .class ),
0 commit comments