Skip to content

Commit

Permalink
feat(client)!: replace multimaps with custom types (#259)
Browse files Browse the repository at this point in the history
# 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
  • Loading branch information
Stainless Bot committed Nov 7, 2024
1 parent 1b773c6 commit 02ca0bf
Show file tree
Hide file tree
Showing 314 changed files with 10,303 additions and 7,323 deletions.
1 change: 0 additions & 1 deletion modern-treasury-java-client-okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ plugins {
dependencies {
api(project(":modern-treasury-java-core"))

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

testImplementation(kotlin("test"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.json.JsonMapper
import com.moderntreasury.api.client.ModernTreasuryClient
import com.moderntreasury.api.client.ModernTreasuryClientImpl
import com.moderntreasury.api.core.ClientOptions
import com.moderntreasury.api.core.http.Headers
import com.moderntreasury.api.core.http.QueryParams
import java.net.Proxy
import java.time.Clock
import java.time.Duration
Expand Down Expand Up @@ -36,6 +38,8 @@ class ModernTreasuryOkHttpClient private constructor() {

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

fun headers(headers: Headers) = apply { clientOptions.headers(headers) }

fun headers(headers: Map<String, Iterable<String>>) = apply {
clientOptions.headers(headers)
}
Expand All @@ -46,6 +50,8 @@ class ModernTreasuryOkHttpClient private constructor() {
clientOptions.putHeaders(name, values)
}

fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }

fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.putAllHeaders(headers)
}
Expand All @@ -58,6 +64,8 @@ class ModernTreasuryOkHttpClient private constructor() {
clientOptions.replaceHeaders(name, values)
}

fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }

fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllHeaders(headers)
}
Expand All @@ -66,6 +74,8 @@ class ModernTreasuryOkHttpClient private constructor() {

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

fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }

fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.queryParams(queryParams)
}
Expand All @@ -78,6 +88,10 @@ class ModernTreasuryOkHttpClient private constructor() {
clientOptions.putQueryParams(key, values)
}

fun putAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.putAllQueryParams(queryParams)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.putAllQueryParams(queryParams)
}
Expand All @@ -90,6 +104,10 @@ class ModernTreasuryOkHttpClient private constructor() {
clientOptions.replaceQueryParams(key, values)
}

fun replaceAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}

fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.json.JsonMapper
import com.moderntreasury.api.client.ModernTreasuryClientAsync
import com.moderntreasury.api.client.ModernTreasuryClientAsyncImpl
import com.moderntreasury.api.core.ClientOptions
import com.moderntreasury.api.core.http.Headers
import com.moderntreasury.api.core.http.QueryParams
import java.net.Proxy
import java.time.Clock
import java.time.Duration
Expand Down Expand Up @@ -36,6 +38,8 @@ class ModernTreasuryOkHttpClientAsync private constructor() {

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

fun headers(headers: Headers) = apply { clientOptions.headers(headers) }

fun headers(headers: Map<String, Iterable<String>>) = apply {
clientOptions.headers(headers)
}
Expand All @@ -46,6 +50,8 @@ class ModernTreasuryOkHttpClientAsync private constructor() {
clientOptions.putHeaders(name, values)
}

fun putAllHeaders(headers: Headers) = apply { clientOptions.putAllHeaders(headers) }

fun putAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.putAllHeaders(headers)
}
Expand All @@ -58,6 +64,8 @@ class ModernTreasuryOkHttpClientAsync private constructor() {
clientOptions.replaceHeaders(name, values)
}

fun replaceAllHeaders(headers: Headers) = apply { clientOptions.replaceAllHeaders(headers) }

fun replaceAllHeaders(headers: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllHeaders(headers)
}
Expand All @@ -66,6 +74,8 @@ class ModernTreasuryOkHttpClientAsync private constructor() {

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

fun queryParams(queryParams: QueryParams) = apply { clientOptions.queryParams(queryParams) }

fun queryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.queryParams(queryParams)
}
Expand All @@ -78,6 +88,10 @@ class ModernTreasuryOkHttpClientAsync private constructor() {
clientOptions.putQueryParams(key, values)
}

