Skip to content

Commit ee6259b

Browse files
chore(internal): refactor delegating from client to options
1 parent 71cf8ab commit ee6259b

File tree

3 files changed

+122
-6
lines changed

3 files changed

+122
-6
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.openai.core.Timeout
1111
import com.openai.core.http.Headers
1212
import com.openai.core.http.QueryParams
1313
import com.openai.credential.Credential
14+
15+
import com.openai.core.jsonMapper
1416
import java.net.Proxy
1517
import java.time.Clock
1618
import java.time.Duration
@@ -32,10 +34,9 @@ class OpenAIOkHttpClient private constructor() {
3234
class Builder internal constructor() {
3335

3436
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
35-
private var timeout: Timeout = Timeout.default()
3637
private var proxy: Proxy? = null
3738

38-
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
39+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3940

4041
/**
4142
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +57,48 @@ class OpenAIOkHttpClient private constructor() {
5657

5758
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
5859

60+
fun baseUrl(baseUrl: String?) = apply { clientOptions.baseUrl(baseUrl) }
61+
62+
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
63+
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
64+
65+
fun responseValidation(responseValidation: Boolean) = apply {
66+
clientOptions.responseValidation(responseValidation)
67+
}
68+
69+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
70+
71+
/**
72+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
73+
*
74+
* See [Timeout.request] for more details.
75+
*
76+
* For fine-grained control, pass a [Timeout] object.
77+
*/
78+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
79+
80+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
81+
82+
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
83+
84+
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
85+
86+
/** Alias for calling [Builder.organization] with `organization.orElse(null)`. */
87+
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
88+
89+
fun project(project: String?) = apply { clientOptions.project(project) }
90+
91+
/** Alias for calling [Builder.project] with `project.orElse(null)`. */
92+
fun project(project: Optional<String>) = project(project.getOrNull())
93+
94+
fun webhookSecret(webhookSecret: String?) = apply {
95+
clientOptions.webhookSecret(webhookSecret)
96+
}
97+
98+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
99+
fun webhookSecret(webhookSecret: Optional<String>) =
100+
webhookSecret(webhookSecret.getOrNull())
101+
59102
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
60103

61104
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -194,7 +237,9 @@ class OpenAIOkHttpClient private constructor() {
194237
fun build(): OpenAIClient =
195238
OpenAIClientImpl(
196239
clientOptions
197-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
240+
.httpClient(
241+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
242+
)
198243
.build()
199244
)
200245
}

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import com.openai.core.Timeout
1111
import com.openai.core.http.Headers
1212
import com.openai.core.http.QueryParams
1313
import com.openai.credential.Credential
14+
15+
import com.openai.core.jsonMapper
1416
import java.net.Proxy
1517
import java.time.Clock
1618
import java.time.Duration
@@ -32,10 +34,9 @@ class OpenAIOkHttpClientAsync private constructor() {
3234
class Builder internal constructor() {
3335

3436
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
35-
private var timeout: Timeout = Timeout.default()
3637
private var proxy: Proxy? = null
3738

38-
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
39+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3940

4041
/**
4142
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +57,48 @@ class OpenAIOkHttpClientAsync private constructor() {
5657

5758
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
5859

60+
fun baseUrl(baseUrl: String?) = apply { clientOptions.baseUrl(baseUrl) }
61+
62+
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
63+
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
64+
65+
fun responseValidation(responseValidation: Boolean) = apply {
66+
clientOptions.responseValidation(responseValidation)
67+
}
68+
69+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
70+
71+
/**
72+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
73+
*
74+
* See [Timeout.request] for more details.
75+
*
76+
* For fine-grained control, pass a [Timeout] object.
77+
*/
78+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
79+
80+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
81+
82+
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
83+
84+
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
85+
86+
/** Alias for calling [Builder.organization] with `organization.orElse(null)`. */
87+
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
88+
89+
fun project(project: String?) = apply { clientOptions.project(project) }
90+
91+
/** Alias for calling [Builder.project] with `project.orElse(null)`. */
92+
fun project(project: Optional<String>) = project(project.getOrNull())
93+
94+
fun webhookSecret(webhookSecret: String?) = apply {
95+
clientOptions.webhookSecret(webhookSecret)
96+
}
97+
98+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
99+
fun webhookSecret(webhookSecret: Optional<String>) =
100+
webhookSecret(webhookSecret.getOrNull())
101+
59102
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
60103

61104
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -194,7 +237,9 @@ class OpenAIOkHttpClientAsync private constructor() {
194237
fun build(): OpenAIClientAsync =
195238
OpenAIClientAsyncImpl(
196239
clientOptions
197-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
240+
.httpClient(
241+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
242+
)
198243
.build()
199244
)
200245
}

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.openai.core.http.RetryingHttpClient
1313
import com.openai.credential.BearerTokenCredential
1414
import com.openai.credential.Credential
1515
import java.time.Clock
16+
import java.time.Duration
1617
import java.util.Optional
1718
import java.util.concurrent.Executor
1819
import java.util.concurrent.Executors
@@ -24,6 +25,13 @@ class ClientOptions
2425
private constructor(
2526
private val originalHttpClient: HttpClient,
2627
@get:JvmName("httpClient") val httpClient: HttpClient,
28+
/**
29+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
30+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
31+
*
32+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee that
33+
* the SDK will work correctly when using an incompatible Jackson version.
34+
*/
2735
@get:JvmName("checkJacksonVersionCompatibility") val checkJacksonVersionCompatibility: Boolean,
2836
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
2937
@get:JvmName("streamHandlerExecutor") val streamHandlerExecutor: Executor,
@@ -119,6 +127,13 @@ private constructor(
119127
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
120128
}
121129

130+
/**
131+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
132+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
133+
*
134+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee
135+
* that the SDK will work correctly when using an incompatible Jackson version.
136+
*/
122137
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
123138
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
124139
}
@@ -142,6 +157,15 @@ private constructor(
142157

143158
fun timeout(timeout: Timeout) = apply { this.timeout = timeout }
144159

160+
/**
161+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
162+
*
163+
* See [Timeout.request] for more details.
164+
*
165+
* For fine-grained control, pass a [Timeout] object.
166+
*/
167+
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
168+
145169
fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }
146170

147171
fun apiKey(apiKey: String) = apply {
@@ -250,6 +274,8 @@ private constructor(
250274

251275
fun removeAllQueryParams(keys: Set<String>) = apply { queryParams.removeAll(keys) }
252276

277+
fun timeout(): Timeout = timeout
278+
253279
fun fromEnv() = apply {
254280
System.getenv("OPENAI_BASE_URL")?.let { baseUrl(it) }
255281

0 commit comments

Comments
 (0)