@@ -10,12 +10,16 @@ import com.openai.core.ClientOptions
10
10
import com.openai.core.Timeout
11
11
import com.openai.core.http.Headers
12
12
import com.openai.core.http.QueryParams
13
+ import com.openai.core.jsonMapper
13
14
import com.openai.credential.Credential
14
15
import java.net.Proxy
15
16
import java.time.Clock
16
17
import java.time.Duration
17
18
import java.util.Optional
18
19
import java.util.concurrent.Executor
20
+ import javax.net.ssl.HostnameVerifier
21
+ import javax.net.ssl.SSLSocketFactory
22
+ import javax.net.ssl.X509TrustManager
19
23
import kotlin.jvm.optionals.getOrNull
20
24
21
25
class OpenAIOkHttpClient private constructor() {
@@ -32,10 +36,63 @@ class OpenAIOkHttpClient private constructor() {
32
36
class Builder internal constructor() {
33
37
34
38
private var clientOptions: ClientOptions .Builder = ClientOptions .builder()
35
- private var timeout: Timeout = Timeout .default()
36
39
private var proxy: Proxy ? = null
40
+ private var sslSocketFactory: SSLSocketFactory ? = null
41
+ private var trustManager: X509TrustManager ? = null
42
+ private var hostnameVerifier: HostnameVerifier ? = null
37
43
38
- fun baseUrl (baseUrl : String ) = apply { clientOptions.baseUrl(baseUrl) }
44
+ fun proxy (proxy : Proxy ? ) = apply { this .proxy = proxy }
45
+
46
+ /* * Alias for calling [Builder.proxy] with `proxy.orElse(null)`. */
47
+ fun proxy (proxy : Optional <Proxy >) = proxy(proxy.getOrNull())
48
+
49
+ /* *
50
+ * The socket factory used to secure HTTPS connections.
51
+ *
52
+ * If this is set, then [trustManager] must also be set.
53
+ *
54
+ * If unset, then the system default is used. Most applications should not call this method,
55
+ * and instead use the system default. The default include special optimizations that can be
56
+ * lost if the implementation is modified.
57
+ */
58
+ fun sslSocketFactory (sslSocketFactory : SSLSocketFactory ? ) = apply {
59
+ this .sslSocketFactory = sslSocketFactory
60
+ }
61
+
62
+ /* * Alias for calling [Builder.sslSocketFactory] with `sslSocketFactory.orElse(null)`. */
63
+ fun sslSocketFactory (sslSocketFactory : Optional <SSLSocketFactory >) =
64
+ sslSocketFactory(sslSocketFactory.getOrNull())
65
+
66
+ /* *
67
+ * The trust manager used to secure HTTPS connections.
68
+ *
69
+ * If this is set, then [sslSocketFactory] must also be set.
70
+ *
71
+ * If unset, then the system default is used. Most applications should not call this method,
72
+ * and instead use the system default. The default include special optimizations that can be
73
+ * lost if the implementation is modified.
74
+ */
75
+ fun trustManager (trustManager : X509TrustManager ? ) = apply {
76
+ this .trustManager = trustManager
77
+ }
78
+
79
+ /* * Alias for calling [Builder.trustManager] with `trustManager.orElse(null)`. */
80
+ fun trustManager (trustManager : Optional <X509TrustManager >) =
81
+ trustManager(trustManager.getOrNull())
82
+
83
+ /* *
84
+ * The verifier used to confirm that response certificates apply to requested hostnames for
85
+ * HTTPS connections.
86
+ *
87
+ * If unset, then a default hostname verifier is used.
88
+ */
89
+ fun hostnameVerifier (hostnameVerifier : HostnameVerifier ? ) = apply {
90
+ this .hostnameVerifier = hostnameVerifier
91
+ }
92
+
93
+ /* * Alias for calling [Builder.hostnameVerifier] with `hostnameVerifier.orElse(null)`. */
94
+ fun hostnameVerifier (hostnameVerifier : Optional <HostnameVerifier >) =
95
+ hostnameVerifier(hostnameVerifier.getOrNull())
39
96
40
97
/* *
41
98
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +113,54 @@ class OpenAIOkHttpClient private constructor() {
56
113
57
114
fun clock (clock : Clock ) = apply { clientOptions.clock(clock) }
58
115
116
+ fun baseUrl (baseUrl : String? ) = apply { clientOptions.baseUrl(baseUrl) }
117
+
118
+ /* * Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
119
+ fun baseUrl (baseUrl : Optional <String >) = baseUrl(baseUrl.getOrNull())
120
+
121
+ fun responseValidation (responseValidation : Boolean ) = apply {
122
+ clientOptions.responseValidation(responseValidation)
123
+ }
124
+
125
+ fun timeout (timeout : Timeout ) = apply { clientOptions.timeout(timeout) }
126
+
127
+ /* *
128
+ * Sets the maximum time allowed for a complete HTTP call, not including retries.
129
+ *
130
+ * See [Timeout.request] for more details.
131
+ *
132
+ * For fine-grained control, pass a [Timeout] object.
133
+ */
134
+ fun timeout (timeout : Duration ) = apply { clientOptions.timeout(timeout) }
135
+
136
+ fun maxRetries (maxRetries : Int ) = apply { clientOptions.maxRetries(maxRetries) }
137
+
138
+ fun apiKey (apiKey : String ) = apply { clientOptions.apiKey(apiKey) }
139
+
140
+ fun credential (credential : Credential ) = apply { clientOptions.credential(credential) }
141
+
142
+ fun azureServiceVersion (azureServiceVersion : AzureOpenAIServiceVersion ) = apply {
143
+ clientOptions.azureServiceVersion(azureServiceVersion)
144
+ }
145
+
146
+ fun organization (organization : String? ) = apply { clientOptions.organization(organization) }
147
+
148
+ /* * Alias for calling [Builder.organization] with `organization.orElse(null)`. */
149
+ fun organization (organization : Optional <String >) = organization(organization.getOrNull())
150
+
151
+ fun project (project : String? ) = apply { clientOptions.project(project) }
152
+
153
+ /* * Alias for calling [Builder.project] with `project.orElse(null)`. */
154
+ fun project (project : Optional <String >) = project(project.getOrNull())
155
+
156
+ fun webhookSecret (webhookSecret : String? ) = apply {
157
+ clientOptions.webhookSecret(webhookSecret)
158
+ }
159
+
160
+ /* * Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
161
+ fun webhookSecret (webhookSecret : Optional <String >) =
162
+ webhookSecret(webhookSecret.getOrNull())
163
+
59
164
fun headers (headers : Headers ) = apply { clientOptions.headers(headers) }
60
165
61
166
fun headers (headers : Map <String , Iterable <String >>) = apply {
@@ -136,54 +241,6 @@ class OpenAIOkHttpClient private constructor() {
136
241
clientOptions.removeAllQueryParams(keys)
137
242
}
138
243
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
-
187
244
fun fromEnv () = apply { clientOptions.fromEnv() }
188
245
189
246
/* *
@@ -194,7 +251,15 @@ class OpenAIOkHttpClient private constructor() {
194
251
fun build (): OpenAIClient =
195
252
OpenAIClientImpl (
196
253
clientOptions
197
- .httpClient(OkHttpClient .builder().timeout(timeout).proxy(proxy).build())
254
+ .httpClient(
255
+ OkHttpClient .builder()
256
+ .timeout(clientOptions.timeout())
257
+ .proxy(proxy)
258
+ .sslSocketFactory(sslSocketFactory)
259
+ .trustManager(trustManager)
260
+ .hostnameVerifier(hostnameVerifier)
261
+ .build()
262
+ )
198
263
.build()
199
264
)
200
265
}
0 commit comments