Skip to content

Commit c92580f

Browse files
stainless-botStainless Bot
authored andcommitted
feat(client)!: replace multimaps with custom types
chore: unknown commit message
1 parent c6089f1 commit c92580f

File tree

76 files changed

+1658
-1200
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1658
-1200
lines changed

openai-java-client-okhttp/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ plugins {
66
dependencies {
77
api(project(":openai-java-core"))
88

9-
implementation("com.google.guava:guava:33.0.0-jre")
109
implementation("com.squareup.okhttp3:okhttp:4.12.0")
1110

1211
testImplementation(kotlin("test"))

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OkHttpClient.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package com.openai.client.okhttp
22

3-
import com.google.common.collect.ListMultimap
4-
import com.google.common.collect.MultimapBuilder
53
import com.openai.core.RequestOptions
4+
import com.openai.core.http.Headers
65
import com.openai.core.http.HttpClient
76
import com.openai.core.http.HttpMethod
87
import com.openai.core.http.HttpRequest
@@ -16,7 +15,6 @@ import java.time.Duration
1615
import java.util.concurrent.CompletableFuture
1716
import okhttp3.Call
1817
import okhttp3.Callback
19-
import okhttp3.Headers
2018
import okhttp3.HttpUrl
2119
import okhttp3.HttpUrl.Companion.toHttpUrl
2220
import okhttp3.MediaType
@@ -95,7 +93,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
9593
}
9694

9795
val builder = Request.Builder().url(toUrl()).method(method.name, body)
98-
headers.forEach(builder::header)
96+
headers.names().forEach { name ->
97+
headers.values(name).forEach { builder.header(name, it) }
98+
}
9999

100100
return builder.build()
101101
}
@@ -107,7 +107,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
107107

108108
val builder = baseUrl.newBuilder()
109109
pathSegments.forEach(builder::addPathSegment)
110-
queryParams.forEach(builder::addQueryParameter)
110+
queryParams.keys().forEach { key ->
111+
queryParams.values(key).forEach { builder.addQueryParameter(key, it) }
112+
}
111113

112114
return builder.toString()
113115
}
@@ -133,21 +135,18 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
133135
return object : HttpResponse {
134136
override fun statusCode(): Int = code
135137

136-
override fun headers(): ListMultimap<String, String> = headers
138+
override fun headers(): Headers = headers
137139

138140
override fun body(): InputStream = body!!.byteStream()
139141

140142
override fun close() = body!!.close()
141143
}
142144
}
143145

144-
private fun Headers.toHeaders(): ListMultimap<String, String> {
145-
val headers =
146-
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
147-
.arrayListValues()
148-
.build<String, String>()
149-
forEach { pair -> headers.put(pair.first, pair.second) }
150-
return headers
146+
private fun okhttp3.Headers.toHeaders(): Headers {
147+
val headersBuilder = Headers.builder()
148+
forEach { (name, value) -> headersBuilder.put(name, value) }
149+
return headersBuilder.build()
151150
}
152151

153152
companion object {

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.openai.client.OpenAIClient
77
import com.openai.client.OpenAIClientImpl
88
import com.openai.core.ClientOptions
9+
import com.openai.core.http.Headers
10+
import com.openai.core.http.QueryParams
911
import java.net.Proxy
1012
import java.time.Clock
1113
import java.time.Duration
@@ -36,6 +38,8 @@ class OpenAIOkHttpClient private constructor() {
3638

3739
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
3840

41+
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
42+
3943
fun headers(headers: Map<String, Iterable<String>>) = apply {
4044
clientOptions.headers(headers)
4145
}
@@ -46,6 +50,8 @@ class OpenAIOkHttpClient private constructor() {
4650
clientOptions.putHeaders(name, values)
4751
}
4852

53+
fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }
54+
4955
fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
5056
clientOptions.putAllHeaders(headers)
5157
}
@@ -58,6 +64,8 @@ class OpenAIOkHttpClient private constructor() {
5864
clientOptions.replaceHeaders(name, values)
5965
}
6066

67+
fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }
68+
6169
fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
6270
clientOptions.replaceAllHeaders(headers)
6371
}
@@ -66,6 +74,8 @@ class OpenAIOkHttpClient private constructor() {
6674

6775
fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) }
6876

