Skip to content

Commit e89596d

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

File tree

3 files changed

+132
-102
lines changed

3 files changed

+132
-102
lines changed

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

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.openai.core.ClientOptions
1010
import com.openai.core.Timeout
1111
import com.openai.core.http.Headers
1212
import com.openai.core.http.QueryParams
13+
import com.openai.core.jsonMapper
1314
import com.openai.credential.Credential
1415
import java.net.Proxy
1516
import java.time.Clock
@@ -32,10 +33,9 @@ class OpenAIOkHttpClient private constructor() {
3233
class Builder internal constructor() {
3334

3435
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
35-
private var timeout: Timeout = Timeout.default()
3636
private var proxy: Proxy? = null
3737

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

4040
/**
4141
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +56,54 @@ class OpenAIOkHttpClient private constructor() {
5656

5757
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
5858

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

61109
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -136,54 +184,6 @@ class OpenAIOkHttpClient private constructor() {
136184
clientOptions.removeAllQueryParams(keys)
137185
}
138186

139-
fun timeout(timeout: Timeout) = apply {
140-
clientOptions.timeout(timeout)
141-
this.timeout = timeout
142-
}
143-
144-
/**
145-
* Sets the maximum time allowed for a complete HTTP call, not including retries.
146-
*
147-
* See [Timeout.request] for more details.
148-
*
149-
* For fine-grained control, pass a [Timeout] object.
150-
*/
151-
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
152-
153-
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
154-
155-
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
156-
157-
fun responseValidation(responseValidation: Boolean) = apply {
158-
clientOptions.responseValidation(responseValidation)
159-
}
160-
161-
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
162-
163-
fun credential(credential: Credential) = apply { clientOptions.credential(credential) }
164-
165-
fun azureServiceVersion(azureServiceVersion: AzureOpenAIServiceVersion) = apply {
166-
clientOptions.azureServiceVersion(azureServiceVersion)
167-
}
168-
169-
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
170-
171-
/** Alias for calling [Builder.organization] with `organization.orElse(null)`. */
172-
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
173-
174-
fun project(project: String?) = apply { clientOptions.project(project) }
175-
176-
/** Alias for calling [Builder.project] with `project.orElse(null)`. */
177-
fun project(project: Optional<String>) = project(project.getOrNull())
178-
179-
fun webhookSecret(webhookSecret: String?) = apply {
180-
clientOptions.webhookSecret(webhookSecret)
181-
}
182-
183-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
184-
fun webhookSecret(webhookSecret: Optional<String>) =
185-
webhookSecret(webhookSecret.getOrNull())
186-
187187
fun fromEnv() = apply { clientOptions.fromEnv() }
188188

189189
/**
@@ -194,7 +194,9 @@ class OpenAIOkHttpClient private constructor() {
194194
fun build(): OpenAIClient =
195195
OpenAIClientImpl(
196196
clientOptions
197-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
197+
.httpClient(
198+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
199+
)
198200
.build()
199201
)
200202
}

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

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.openai.core.ClientOptions
1010
import com.openai.core.Timeout
1111
import com.openai.core.http.Headers
1212
import com.openai.core.http.QueryParams
13+
import com.openai.core.jsonMapper
1314
import com.openai.credential.Credential
1415
import java.net.Proxy
1516
import java.time.Clock
@@ -32,10 +33,9 @@ class OpenAIOkHttpClientAsync private constructor() {
3233
class Builder internal constructor() {
3334

3435
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
35-
private var timeout: Timeout = Timeout.default()
3636
private var proxy: Proxy? = null
3737

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

4040
/**
4141
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +56,54 @@ class OpenAIOkHttpClientAsync private constructor() {
5656

5757
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
5858

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

61109
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -136,54 +184,6 @@ class OpenAIOkHttpClientAsync private constructor() {
136184
clientOptions.removeAllQueryParams(keys)
137185
}
138186

139-
fun timeout(timeout: Timeout) = apply {
140-
clientOptions.timeout(timeout)
141-
this.timeout = timeout
142-
}
143-
144-
/**
145-
* Sets the maximum time allowed for a complete HTTP call, not including retries.
146-
*
147-
* See [Timeout.request] for more details.
148-
*
149-
* For fine-grained control, pass a [Timeout] object.
150-
*/
151-
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
152-
153-
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
154-
155-
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
156-
157-
fun responseValidation(responseValidation: Boolean) = apply {
158-
clientOptions.responseValidation(responseValidation)
159-
}
160-
161-
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
162-
163-
fun credential(credential: Credential) = apply { clientOptions.credential(credential) }
164-
165-
fun azureServiceVersion(azureServiceVersion: AzureOpenAIServiceVersion) = apply {
166-
clientOptions.azureServiceVersion(azureServiceVersion)
167-
}
168-
169-
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
170-
171-
/** Alias for calling [Builder.organization] with `organization.orElse(null)`. */
172-
fun organization(organization: Optional<String>) = organization(organization.getOrNull())
173-
174-
fun project(project: String?) = apply { clientOptions.project(project) }
175-
176-
/** Alias for calling [Builder.project] with `project.orElse(null)`. */
177-
fun project(project: Optional<String>) = project(project.getOrNull())
178-
179-
fun webhookSecret(webhookSecret: String?) = apply {
180-
clientOptions.webhookSecret(webhookSecret)
181-
}
182-
183-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
184-
fun webhookSecret(webhookSecret: Optional<String>) =
185-
webhookSecret(webhookSecret.getOrNull())
186-
187187
fun fromEnv() = apply { clientOptions.fromEnv() }
188188

189189
/**
@@ -194,7 +194,9 @@ class OpenAIOkHttpClientAsync private constructor() {
194194
fun build(): OpenAIClientAsync =
195195
OpenAIClientAsyncImpl(
196196
clientOptions
197-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
197+
.httpClient(
198+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
199+
)
198200
.build()
199201
)
200202
}

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)