Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client)!: replace multimaps with custom types #370

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lithic-kotlin-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(":lithic-kotlin-core"))

implementation("com.google.guava:guava:33.0.0-jre")
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")

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.lithic.api.client.LithicClient
import com.lithic.api.client.LithicClientImpl
import com.lithic.api.core.ClientOptions
import com.lithic.api.core.http.Headers
import com.lithic.api.core.http.QueryParams
import java.net.Proxy
import java.time.Clock
import java.time.Duration
Expand Down Expand Up @@ -38,6 +40,8 @@ class LithicOkHttpClient 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 @@ -48,6 +52,8 @@ class LithicOkHttpClient 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 @@ -60,6 +66,8 @@ class LithicOkHttpClient 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 @@ -68,6 +76,8 @@ class LithicOkHttpClient 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 @@ -80,6 +90,10 @@ class LithicOkHttpClient 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 @@ -92,6 +106,10 @@ class LithicOkHttpClient 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.lithic.api.client.LithicClientAsync
import com.lithic.api.client.LithicClientAsyncImpl
import com.lithic.api.core.ClientOptions
import com.lithic.api.core.http.Headers
import com.lithic.api.core.http.QueryParams
import java.net.Proxy
import java.time.Clock
import java.time.Duration
Expand Down Expand Up @@ -38,6 +40,8 @@ class LithicOkHttpClientAsync 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 @@ -48,6 +52,8 @@ class LithicOkHttpClientAsync 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 @@ -60,6 +66,8 @@ class LithicOkHttpClientAsync 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 @@ -68,6 +76,8 @@ class LithicOkHttpClientAsync 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 @@ -80,6 +90,10 @@ class LithicOkHttpClientAsync 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 @@ -92,6 +106,10 @@ class LithicOkHttpClientAsync 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.lithic.api.client.okhttp

import com.google.common.collect.ListMultimap
import com.google.common.collect.MultimapBuilder
import com.lithic.api.core.RequestOptions
import com.lithic.api.core.http.Headers
import com.lithic.api.core.http.HttpClient
import com.lithic.api.core.http.HttpMethod
import com.lithic.api.core.http.HttpRequest
Expand All @@ -16,7 +15,6 @@ import java.time.Duration
import kotlinx.coroutines.suspendCancellableCoroutine
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Headers
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.MediaType
Expand Down Expand Up @@ -86,7 +84,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 @@ -98,7 +98,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 @@ -124,21 +126,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
1 change: 0 additions & 1 deletion lithic-kotlin-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constructor(
) : LithicClientAsync {

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 @@ -196,9 +196,9 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "status")
.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).let { response ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ constructor(
) : LithicClient {

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 @@ -181,9 +181,9 @@ constructor(
HttpRequest.builder()
.method(HttpMethod.GET)
.addPathSegments("v1", "status")
.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