88import okhttp3 .OkHttpClient ;
99import org .apache .commons .lang3 .ObjectUtils ;
1010import org .apache .commons .lang3 .StringUtils ;
11+ import org .devlive .sdk .openai .exception .ParamException ;
1112import org .devlive .sdk .openai .interceptor .DefaultInterceptor ;
1213import retrofit2 .Retrofit ;
1314import retrofit2 .adapter .rxjava2 .RxJava2CallAdapterFactory ;
@@ -36,16 +37,16 @@ private OpenAiClient(OpenAiClientBuilder builder)
3637 }
3738 Preconditions .checkState (hasApiKey , "Invalid OpenAi token" );
3839
39- if (ObjectUtils .isEmpty (apiHost )) {
40+ if (ObjectUtils .isEmpty (builder . apiHost )) {
4041 builder .apiHost (null );
4142 }
42- if (ObjectUtils .isEmpty (timeout )) {
43+ if (ObjectUtils .isEmpty (builder . timeout )) {
4344 builder .timeout (null );
4445 }
45- if (ObjectUtils .isEmpty (unit )) {
46+ if (ObjectUtils .isEmpty (builder . unit )) {
4647 builder .unit (null );
4748 }
48- if (ObjectUtils .isEmpty (client )) {
49+ if (ObjectUtils .isEmpty (builder . client )) {
4950 builder .client (null );
5051 }
5152
@@ -101,10 +102,10 @@ public OpenAiClientBuilder unit(TimeUnit unit)
101102
102103 public OpenAiClientBuilder client (OkHttpClient client )
103104 {
105+ DefaultInterceptor interceptor = new DefaultInterceptor ();
106+ interceptor .setApiKey (this .apiKey );
104107 if (ObjectUtils .isEmpty (client )) {
105108 log .warn ("No client, creating default client" );
106- DefaultInterceptor interceptor = new DefaultInterceptor ();
107- interceptor .setApiKey (this .apiKey );
108109 client = new OkHttpClient .Builder ()
109110 .addInterceptor (interceptor )
110111 .connectTimeout (this .timeout , this .unit )
@@ -113,6 +114,18 @@ public OpenAiClientBuilder client(OkHttpClient client)
113114 .callTimeout (this .timeout , this .unit )
114115 .build ();
115116 }
117+
118+ if (client .interceptors ().size () <= 0 ) {
119+ throw new ParamException ("No interceptors available" );
120+ }
121+
122+ long count = client .interceptors ()
123+ .stream ()
124+ .filter (inter -> inter instanceof DefaultInterceptor )
125+ .count ();
126+ if (count <= 0 ) {
127+ throw new ParamException ("Must inject DefaultInterceptor" );
128+ }
116129 this .client = client ;
117130 return this ;
118131 }
0 commit comments