Skip to content

Commit f165926

Browse files
chore(internal): refactor delegating from client to options
1 parent ac4acbe commit f165926

3 files changed

Lines changed: 106 additions & 74 deletions

File tree

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClient.kt

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lithic.api.core.ClientOptions
99
import com.lithic.api.core.Timeout
1010
import com.lithic.api.core.http.Headers
1111
import com.lithic.api.core.http.QueryParams
12+
import com.lithic.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,12 +31,9 @@ class LithicOkHttpClient private constructor() {
3031
class Builder internal constructor() {
3132

3233
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
33-
private var timeout: Timeout = Timeout.default()
3434
private var proxy: Proxy? = null
3535

36-
fun sandbox() = apply { baseUrl(ClientOptions.SANDBOX_URL) }
37-
38-
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
36+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3937

4038
/**
4139
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +54,40 @@ class LithicOkHttpClient private constructor() {
5654

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

57+
fun baseUrl(baseUrl: String?) = apply { clientOptions.baseUrl(baseUrl) }
58+
59+
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
60+
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
61+
62+
fun sandbox() = apply { clientOptions.sandbox() }
63+
64+
fun responseValidation(responseValidation: Boolean) = apply {
65+
clientOptions.responseValidation(responseValidation)
66+
}
67+
68+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
69+
70+
/**
71+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
72+
*
73+
* See [Timeout.request] for more details.
74+
*
75+
* For fine-grained control, pass a [Timeout] object.
76+
*/
77+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
78+
79+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
80+
81+
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
82+
83+
fun webhookSecret(webhookSecret: String?) = apply {
84+
clientOptions.webhookSecret(webhookSecret)
85+
}
86+
87+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
88+
fun webhookSecret(webhookSecret: Optional<String>) =
89+
webhookSecret(webhookSecret.getOrNull())
90+
5991
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
6092

6193
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -136,38 +168,6 @@ class LithicOkHttpClient private constructor() {
136168
clientOptions.removeAllQueryParams(keys)
137169
}
138170

139-
fun timeout(timeout: Timeout) = apply {
140-
clientOptions.timeout(timeout)
141-
this.timeout = timeout
142-
}
143-
144-
/**
145-
* Sets the maximum time allowed for a complete HTTP call, not including retries.
146-
*
147-
* See [Timeout.request] for more details.
148-
*
149-
* For fine-grained control, pass a [Timeout] object.
150-
*/
151-
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
152-
153-
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
154-
155-
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
156-
157-
fun responseValidation(responseValidation: Boolean) = apply {
158-
clientOptions.responseValidation(responseValidation)
159-
}
160-
161-
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
162-
163-
fun webhookSecret(webhookSecret: String?) = apply {
164-
clientOptions.webhookSecret(webhookSecret)
165-
}
166-
167-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
168-
fun webhookSecret(webhookSecret: Optional<String>) =
169-
webhookSecret(webhookSecret.getOrNull())
170-
171171
fun fromEnv() = apply { clientOptions.fromEnv() }
172172

173173
/**
@@ -178,7 +178,9 @@ class LithicOkHttpClient private constructor() {
178178
fun build(): LithicClient =
179179
LithicClientImpl(
180180
clientOptions
181-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
181+
.httpClient(
182+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
183+
)
182184
.build()
183185
)
184186
}

lithic-java-client-okhttp/src/main/kotlin/com/lithic/api/client/okhttp/LithicOkHttpClientAsync.kt

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lithic.api.core.ClientOptions
99
import com.lithic.api.core.Timeout
1010
import com.lithic.api.core.http.Headers
1111
import com.lithic.api.core.http.QueryParams
12+
import com.lithic.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,12 +31,9 @@ class LithicOkHttpClientAsync private constructor() {
3031
class Builder internal constructor() {
3132

3233
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
33-
private var timeout: Timeout = Timeout.default()
3434
private var proxy: Proxy? = null
3535

36-
fun sandbox() = apply { baseUrl(ClientOptions.SANDBOX_URL) }
37-
38-
fun baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
36+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3937

4038
/**
4139
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -56,6 +54,40 @@ class LithicOkHttpClientAsync private constructor() {
5654

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

57+
fun baseUrl(baseUrl: String?) = apply { clientOptions.baseUrl(baseUrl) }
58+
59+
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
60+
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
61+
62+
fun sandbox() = apply { clientOptions.sandbox() }
63+
64+
fun responseValidation(responseValidation: Boolean) = apply {
65+
clientOptions.responseValidation(responseValidation)
66+
}
67+
68+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
69+
70+
/**
71+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
72+
*
73+
* See [Timeout.request] for more details.
74+
*
75+
* For fine-grained control, pass a [Timeout] object.
76+
*/
77+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
78+
79+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
80+
81+
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
82+
83+
fun webhookSecret(webhookSecret: String?) = apply {
84+
clientOptions.webhookSecret(webhookSecret)
85+
}
86+
87+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
88+
fun webhookSecret(webhookSecret: Optional<String>) =
89+
webhookSecret(webhookSecret.getOrNull())
90+
5991
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
6092

6193
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -136,38 +168,6 @@ class LithicOkHttpClientAsync private constructor() {
136168
clientOptions.removeAllQueryParams(keys)
137169
}
138170

139-
fun timeout(timeout: Timeout) = apply {
140-
clientOptions.timeout(timeout)
141-
this.timeout = timeout
142-
}
143-
144-
/**
145-
* Sets the maximum time allowed for a complete HTTP call, not including retries.
146-
*
147-
* See [Timeout.request] for more details.
148-
*
149-
* For fine-grained control, pass a [Timeout] object.
150-
*/
151-
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
152-
153-
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
154-
155-
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
156-
157-
fun responseValidation(responseValidation: Boolean) = apply {
158-
clientOptions.responseValidation(responseValidation)
159-
}
160-
161-
fun apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
162-
163-
fun webhookSecret(webhookSecret: String?) = apply {
164-
clientOptions.webhookSecret(webhookSecret)
165-
}
166-
167-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
168-
fun webhookSecret(webhookSecret: Optional<String>) =
169-
webhookSecret(webhookSecret.getOrNull())
170-
171171
fun fromEnv() = apply { clientOptions.fromEnv() }
172172

173173
/**
@@ -178,7 +178,9 @@ class LithicOkHttpClientAsync private constructor() {
178178
fun build(): LithicClientAsync =
179179
LithicClientAsyncImpl(
180180
clientOptions
181-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
181+
.httpClient(
182+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
183+
)
182184
.build()
183185
)
184186
}

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lithic.api.core.http.PhantomReachableClosingHttpClient
99
import com.lithic.api.core.http.QueryParams
1010
import com.lithic.api.core.http.RetryingHttpClient
1111
import java.time.Clock
12+
import java.time.Duration
1213
import java.util.Optional
1314
import java.util.concurrent.Executor
1415
import java.util.concurrent.Executors
@@ -20,6 +21,13 @@ class ClientOptions
2021
private constructor(
2122
private val originalHttpClient: HttpClient,
2223
@get:JvmName("httpClient") val httpClient: HttpClient,
24+
/**
25+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
26+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
27+
*
28+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee that
29+
* the SDK will work correctly when using an incompatible Jackson version.
30+
*/
2331
@get:JvmName("checkJacksonVersionCompatibility") val checkJacksonVersionCompatibility: Boolean,
2432
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
2533
@get:JvmName("streamHandlerExecutor") val streamHandlerExecutor: Executor,
@@ -104,6 +112,13 @@ private constructor(
104112
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
105113
}
106114

115+
/**
116+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
117+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
118+
*
119+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee
120+
* that the SDK will work correctly when using an incompatible Jackson version.
121+
*/
107122
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
108123
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
109124
}
@@ -121,12 +136,23 @@ private constructor(
121136
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
122137
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
123138

139+
fun sandbox() = baseUrl(SANDBOX_URL)
140+
124141
fun responseValidation(responseValidation: Boolean) = apply {
125142
this.responseValidation = responseValidation
126143
}
127144

128145
fun timeout(timeout: Timeout) = apply { this.timeout = timeout }
129146

147+
/**
148+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
149+
*
150+
* See [Timeout.request] for more details.
151+
*
152+
* For fine-grained control, pass a [Timeout] object.
153+
*/
154+
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
155+
130156
fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }
131157

132158
fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }
@@ -217,6 +243,8 @@ private constructor(
217243

218244
fun removeAllQueryParams(keys: Set<String>) = apply { queryParams.removeAll(keys) }
219245

246+
fun timeout(): Timeout = timeout
247+
220248
fun fromEnv() = apply {
221249
System.getenv("LITHIC_BASE_URL")?.let { baseUrl(it) }
222250
System.getenv("LITHIC_API_KEY")?.let { apiKey(it) }

0 commit comments

Comments
 (0)