Skip to content

Commit 627cb56

Browse files
chore(internal): add and tweak check functions (#117)
chore(internal): tweak client options nullability handling
1 parent 387935f commit 627cb56

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,27 @@ package com.openai.core
44

55
@JvmSynthetic
66
internal fun <T : Any> checkRequired(name: String, value: T?): T =
7-
checkNotNull(value) { "`$name` is required but was not set" }
7+
checkNotNull(value) { "`$name` is required, but was not set" }
8+
9+
@JvmSynthetic
10+
internal fun checkLength(name: String, value: String, length: Int): String =
11+
value.also {
12+
check(it.length == length) { "`$name` must have length $length, but was ${it.length}" }
13+
}
14+
15+
@JvmSynthetic
16+
internal fun checkMinLength(name: String, value: String, minLength: Int): String =
17+
value.also {
18+
check(it.length >= minLength) {
19+
if (minLength == 1) "`$name` must be non-empty, but was empty"
20+
else "`$name` must have at least length $minLength, but was ${it.length}"
21+
}
22+
}
23+
24+
@JvmSynthetic
25+
internal fun checkMaxLength(name: String, value: String, maxLength: Int): String =
26+
value.also {
27+
check(it.length <= maxLength) {
28+
"`$name` must have at most length $maxLength, but was ${it.length}"
29+
}
30+
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ private constructor(
230230
}
231231

232232
fun build(): ClientOptions {
233-
checkRequired("httpClient", httpClient)
234-
checkRequired("credential", credential)
233+
val httpClient = checkRequired("httpClient", httpClient)
234+
val credential = checkRequired("credential", credential)
235235

236236
val headers = Headers.builder()
237237
val queryParams = QueryParams.builder()
@@ -245,12 +245,12 @@ private constructor(
245245
organization?.let { headers.put("OpenAI-Organization", it) }
246246
project?.let { headers.put("OpenAI-Project", it) }
247247

248-
when (val currentCredential = credential) {
248+
when (credential) {
249249
is AzureApiKeyCredential -> {
250-
headers.put("api-key", currentCredential.apiKey())
250+
headers.put("api-key", credential.apiKey())
251251
}
252252
is BearerTokenCredential -> {
253-
headers.put("Authorization", "Bearer ${currentCredential.token()}")
253+
headers.put("Authorization", "Bearer ${credential.token()}")
254254
}
255255
else -> {
256256
throw IllegalArgumentException("Invalid credential type")
@@ -268,10 +268,10 @@ private constructor(
268268
queryParams.replaceAll(this.queryParams.build())
269269

270270
return ClientOptions(
271-
httpClient!!,
271+
httpClient,
272272
PhantomReachableClosingHttpClient(
273273
RetryingHttpClient.builder()
274-
.httpClient(httpClient!!)
274+
.httpClient(httpClient)
275275
.clock(clock)
276276
.maxRetries(maxRetries)
277277
.build()
@@ -298,7 +298,7 @@ private constructor(
298298
queryParams.build(),
299299
responseValidation,
300300
maxRetries,
301-
credential!!,
301+
credential,
302302
organization,
303303
project,
304304
)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ sealed class JsonField<out T : Any> {
117117
is JsonValue -> this
118118
}
119119

120+
@JvmSynthetic fun accept(consume: (T) -> Unit) = asKnown().ifPresent(consume)
121+
120122
fun <R> accept(visitor: Visitor<T, R>): R =
121123
when (this) {
122124
is KnownValue -> visitor.visitKnown(value)

0 commit comments

Comments
 (0)