fun putAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.putAllQueryParams(queryParams)
}

fun putAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.putAllQueryParams(queryParams)
}
Expand All @@ -90,6 +104,10 @@ class ModernTreasuryOkHttpClientAsync private constructor() {
clientOptions.replaceQueryParams(key, values)
}

fun replaceAllQueryParams(queryParams: QueryParams) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}

fun replaceAllQueryParams(queryParams: Map<String, Iterable<String>>) = apply {
clientOptions.replaceAllQueryParams(queryParams)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.moderntreasury.api.client.okhttp

import com.google.common.collect.ListMultimap
import com.google.common.collect.MultimapBuilder
import com.moderntreasury.api.core.RequestOptions
import com.moderntreasury.api.core.http.Headers
import com.moderntreasury.api.core.http.HttpClient
import com.moderntreasury.api.core.http.HttpMethod
import com.moderntreasury.api.core.http.HttpRequest
Expand All @@ -16,7 +15,6 @@ import java.time.Duration
import java.util.concurrent.CompletableFuture
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType
Expand Down Expand Up @@ -95,7 +93,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
}

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

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

val builder = baseUrl.newBuilder()
pathSegments.forEach(builder::addPathSegment)
queryParams.forEach(builder::addQueryParameter)
queryParams.keys().forEach { key ->
queryParams.values(key).forEach { builder.addQueryParameter(key, it) }
}

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

override fun headers(): ListMultimap<String, String> = headers
override fun headers(): Headers = headers

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

override fun close() = body!!.close()
}
}

private fun Headers.toHeaders(): ListMultimap<String, String> {
val headers =
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
.arrayListValues()
.build<String, String>()
forEach { pair -> headers.put(pair.first, pair.second) }
return headers
private fun okhttp3.Headers.toHeaders(): Headers {
val headersBuilder = Headers.builder()
forEach { (name, value) -> headersBuilder.put(name, value) }
return headersBuilder.build()
}

companion object {
Expand Down
2 changes: 1 addition & 1 deletion modern-treasury-java-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ plugins {
dependencies {
api("com.fasterxml.jackson.core:jackson-core:2.14.3")
api("com.fasterxml.jackson.core:jackson-databind:2.14.3")
api("com.google.guava:guava:33.0.0-jre")

implementation("com.fasterxml.jackson.core:jackson-annotations:2.14.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.14.3")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.3")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.14.3")
implementation("org.apache.httpcomponents.core5:httpcore5:5.2.4")
implementation("org.apache.httpcomponents.client5:httpclient5:5.3.1")
implementation("com.google.guava:guava:33.0.0-jre")

testImplementation(kotlin("test"))
testImplementation(project(":modern-treasury-java-client-okhttp"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ constructor(
) : ModernTreasuryClientAsync {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
Expand Down Expand Up @@ -284,9 +284,9 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("api", "ping")
.putAllQueryParams(clientOptions.queryParams.asMap())
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.putAllHeaders(clientOptions.headers)
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.executeAsync(request, requestOptions).thenApply { response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constructor(
) : ModernTreasuryClient {

private val clientOptionsWithUserAgent =
if (clientOptions.headers.containsKey("User-Agent")) clientOptions
if (clientOptions.headers.names().contains("User-Agent")) clientOptions
else
clientOptions
.toBuilder()
Expand Down Expand Up @@ -268,9 +268,9 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("api", "ping")
.putAllQueryParams(clientOptions.queryParams.asMap())
.putAllQueryParams(clientOptions.queryParams)
.replaceAllQueryParams(params.getQueryParams())
.putAllHeaders(clientOptions.headers.asMap())
.putAllHeaders(clientOptions.headers)
.replaceAllHeaders(params.getHeaders())
.build()
return clientOptions.httpClient.execute(request, requestOptions).let { response ->
Expand Down
Loading

0 comments on commit 02ca0bf

Please sign in to comment.