77+
fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }
78+
6979
fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
7080
clientOptions.queryParams(queryParams)
7181
}
@@ -78,6 +88,10 @@ class OpenAIOkHttpClient private constructor() {
7888
clientOptions.putQueryParams(key, values)
7989
}
8090

91+
fun putAllQueryParams(queryParams: QueryParams) = apply {
92+
clientOptions.putAllQueryParams(queryParams)
93+
}
94+
8195
fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
8296
clientOptions.putAllQueryParams(queryParams)
8397
}
@@ -90,6 +104,10 @@ class OpenAIOkHttpClient private constructor() {
90104
clientOptions.replaceQueryParams(key, values)
91105
}
92106

107+
fun replaceAllQueryParams(queryParams: QueryParams) = apply {
108+
clientOptions.replaceAllQueryParams(queryParams)
109+
}
110+
93111
fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
94112
clientOptions.replaceAllQueryParams(queryParams)
95113
}

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.openai.client.OpenAIClientAsync
77
import com.openai.client.OpenAIClientAsyncImpl
88
import com.openai.core.ClientOptions
9+
import com.openai.core.http.Headers
10+
import com.openai.core.http.QueryParams
911
import java.net.Proxy
1012
import java.time.Clock
1113
import java.time.Duration
@@ -36,6 +38,8 @@ class OpenAIOkHttpClientAsync private constructor() {
3638

3739
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
3840

41+
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
42+
3943
fun headers(headers: Map<String, Iterable<String>>) = apply {
4044
clientOptions.headers(headers)
4145
}
@@ -46,6 +50,8 @@ class OpenAIOkHttpClientAsync private constructor() {
4650
clientOptions.putHeaders(name, values)
4751
}
4852

53+
fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }
54+
4955
fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
5056
clientOptions.putAllHeaders(headers)
5157
}
@@ -58,6 +64,8 @@ class OpenAIOkHttpClientAsync private constructor() {
5864
clientOptions.replaceHeaders(name, values)
5965
}
6066

67+
fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }
68+
6169
fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
6270
clientOptions.replaceAllHeaders(headers)
6371
}
@@ -66,6 +74,8 @@ class OpenAIOkHttpClientAsync private constructor() {
6674

6775
fun removeAllHeaders(names: Set<String>) = apply { clientOptions.removeAllHeaders(names) }
6876

77+
fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }
78+
6979
fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
7080
clientOptions.queryParams(queryParams)
7181
}
@@ -78,6 +88,10 @@ class OpenAIOkHttpClientAsync private constructor() {
7888
clientOptions.putQueryParams(key, values)
7989
}
8090

91+
fun putAllQueryParams(queryParams: QueryParams) = apply {
92+
clientOptions.putAllQueryParams(queryParams)
93+
}
94+
8195
fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
8296
clientOptions.putAllQueryParams(queryParams)
8397
}
@@ -90,6 +104,10 @@ class OpenAIOkHttpClientAsync private constructor() {
90104
clientOptions.replaceQueryParams(key, values)
91105
}
92106

107+
fun replaceAllQueryParams(queryParams: QueryParams) = apply {
108+
clientOptions.replaceAllQueryParams(queryParams)
109+
}
110+
93111
fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
94112
clientOptions.replaceAllQueryParams(queryParams)
95113
}

openai-java-core/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ plugins {
66
dependencies {
77
api("com.fasterxml.jackson.core:jackson-core:2.14.3")
88
api("com.fasterxml.jackson.core:jackson-databind:2.14.3")
9-
api("com.google.guava:guava:33.0.0-jre")
109
api("com.google.errorprone:error_prone_annotations:2.33.0")
1110

1211
implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.3")

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientAsyncImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ constructor(
1313
) : OpenAIClientAsync {
1414

1515
private val clientOptionsWithUserAgent =
16-
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
16+
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
1717
else
1818
clientOptions
1919
.toBuilder()

openai-java-core/src/main/kotlin/com/openai/client/OpenAIClientImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ constructor(
1313
) : OpenAIClient {
1414

1515
private val clientOptionsWithUserAgent =
16-
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
16+
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
1717
else
1818
clientOptions
1919
.toBuilder()

0 commit comments

Comments
 (0)