File tree Expand file tree Collapse file tree 3 files changed +18
-4
lines changed
main/java/org/devlive/sdk/openai
test/java/org/devlive/sdk/openai Expand file tree Collapse file tree 3 files changed +18
-4
lines changed Original file line number Diff line number Diff line change 11package org .devlive .sdk .openai ;
22
33import lombok .extern .slf4j .Slf4j ;
4+ import okhttp3 .OkHttpClient ;
5+ import org .apache .commons .lang3 .ObjectUtils ;
46import org .devlive .sdk .openai .entity .CompletionChatEntity ;
57import org .devlive .sdk .openai .entity .CompletionEntity ;
68import org .devlive .sdk .openai .entity .ModelEntity ;
1416import org .devlive .sdk .openai .utils .ProviderUtils ;
1517
1618@ Slf4j
17- public abstract class DefaultClient
19+ public abstract class DefaultClient implements AutoCloseable
1820{
1921 protected DefaultApi api ;
2022 protected ProviderModel provider ;
23+ protected OkHttpClient client ;
2124
2225 public ModelResponse getModels ()
2326 {
@@ -54,4 +57,13 @@ public UserKeyResponse createUserAPIKey(UserKeyEntity configure)
5457 return this .api .fetchCreateUserAPIKey (configure )
5558 .blockingGet ();
5659 }
60+
61+ public void close ()
62+ {
63+ if (ObjectUtils .isNotEmpty (this .client )) {
64+ this .client .dispatcher ().cancelAll ();
65+ this .client .connectionPool ().evictAll ();
66+ this .client = null ;
67+ }
68+ }
5769}
Original file line number Diff line number Diff line change @@ -70,6 +70,7 @@ private OpenAiClient(OpenAiClientBuilder builder)
7070 }
7171
7272 super .provider = builder .provider ;
73+ super .client = builder .client ;
7374 // Build a remote API client
7475 objectMapper .setSerializationInclusion (JsonInclude .Include .NON_NULL );
7576 this .api = new Retrofit .Builder ()
Original file line number Diff line number Diff line change @@ -56,10 +56,11 @@ public void testClient()
5656 @ Test
5757 public void testNoAuthorized ()
5858 {
59- client = OpenAiClient .builder ()
59+ try ( OpenAiClient client = OpenAiClient .builder ()
6060 .apiKey (invalidApiKey )
61- .build ();
62- Assert .assertThrows (AuthorizedException .class , () -> client .getModels ());
61+ .build ()) {
62+ Assert .assertThrows (AuthorizedException .class , () -> client .getModels ());
63+ }
6364 }
6465
6566 @ Test
You can’t perform that action at this time.
0 commit comments