Skip to content

Commit abbf2d2

Browse files
chore(internal): add and tweak check functions (#692)
chore(internal): tweak client options nullability handling
1 parent f6dbaf9 commit abbf2d2

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

increase-java-core/src/main/kotlin/com/increase/api/core/Check.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,27 @@ package com.increase.api.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+
}

increase-java-core/src/main/kotlin/com/increase/api/core/ClientOptions.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ private constructor(
173173
}
174174

175175
fun build(): ClientOptions {
176-
checkRequired("httpClient", httpClient)
177-
checkRequired("apiKey", apiKey)
176+
val httpClient = checkRequired("httpClient", httpClient)
177+
val apiKey = checkRequired("apiKey", apiKey)
178178

179179
val headers = Headers.builder()
180180
val queryParams = QueryParams.builder()
@@ -185,7 +185,7 @@ private constructor(
185185
headers.put("X-Stainless-Package-Version", getPackageVersion())
186186
headers.put("X-Stainless-Runtime", "JRE")
187187
headers.put("X-Stainless-Runtime-Version", getJavaVersion())
188-
apiKey?.let {
188+
apiKey.let {
189189
if (!it.isEmpty()) {
190190
headers.put("Authorization", "Bearer $it")
191191
}
@@ -194,10 +194,10 @@ private constructor(
194194
queryParams.replaceAll(this.queryParams.build())
195195

196196
return ClientOptions(
197-
httpClient!!,
197+
httpClient,
198198
PhantomReachableClosingHttpClient(
199199
RetryingHttpClient.builder()
200-
.httpClient(httpClient!!)
200+
.httpClient(httpClient)
201201
.clock(clock)
202202
.maxRetries(maxRetries)
203203
.idempotencyHeader("Idempotency-Key")
@@ -210,7 +210,7 @@ private constructor(
210210
queryParams.build(),
211211
responseValidation,
212212
maxRetries,
213-
apiKey!!,
213+
apiKey,
214214
webhookSecret,
215215
)
216216
}

increase-java-core/src/main/kotlin/com/increase/api/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)