Skip to content

Commit 49c0367

Browse files
David Meadowsstainless-app[bot]
authored andcommitted
fix(internal): add missing options
1 parent 15f22cd commit 49c0367

File tree

5 files changed

+674
-11
lines changed

5 files changed

+674
-11
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ private constructor(
1717
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
1818
@get:JvmName("clock") val clock: Clock,
1919
@get:JvmName("baseUrl") val baseUrl: String,
20-
@get:JvmName("apiKey") val apiKey: String,
21-
@get:JvmName("webhookSecret") val webhookSecret: String?,
2220
@get:JvmName("headers") val headers: Headers,
2321
@get:JvmName("queryParams") val queryParams: QueryParams,
2422
@get:JvmName("responseValidation") val responseValidation: Boolean,
2523
@get:JvmName("maxRetries") val maxRetries: Int,
24+
@get:JvmName("apiKey") val apiKey: String,
25+
@get:JvmName("webhookSecret") val webhookSecret: String?,
2626
) {
2727

2828
fun toBuilder() = Builder().from(this)
@@ -39,7 +39,7 @@ private constructor(
3939
class Builder {
4040

4141
private var httpClient: HttpClient? = null
42-
private var jsonMapper: JsonMapper? = null
42+
private var jsonMapper: JsonMapper = jsonMapper()
4343
private var clock: Clock = Clock.systemUTC()
4444
private var baseUrl: String = PRODUCTION_URL
4545
private var headers: Headers.Builder = Headers.builder()
@@ -67,10 +67,10 @@ private constructor(
6767

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

70-
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
71-
7270
fun clock(clock: Clock) = apply { this.clock = clock }
7371

72+
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
73+
7474
fun headers(headers: Headers) = apply {
7575
this.headers.clear()
7676
putAllHeaders(headers)
@@ -81,9 +81,8 @@ private constructor(
8181
putAllHeaders(headers)
8282
}
8383

84-
fun putHeader(name: String, value: String) = apply {
85-
this.headers.getOrPut(name) { mutableListOf() }.add(value)
86-
}
84+
fun putHeader(name: String, value: String) = apply { headers.put(name, value) }
85+
8786
fun putHeaders(name: String, values: Iterable<String>) = apply { headers.put(name, values) }
8887

8988
fun putAllHeaders(headers: Headers) = apply { this.headers.putAll(headers) }
@@ -198,15 +197,15 @@ private constructor(
198197
.idempotencyHeader("Idempotency-Key")
199198
.build()
200199
),
201-
jsonMapper ?: jsonMapper(),
200+
jsonMapper,
202201
clock,
203202
baseUrl,
204-
apiKey!!,
205-
webhookSecret,
206203
headers.build(),
207204
queryParams.build(),
208205
responseValidation,
209206
maxRetries,
207+
apiKey!!,
208+
webhookSecret,
210209
)
211210
}
212211
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.withorb.api.core.http
2+
3+
import com.withorb.api.core.toImmutable
4+
import java.util.TreeMap
5+
6+
class Headers
7+
private constructor(
8+
private val map: Map<String, List<String>>,
9+
@get:JvmName("size") val size: Int
10+
) {
11+
12+
fun isEmpty(): Boolean = map.isEmpty()
13+
14+
fun names(): Set<String> = map.keys
15+
16+
fun values(name: String): List<String> = map[name].orEmpty()
17+
18+
fun toBuilder(): Builder = Builder().putAll(map)
19+
20+
companion object {
21+
22+
@JvmStatic fun builder() = Builder()
23+
}
24+
25+
class Builder {
26+
27+
private val map: MutableMap<String, MutableList<String>> =
28+
TreeMap(String.CASE_INSENSITIVE_ORDER)
29+
private var size: Int = 0
30+
31+
fun put(name: String, value: String) = apply {
32+
map.getOrPut(name) { mutableListOf() }.add(value)
33+
size++
34+
}
35+
36+
fun put(name: String, values: Iterable<String>) = apply { values.forEach { put(name, it) } }
37+
38+
fun putAll(headers: Map<String, Iterable<String>>) = apply { headers.forEach(::put) }
39+
40+
fun putAll(headers: Headers) = apply {
41+
headers.names().forEach { put(it, headers.values(it)) }
42+
}
43+
44+
fun remove(name: String) = apply { size -= map.remove(name).orEmpty().size }
45+
46+
fun removeAll(names: Set<String>) = apply { names.forEach(::remove) }
47+
48+
fun clear() = apply {
49+
map.clear()
50+
size = 0
51+
}
52+
53+
fun replace(name: String, value: String) = apply {
54+
remove(name)
55+
put(name, value)
56+
}
57+
58+
fun replace(name: String, values: Iterable<String>) = apply {
59+
remove(name)
60+
put(name, values)
61+
}
62+
63+
fun replaceAll(headers: Map<String, Iterable<String>>) = apply {
64+
headers.forEach(::replace)
65+
}
66+
67+
fun replaceAll(headers: Headers) = apply {
68+
headers.names().forEach { replace(it, headers.values(it)) }
69+
}
70+
71+
fun build() =
72+
Headers(
73+
map.mapValuesTo(TreeMap(String.CASE_INSENSITIVE_ORDER)) { (_, values) ->
74+
values.toImmutable()
75+
}
76+
.toImmutable(),
77+
size
78+
)
79+
}
80+
81+
override fun hashCode(): Int = map.hashCode()
82+
83+
override fun equals(other: Any?): Boolean {
84+
if (this === other) {
85+
return true
86+
}
87+
88+
return other is Headers && map == other.map
89+
}
90+
91+
override fun toString(): String = "Headers{map=$map}"
92+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.withorb.api.core.http
2+
3+
import com.withorb.api.core.toImmutable
4+
5+
class QueryParams
6+
private constructor(
7+
private val map: Map<String, List<String>>,
8+
@get:JvmName("size") val size: Int
9+
) {
10+
11+
fun isEmpty(): Boolean = map.isEmpty()
12+
13+
fun keys(): Set<String> = map.keys
14+
15+
fun values(key: String): List<String> = map[key].orEmpty()
16+
17+
fun toBuilder(): Builder = Builder().putAll(map)
18+
19+
companion object {
20+
21+
@JvmStatic fun builder() = Builder()
22+
}
23+
24+
class Builder {
25+
26+
private val map: MutableMap<String, MutableList<String>> = mutableMapOf()
27+
private var size: Int = 0
28+
29+
fun put(key: String, value: String) = apply {
30+
map.getOrPut(key) { mutableListOf() }.add(value)
31+
size++
32+
}
33+
34+
fun put(key: String, values: Iterable<String>) = apply { values.forEach { put(key, it) } }
35+
36+
fun putAll(queryParams: Map<String, Iterable<String>>) = apply {
37+
queryParams.forEach(::put)
38+
}
39+
40+
fun putAll(queryParams: QueryParams) = apply {
41+
queryParams.keys().forEach { put(it, queryParams.values(it)) }
42+
}
43+
44+
fun replace(key: String, value: String) = apply {
45+
remove(key)
46+
put(key, value)
47+
}
48+
49+
fun replace(key: String, values: Iterable<String>) = apply {
50+
remove(key)
51+
put(key, values)
52+
}
53+
54+
fun replaceAll(queryParams: Map<String, Iterable<String>>) = apply {
55+
queryParams.forEach(::replace)
56+
}
57+
58+
fun replaceAll(queryParams: QueryParams) = apply {
59+
queryParams.keys().forEach { replace(it, queryParams.values(it)) }
60+
}
61+
62+
fun remove(key: String) = apply { size -= map.remove(key).orEmpty().size }
63+
64+
fun removeAll(keys: Set<String>) = apply { keys.forEach(::remove) }
65+
66+
fun clear() = apply {
67+
map.clear()
68+
size = 0
69+
}
70+
71+
fun build() =
72+
QueryParams(map.mapValues { (_, values) -> values.toImmutable() }.toImmutable(), size)
73+
}
74+
75+
override fun hashCode(): Int = map.hashCode()
76+
77+
override fun equals(other: Any?): Boolean {
78+
if (this === other) {
79+
return true
80+
}
81+
82+
return other is QueryParams && map == other.map
83+
}
84+
85+
override fun toString(): String = "QueryParams{map=$map}"
86+
}

0 commit comments

Comments
 (0)