Skip to content

Commit 7afadee

Browse files
Stainless Botstainless-app[bot]
authored andcommitted
feat(client)!: replace multimaps with custom types (#381)
# Migration - Methods that return headers or query params (e.g. headers from exception classes) previously returned `ListMultimap`, but now return custom `Headers` and `QueryParams` types, respectively, instead. If you were referencing `ListMultimap`, then update the referenced types and the referenced methods, which are almost identical in name, but have some differences - Methods that accept individual headers and query params are unchanged. However, they now have additional overloads for passing the new custom `Headers` and `QueryParams` types
1 parent 029fe55 commit 7afadee

292 files changed

Lines changed: 9339 additions & 6721 deletions

File tree

Some content is hidden

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

lithic-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(":lithic-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"))

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClient.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.lithic.api.client.LithicClient
77
import com.lithic.api.client.LithicClientImpl
88
import com.lithic.api.core.ClientOptions
9+
import com.lithic.api.core.http.Headers
10+
import com.lithic.api.core.http.QueryParams
911
import java.net.Proxy
1012
import java.time.Clock
1113
import java.time.Duration
@@ -38,6 +40,8 @@ class LithicOkHttpClient private constructor() {
3840

3941
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
4042

43+
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
44+
4145
fun headers(headers: Map<String, Iterable<String>>) = apply {
4246
clientOptions.headers(headers)
4347
}
@@ -48,6 +52,8 @@ class LithicOkHttpClient private constructor() {
4852
clientOptions.putHeaders(name, values)
4953
}
5054

55+
fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }
56+
5157
fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
5258
clientOptions.putAllHeaders(headers)
5359
}
@@ -60,6 +66,8 @@ class LithicOkHttpClient private constructor() {
6066
clientOptions.replaceHeaders(name, values)
6167
}
6268

69+
fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }
70+
6371
fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
6472
clientOptions.replaceAllHeaders(headers)
6573
}
@@ -68,6 +76,8 @@ class LithicOkHttpClient private constructor() {
6876

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

79+
fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }
80+
7181
fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
7282
clientOptions.queryParams(queryParams)
7383
}
@@ -80,6 +90,10 @@ class LithicOkHttpClient private constructor() {
8090
clientOptions.putQueryParams(key, values)
8191
}
8292

93+
fun putAllQueryParams(queryParams: QueryParams) = apply {
94+
clientOptions.putAllQueryParams(queryParams)
95+
}
96+
8397
fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
8498
clientOptions.putAllQueryParams(queryParams)
8599
}
@@ -92,6 +106,10 @@ class LithicOkHttpClient private constructor() {
92106
clientOptions.replaceQueryParams(key, values)
93107
}
94108

109+
fun replaceAllQueryParams(queryParams: QueryParams) = apply {
110+
clientOptions.replaceAllQueryParams(queryParams)
111+
}
112+
95113
fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
96114
clientOptions.replaceAllQueryParams(queryParams)
97115
}

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClientAsync.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.lithic.api.client.LithicClientAsync
77
import com.lithic.api.client.LithicClientAsyncImpl
88
import com.lithic.api.core.ClientOptions
9+
import com.lithic.api.core.http.Headers
10+
import com.lithic.api.core.http.QueryParams
911
import java.net.Proxy
1012
import java.time.Clock
1113
import java.time.Duration
@@ -38,6 +40,8 @@ class LithicOkHttpClientAsync private constructor() {
3840

3941
fun clock(clock: Clock) = apply { clientOptions.clock(clock) }
4042

43+
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
44+
4145
fun headers(headers: Map<String, Iterable<String>>) = apply {
4246
clientOptions.headers(headers)
4347
}
@@ -48,6 +52,8 @@ class LithicOkHttpClientAsync private constructor() {
4852
clientOptions.putHeaders(name, values)
4953
}
5054

55+
fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }
56+
5157
fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
5258
clientOptions.putAllHeaders(headers)
5359
}
@@ -60,6 +66,8 @@ class LithicOkHttpClientAsync private constructor() {
6066
clientOptions.replaceHeaders(name, values)
6167
}
6268

69+
fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }
70+
6371
fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
6472
clientOptions.replaceAllHeaders(headers)
6573
}
@@ -68,6 +76,8 @@ class LithicOkHttpClientAsync private constructor() {
6876

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

79+
fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }
80+
7181
fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
7282
clientOptions.queryParams(queryParams)
7383
}
@@ -80,6 +90,10 @@ class LithicOkHttpClientAsync private constructor() {
8090
clientOptions.putQueryParams(key, values)
8191
}
8292

93+
fun putAllQueryParams(queryParams: QueryParams) = apply {
94+
clientOptions.putAllQueryParams(queryParams)
95+
}
96+
8397
fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
8498
clientOptions.putAllQueryParams(queryParams)
8599
}
@@ -92,6 +106,10 @@ class LithicOkHttpClientAsync private constructor() {
92106
clientOptions.replaceQueryParams(key, values)
93107
}
94108

109+
fun replaceAllQueryParams(queryParams: QueryParams) = apply {
110+
clientOptions.replaceAllQueryParams(queryParams)
111+
}
112+
95113
fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
96114
clientOptions.replaceAllQueryParams(queryParams)
97115
}

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/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.lithic.api.client.okhttp
22

3-
import com.google.common.collect.ListMultimap
4-
import com.google.common.collect.MultimapBuilder
53
import com.lithic.api.core.RequestOptions
4+
import com.lithic.api.core.http.Headers
65
import com.lithic.api.core.http.HttpClient
76
import com.lithic.api.core.http.HttpMethod
87
import com.lithic.api.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 {

lithic-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

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

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientAsyncImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ constructor(
2222
) : LithicClientAsync {
2323

2424
private val clientOptionsWithUserAgent =
25-
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
25+
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
2626
else
2727
clientOptions
2828
.toBuilder()
@@ -201,9 +201,9 @@ constructor(
201201
HttpRequest.builder()
202202
.method(HttpMethod.GET)
203203
.addPathSegments("v1", "status")
204-
.putAllQueryParams(clientOptions.queryParams.asMap())
204+
.putAllQueryParams(clientOptions.queryParams)
205205
.replaceAllQueryParams(params.getQueryParams())
206-
.putAllHeaders(clientOptions.headers.asMap())
206+
.putAllHeaders(clientOptions.headers)
207207
.replaceAllHeaders(params.getHeaders())
208208
.build()
209209
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response

lithic-java-core/src/main/kotlin/com/lithic/api/client/LithicClientImpl.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ constructor(
2121
) : LithicClient {
2222

2323
private val clientOptionsWithUserAgent =
24-
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
24+
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
2525
else
2626
clientOptions
2727
.toBuilder()
@@ -185,9 +185,9 @@ constructor(
185185
HttpRequest.builder()
186186
.method(HttpMethod.GET)
187187
.addPathSegments("v1", "status")
188-
.putAllQueryParams(clientOptions.queryParams.asMap())
188+
.putAllQueryParams(clientOptions.queryParams)
189189
.replaceAllQueryParams(params.getQueryParams())
190-
.putAllHeaders(clientOptions.headers.asMap())
190+
.putAllHeaders(clientOptions.headers)
191191
.replaceAllHeaders(params.getHeaders())
192192
.build()
193193
return clientOptions.httpClient.execute(request, requestOptions).let { response ->

0 commit comments

Comments
 (0)