openai/openai-java

Public

mirrored from https://github.com/openai/openai-javaAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v1.4.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

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

199lines · modecode

1// File generated from our OpenAPI spec by Stainless.
2
3package com.openai.client.okhttp
4
5import com.fasterxml.jackson.databind.json.JsonMapper
6import com.openai.azure.AzureOpenAIServiceVersion
7import com.openai.client.OpenAIClientAsync
8import com.openai.client.OpenAIClientAsyncImpl
9import com.openai.core.ClientOptions
10import com.openai.core.Timeout
11import com.openai.core.http.Headers
12import com.openai.core.http.QueryParams
13import com.openai.credential.Credential
14import java.net.Proxy
15import java.time.Clock
16import java.time.Duration
17import java.util.Optional
18import java.util.concurrent.Executor
19import kotlin.jvm.optionals.getOrNull
20
21class OpenAIOkHttpClientAsync private constructor() {
22
23 companion object {
24
25 /** Returns a mutable builder for constructing an instance of [OpenAIOkHttpClientAsync]. */
26 @JvmStatic fun builder() = Builder()
27
28 @JvmStatic fun fromEnv(): OpenAIClientAsync = builder().fromEnv().build()
29 }
30
31 /** A builder for [OpenAIOkHttpClientAsync]. */
32 class Builder internal constructor() {
33
34 private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
35 private var timeout: Timeout = Timeout.default()
36 private var proxy: Proxy? = null
37
38 fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
39
40 /**
41 * Whether to throw an exception if any of the Jackson versions detected at runtime are
42 * incompatible with the SDK's minimum supported Jackson version (2.13.4).
43 *
44 * Defaults to true. Use extreme caution when disabling this option. There is no guarantee
45 * that the SDK will work correctly when using an incompatible Jackson version.
46 */
47 fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
48 clientOptions.checkJacksonVersionCompatibility(checkJacksonVersionCompatibility)
49 }
50
51 fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) }
52
53 fun streamHandlerExecutor(streamHandlerExecutor: Executor) = apply {
54 clientOptions.streamHandlerExecutor(streamHandlerExecutor)
55 }
56
57 fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
58
59 fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
60
61 fun headers(headers: Map<String, Iterable<String>>) = apply {
62 clientOptions.headers(headers)
63 }
64
65 fun putHeader(name: String, value: String) = apply { clientOptions.putHeader(name, value) }
66
67 fun putHeaders(name: String, values: Iterable<String>) = apply {
68 clientOptions.putHeaders(name, values)
69 }
70
71 fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }
72
73 fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
74 clientOptions.putAllHeaders(headers)
75 }
76
77 fun replaceHeaders(name: String, value: String) = apply {
78 clientOptions.replaceHeaders(name, value)
79 }
80
81 fun replaceHeaders(name: String, values: Iterable<String>) = apply {
82 clientOptions.replaceHeaders(name, values)
83 }
84
85 fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }
86
87 fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
88 clientOptions.replaceAllHeaders(headers)
89 }
90
91 fun removeHeaders(name: String) = apply { clientOptions.removeHeaders(name) }
92
93 fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) }
94
95 fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }
96
97 fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
98 clientOptions.queryParams(queryParams)
99 }
100
101 fun putQueryParam(key: String, value: String) = apply {
102 clientOptions.putQueryParam(key, value)
103 }
104
105 fun putQueryParams(key: String, values: Iterable<String>) = apply {
106 clientOptions.putQueryParams(key, values)
107 }
108
109 fun putAllQueryParams(queryParams: QueryParams) = apply {
110 clientOptions.putAllQueryParams(queryParams)
111 }
112
113 fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
114 clientOptions.putAllQueryParams(queryParams)
115 }
116
117 fun replaceQueryParams(key: String, value: String) = apply {
118 clientOptions.replaceQueryParams(key, value)
119 }
120
121 fun replaceQueryParams(key: String, values: Iterable<String>) = apply {
122 clientOptions.replaceQueryParams(key, values)
123 }
124
125 fun replaceAllQueryParams(queryParams: QueryParams) = apply {
126 clientOptions.replaceAllQueryParams(queryParams)
127 }
128
129 fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
130 clientOptions.replaceAllQueryParams(queryParams)
131 }
132
133 fun removeQueryParams(key: String) = apply { clientOptions.removeQueryParams(key) }
134
135 fun removeAllQueryParams(keys: Set<String>) = apply {
136 clientOptions.removeAllQueryParams(keys)
137 }
138
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 fromEnv() = apply { clientOptions.fromEnv() }
180
181 /**
182 * Returns an immutable instance of [OpenAIClientAsync].
183 *
184 * Further updates to this [Builder] will not mutate the returned instance.
185 */
186 fun build(): OpenAIClientAsync =
187 OpenAIClientAsyncImpl(
188 clientOptions
189 .httpClient(
190 OkHttpClient.builder()
191 .baseUrl(clientOptions.baseUrl())
192 .timeout(timeout)
193 .proxy(proxy)
194 .build()
195 )
196 .build()
197 )
198 }
199}
200