Skip to content

Commit

Permalink
fix(internal): add missing options
Browse files Browse the repository at this point in the history
  • Loading branch information
David Meadows authored and stainless-app[bot] committed Nov 12, 2024
1 parent 15f22cd commit 49c0367
Show file tree
Hide file tree
Showing 5 changed files with 674 additions and 11 deletions.
21 changes: 10 additions & 11 deletions orb-java-core/src/main/kotlin/com/withorb/api/core/ClientOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ private constructor(
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
@get:JvmName("clock") val clock: Clock,
@get:JvmName("baseUrl") val baseUrl: String,
@get:JvmName("apiKey") val apiKey: String,
@get:JvmName("webhookSecret") val webhookSecret: String?,
@get:JvmName("headers") val headers: Headers,
@get:JvmName("queryParams") val queryParams: QueryParams,
@get:JvmName("responseValidation") val responseValidation: Boolean,
@get:JvmName("maxRetries") val maxRetries: Int,
@get:JvmName("apiKey") val apiKey: String,
@get:JvmName("webhookSecret") val webhookSecret: String?,
) {

fun toBuilder() = Builder().from(this)
Expand All @@ -39,7 +39,7 @@ private constructor(
class Builder {

private var httpClient: HttpClient? = null
private var jsonMapper: JsonMapper? = null
private var jsonMapper: JsonMapper = jsonMapper()
private var clock: Clock = Clock.systemUTC()
private var baseUrl: String = PRODUCTION_URL
private var headers: Headers.Builder = Headers.builder()
Expand Down Expand Up @@ -67,10 +67,10 @@ private constructor(

fun jsonMapper(jsonMapper: JsonMapper) = apply { this.jsonMapper = jsonMapper }

fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }

fun clock(clock: Clock) = apply { this.clock = clock }

fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }

fun headers(headers: Headers) = apply {
this.headers.clear()
putAllHeaders(headers)
Expand All @@ -81,9 +81,8 @@ private constructor(
putAllHeaders(headers)
}

fun putHeader(name: String, value: String) = apply {
this.headers.getOrPut(name) { mutableListOf() }.add(value)
}
fun putHeader(name: String, value: String) = apply { headers.put(name, value) }

fun putHeaders(name: String, values: Iterable<String>) = apply { headers.put(name, values) }

fun putAllHeaders(headers: Headers) = apply { this.headers.putAll(headers) }
Expand Down Expand Up @@ -198,15 +197,15 @@ private constructor(
.idempotencyHeader("Idempotency-Key")
.build()
),
jsonMapper ?: jsonMapper(),
jsonMapper,
clock,
baseUrl,
apiKey!!,
webhookSecret,
headers.build(),
queryParams.build(),
responseValidation,
maxRetries,
apiKey!!,
webhookSecret,
)
}
}
Expand Down
92 changes: 92 additions & 0 deletions orb-java-core/src/main/kotlin/com/withorb/api/core/http/Headers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.withorb.api.core.http

import com.withorb.api.core.toImmutable
import java.util.TreeMap

class Headers
private constructor(
private val map: Map<String, List<String>>,
@get:JvmName("size") val size: Int
) {

fun isEmpty(): Boolean = map.isEmpty()

fun names(): Set<String> = map.keys

fun values(name: String): List<String> = map[name].orEmpty()

fun toBuilder(): Builder = Builder().putAll(map)

companion object {

@JvmStatic fun builder() = Builder()
}

class Builder {

private val map: MutableMap<String, MutableList<String>> =
TreeMap(String.CASE_INSENSITIVE_ORDER)
private var size: Int = 0

fun put(name: String, value: String) = apply {
map.getOrPut(name) { mutableListOf() }.add(value)
size++
}

fun put(name: String, values: Iterable<String>) = apply { values.forEach { put(name, it) } }

fun putAll(headers: Map<String, Iterable<String>>) = apply { headers.forEach(::put) }

fun putAll(headers: Headers) = apply {
headers.names().forEach { put(it, headers.values(it)) }
}

fun remove(name: String) = apply { size -= map.remove(name).orEmpty().size }

fun removeAll(names: Set<String>) = apply { names.forEach(::remove) }

fun clear() = apply {
map.clear()
size = 0
}

fun replace(name: String, value: String) = apply {
remove(name)
put(name, value)
}

fun replace(name: String, values: Iterable<String>) = apply {
remove(name)
put(name, values)
}

fun replaceAll(headers: Map<String, Iterable<String>>) = apply {
headers.forEach(::replace)
}

fun replaceAll(headers: Headers) = apply {
headers.names().forEach { replace(it, headers.values(it)) }
}

fun build() =
Headers(
map.mapValuesTo(TreeMap(String.CASE_INSENSITIVE_ORDER)) { (_, values) ->
values.toImmutable()
}
.toImmutable(),
size
)
}

override fun hashCode(): Int = map.hashCode()

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is Headers && map == other.map
}

override fun toString(): String = "Headers{map=$map}"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.withorb.api.core.http

import com.withorb.api.core.toImmutable

class QueryParams
private constructor(
private val map: Map<String, List<String>>,
@get:JvmName("size") val size: Int
) {

fun isEmpty(): Boolean = map.isEmpty()

fun keys(): Set<String> = map.keys

fun values(key: String): List<String> = map[key].orEmpty()

fun toBuilder(): Builder = Builder().putAll(map)

companion object {

@JvmStatic fun builder() = Builder()
}

class Builder {

private val map: MutableMap<String, MutableList<String>> = mutableMapOf()
private var size: Int = 0

fun put(key: String, value: String) = apply {
map.getOrPut(key) { mutableListOf() }.add(value)
size++
}

fun put(key: String, values: Iterable<String>) = apply { values.forEach { put(key, it) } }

fun putAll(queryParams: Map<String, Iterable<String>>) = apply {
queryParams.forEach(::put)
}

fun putAll(queryParams: QueryParams) = apply {
queryParams.keys().forEach { put(it, queryParams.values(it)) }
}

fun replace(key: String, value: String) = apply {
remove(key)
put(key, value)
}

fun replace(key: String, values: Iterable<String>) = apply {
remove(key)
put(key, values)
}

fun replaceAll(queryParams: Map<String, Iterable<String>>) = apply {
queryParams.forEach(::replace)
}

fun replaceAll(queryParams: QueryParams) = apply {
queryParams.keys().forEach { replace(it, queryParams.values(it)) }
}

fun remove(key: String) = apply { size -= map.remove(key).orEmpty().size }

fun removeAll(keys: Set<String>) = apply { keys.forEach(::remove) }

fun clear() = apply {
map.clear()
size = 0
}

fun build() =
QueryParams(map.mapValues { (_, values) -> values.toImmutable() }.toImmutable(), size)
}

override fun hashCode(): Int = map.hashCode()

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is QueryParams && map == other.map
}

override fun toString(): String = "QueryParams{map=$map}"
}
Loading

0 comments on commit 49c0367

Please sign in to comment.