Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 05bf591

Browse filesBrowse files
authored
Exposed initialization state for service (for scenario testing). (TheoKanning#47)
Co-authored-by: hawkan <Aeroplane!2000>
1 parent e68a098 commit 05bf591
Copy full SHA for 05bf591

File tree

Expand file treeCollapse file tree

1 file changed

+34
-7
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+34
-7
lines changed

‎client/src/main/java/com/theokanning/openai/OpenAiService.java

Copy file name to clipboardExpand all lines: client/src/main/java/com/theokanning/openai/OpenAiService.java
+34-7Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,56 @@
2424
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
2525
import retrofit2.converter.jackson.JacksonConverterFactory;
2626

27+
import java.time.Duration;
2728
import java.util.List;
2829
import java.util.concurrent.TimeUnit;
2930

31+
import static java.time.Duration.ofSeconds;
32+
3033
public class OpenAiService {
3134

32-
OpenAiApi api;
35+
private static final String BASE_URL = "https://api.openai.com/";
36+
37+
final OpenAiApi api;
3338

3439
/**
3540
* Creates a new OpenAiService that wraps OpenAiApi
3641
*
3742
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
3843
*/
39-
public OpenAiService(String token) {
40-
this(token, 10);
44+
public OpenAiService(final String token) {
45+
this(token, BASE_URL, ofSeconds(10));
4146
}
4247

4348
/**
4449
* Creates a new OpenAiService that wraps OpenAiApi
4550
*
4651
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
4752
* @param timeout http read timeout in seconds, 0 means no timeout
53+
* @deprecated use {@link OpenAiService(String, Duration)}
54+
*/
55+
@Deprecated
56+
public OpenAiService(final String token, final int timeout) {
57+
this(token, BASE_URL, ofSeconds(timeout));
58+
}
59+
60+
/**
61+
* Creates a new OpenAiService that wraps OpenAiApi
62+
*
63+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
64+
* @param timeout http read timeout, Duration.ZERO means no timeout
65+
*/
66+
public OpenAiService(final String token, final Duration timeout) {
67+
this(token, BASE_URL, timeout);
68+
}
69+
70+
/**
71+
* Creates a new OpenAiService that wraps OpenAiApi
72+
*
73+
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
74+
* @param timeout http read timeout, Duration.ZERO means no timeout
4875
*/
49-
public OpenAiService(String token, int timeout) {
76+
public OpenAiService(final String token, final String baseUrl, final Duration timeout) {
5077
ObjectMapper mapper = new ObjectMapper();
5178
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
5279
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
@@ -55,11 +82,11 @@ public OpenAiService(String token, int timeout) {
5582
OkHttpClient client = new OkHttpClient.Builder()
5683
.addInterceptor(new AuthenticationInterceptor(token))
5784
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
58-
.readTimeout(timeout, TimeUnit.SECONDS)
85+
.readTimeout(timeout.toMillis(), TimeUnit.MILLISECONDS)
5986
.build();
6087

6188
Retrofit retrofit = new Retrofit.Builder()
62-
.baseUrl("https://api.openai.com/")
89+
.baseUrl(baseUrl)
6390
.client(client)
6491
.addConverterFactory(JacksonConverterFactory.create(mapper))
6592
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
@@ -73,7 +100,7 @@ public OpenAiService(String token, int timeout) {
73100
*
74101
* @param api OpenAiApi instance to use for all methods
75102
*/
76-
public OpenAiService(OpenAiApi api) {
103+
public OpenAiService(final OpenAiApi api) {
77104
this.api = api;
78105
}
79106

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.