Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(internal): add and tweak check functions #195

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion orb-java-core/src/main/kotlin/com/withorb/api/core/Check.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,27 @@ package com.withorb.api.core

@JvmSynthetic
internal fun <T : Any> checkRequired(name: String, value: T?): T =
checkNotNull(value) { "`$name` is required but was not set" }
checkNotNull(value) { "`$name` is required, but was not set" }

@JvmSynthetic
internal fun checkLength(name: String, value: String, length: Int): String =
value.also {
check(it.length == length) { "`$name` must have length $length, but was ${it.length}" }
}

@JvmSynthetic
internal fun checkMinLength(name: String, value: String, minLength: Int): String =
value.also {
check(it.length >= minLength) {
if (minLength == 1) "`$name` must be non-empty, but was empty"
else "`$name` must have at least length $minLength, but was ${it.length}"
}
}

@JvmSynthetic
internal fun checkMaxLength(name: String, value: String, maxLength: Int): String =
value.also {
check(it.length <= maxLength) {
"`$name` must have at most length $maxLength, but was ${it.length}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ private constructor(
}

fun build(): ClientOptions {
checkRequired("httpClient", httpClient)
checkRequired("apiKey", apiKey)
val httpClient = checkRequired("httpClient", httpClient)
val apiKey = checkRequired("apiKey", apiKey)

val headers = Headers.builder()
val queryParams = QueryParams.builder()
Expand All @@ -183,7 +183,7 @@ private constructor(
headers.put("X-Stainless-Package-Version", getPackageVersion())
headers.put("X-Stainless-Runtime", "JRE")
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
apiKey?.let {
apiKey.let {
if (!it.isEmpty()) {
headers.put("Authorization", "Bearer $it")
}
Expand All @@ -192,10 +192,10 @@ private constructor(
queryParams.replaceAll(this.queryParams.build())

return ClientOptions(
httpClient!!,
httpClient,
PhantomReachableClosingHttpClient(
RetryingHttpClient.builder()
.httpClient(httpClient!!)
.httpClient(httpClient)
.clock(clock)
.maxRetries(maxRetries)
.idempotencyHeader("Idempotency-Key")
Expand All @@ -208,7 +208,7 @@ private constructor(
queryParams.build(),
responseValidation,
maxRetries,
apiKey!!,
apiKey,
webhookSecret,
)
}
Expand Down
2 changes: 2 additions & 0 deletions orb-java-core/src/main/kotlin/com/withorb/api/core/Values.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ sealed class JsonField<out T : Any> {
is JsonValue -> this
}

@JvmSynthetic fun accept(consume: (T) -> Unit) = asKnown().ifPresent(consume)

fun <R> accept(visitor: Visitor<T, R>): R =
when (this) {
is KnownValue -> visitor.visitKnown(value)
Expand Down