Skip to content

Commit 8555a12

Browse files
feat(client): add {QueryParams,Headers}#put(String, JsonValue) methods
1 parent 3a40fb2 commit 8555a12

File tree

2 files changed

+53
-9
lines changed

2 files changed

+53
-9
lines changed

openai-java-core/src/main/kotlin/com/openai/core/http/Headers.kt

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1+
// File generated from our OpenAPI spec by Stainless.
2+
13
package com.openai.core.http
24

5+
import com.openai.core.JsonArray
6+
import com.openai.core.JsonBoolean
7+
import com.openai.core.JsonMissing
8+
import com.openai.core.JsonNull
9+
import com.openai.core.JsonNumber
10+
import com.openai.core.JsonObject
11+
import com.openai.core.JsonString
12+
import com.openai.core.JsonValue
313
import com.openai.core.toImmutable
414
import java.util.TreeMap
515

@@ -28,6 +38,19 @@ private constructor(
2838
TreeMap(String.CASE_INSENSITIVE_ORDER)
2939
private var size: Int = 0
3040

41+
fun put(name: String, value: JsonValue): Builder = apply {
42+
when (value) {
43+
is JsonMissing,
44+
is JsonNull -> {}
45+
is JsonBoolean -> put(name, value.value.toString())
46+
is JsonNumber -> put(name, value.value.toString())
47+
is JsonString -> put(name, value.value)
48+
is JsonArray -> value.values.forEach { put(name, it) }
49+
is JsonObject ->
50+
value.values.forEach { (nestedName, value) -> put("$name.$nestedName", value) }
51+
}
52+
}
53+
3154
fun put(name: String, value: String) = apply {
3255
map.getOrPut(name) { mutableListOf() }.add(value)
3356
size++
@@ -41,15 +64,6 @@ private constructor(
4164
headers.names().forEach { put(it, headers.values(it)) }
4265
}
4366

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-
5367
fun replace(name: String, value: String) = apply {
5468
remove(name)
5569
put(name, value)
@@ -68,6 +82,15 @@ private constructor(
6882
headers.names().forEach { replace(it, headers.values(it)) }
6983
}
7084

85+
fun remove(name: String) = apply { size -= map.remove(name).orEmpty().size }
86+
87+
fun removeAll(names: Set<String>) = apply { names.forEach(::remove) }
88+
89+
fun clear() = apply {
90+
map.clear()
91+
size = 0
92+
}
93+
7194
fun build() =
7295
Headers(
7396
map.mapValuesTo(TreeMap(String.CASE_INSENSITIVE_ORDER)) { (_, values) ->

openai-java-core/src/main/kotlin/com/openai/core/http/QueryParams.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
package com.openai.core.http
44

5+
import com.openai.core.JsonArray
6+
import com.openai.core.JsonBoolean
7+
import com.openai.core.JsonMissing
8+
import com.openai.core.JsonNull
9+
import com.openai.core.JsonNumber
10+
import com.openai.core.JsonObject
11+
import com.openai.core.JsonString
12+
import com.openai.core.JsonValue
513
import com.openai.core.toImmutable
614

715
class QueryParams
@@ -28,6 +36,19 @@ private constructor(
2836
private val map: MutableMap<String, MutableList<String>> = mutableMapOf()
2937
private var size: Int = 0
3038

39+
fun put(key: String, value: JsonValue): Builder = apply {
40+
when (value) {
41+
is JsonMissing,
42+
is JsonNull -> {}
43+
is JsonBoolean -> put(key, value.value.toString())
44+
is JsonNumber -> put(key, value.value.toString())
45+
is JsonString -> put(key, value.value)
46+
is JsonArray -> value.values.forEach { put("$key[]", it) }
47+
is JsonObject ->
48+
value.values.forEach { (nestedKey, value) -> put("$key[$nestedKey]", value) }
49+
}
50+
}
51+
3152
fun put(key: String, value: String) = apply {
3253
map.getOrPut(key) { mutableListOf() }.add(value)
3354
size++

0 commit comments

Comments
 (0)