Skip to content

Commit e9e1c40

Browse files
chore(internal): refactor delegating from client to options
1 parent 532d180 commit e9e1c40

File tree

3 files changed

+126
-96
lines changed

3 files changed

+126
-96
lines changed

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClient.kt

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.tryfinch.api.core.ClientOptions
99
import com.tryfinch.api.core.Timeout
1010
import com.tryfinch.api.core.http.Headers
1111
import com.tryfinch.api.core.http.QueryParams
12+
import com.tryfinch.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,10 +31,9 @@ class FinchOkHttpClient 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 baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
36+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3737

3838
/**
3939
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -54,6 +54,51 @@ class FinchOkHttpClient private constructor() {
5454

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

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 responseValidation(responseValidation: Boolean) = apply {
63+
clientOptions.responseValidation(responseValidation)
64+
}
65+
66+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
67+
68+
/**
69+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
70+
*
71+
* See [Timeout.request] for more details.
72+
*
73+
* For fine-grained control, pass a [Timeout] object.
74+
*/
75+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
76+
77+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
78+
79+
fun accessToken(accessToken: String?) = apply { clientOptions.accessToken(accessToken) }
80+
81+
/** Alias for calling [Builder.accessToken] with `accessToken.orElse(null)`. */
82+
fun accessToken(accessToken: Optional<String>) = accessToken(accessToken.getOrNull())
83+
84+
fun clientId(clientId: String?) = apply { clientOptions.clientId(clientId) }
85+
86+
/** Alias for calling [Builder.clientId] with `clientId.orElse(null)`. */
87+
fun clientId(clientId: Optional<String>) = clientId(clientId.getOrNull())
88+
89+
fun clientSecret(clientSecret: String?) = apply { clientOptions.clientSecret(clientSecret) }
90+
91+
/** Alias for calling [Builder.clientSecret] with `clientSecret.orElse(null)`. */
92+
fun clientSecret(clientSecret: Optional<String>) = clientSecret(clientSecret.getOrNull())
93+
94+
fun webhookSecret(webhookSecret: String?) = apply {
95+
clientOptions.webhookSecret(webhookSecret)
96+
}
97+
98+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
99+
fun webhookSecret(webhookSecret: Optional<String>) =
100+
webhookSecret(webhookSecret.getOrNull())
101+
57102
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
58103

59104
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -134,51 +179,6 @@ class FinchOkHttpClient private constructor() {
134179
clientOptions.removeAllQueryParams(keys)
135180
}
136181

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

184184
/**
@@ -189,7 +189,9 @@ class FinchOkHttpClient private constructor() {
189189
fun build(): FinchClient =
190190
FinchClientImpl(
191191
clientOptions
192-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
192+
.httpClient(
193+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
194+
)
193195
.build()
194196
)
195197
}

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/FinchOkHttpClientAsync.kt

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.tryfinch.api.core.ClientOptions
99
import com.tryfinch.api.core.Timeout
1010
import com.tryfinch.api.core.http.Headers
1111
import com.tryfinch.api.core.http.QueryParams
12+
import com.tryfinch.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,10 +31,9 @@ class FinchOkHttpClientAsync 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 baseUrl(baseUrl: String) = apply { clientOptions.baseUrl(baseUrl) }
36+
fun proxy(proxy: Proxy) = apply { this.proxy = proxy }
3737

3838
/**
3939
* Whether to throw an exception if any of the Jackson versions detected at runtime are
@@ -54,6 +54,51 @@ class FinchOkHttpClientAsync private constructor() {
5454

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

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 responseValidation(responseValidation: Boolean) = apply {
63+
clientOptions.responseValidation(responseValidation)
64+
}
65+
66+
fun timeout(timeout: Timeout) = apply { clientOptions.timeout(timeout) }
67+
68+
/**
69+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
70+
*
71+
* See [Timeout.request] for more details.
72+
*
73+
* For fine-grained control, pass a [Timeout] object.
74+
*/
75+
fun timeout(timeout: Duration) = apply { clientOptions.timeout(timeout) }
76+
77+
fun maxRetries(maxRetries: Int) = apply { clientOptions.maxRetries(maxRetries) }
78+
79+
fun accessToken(accessToken: String?) = apply { clientOptions.accessToken(accessToken) }
80+
81+
/** Alias for calling [Builder.accessToken] with `accessToken.orElse(null)`. */
82+
fun accessToken(accessToken: Optional<String>) = accessToken(accessToken.getOrNull())
83+
84+
fun clientId(clientId: String?) = apply { clientOptions.clientId(clientId) }
85+
86+
/** Alias for calling [Builder.clientId] with `clientId.orElse(null)`. */
87+
fun clientId(clientId: Optional<String>) = clientId(clientId.getOrNull())
88+
89+
fun clientSecret(clientSecret: String?) = apply { clientOptions.clientSecret(clientSecret) }
90+
91+
/** Alias for calling [Builder.clientSecret] with `clientSecret.orElse(null)`. */
92+
fun clientSecret(clientSecret: Optional<String>) = clientSecret(clientSecret.getOrNull())
93+
94+
fun webhookSecret(webhookSecret: String?) = apply {
95+
clientOptions.webhookSecret(webhookSecret)
96+
}
97+
98+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
99+
fun webhookSecret(webhookSecret: Optional<String>) =
100+
webhookSecret(webhookSecret.getOrNull())
101+
57102
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
58103

59104
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -134,51 +179,6 @@ class FinchOkHttpClientAsync private constructor() {
134179
clientOptions.removeAllQueryParams(keys)
135180
}
136181

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

184184
/**
@@ -189,7 +189,9 @@ class FinchOkHttpClientAsync private constructor() {
189189
fun build(): FinchClientAsync =
190190
FinchClientAsyncImpl(
191191
clientOptions
192-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
192+
.httpClient(
193+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
194+
)
193195
.build()
194196
)
195197
}

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

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

125+
/**
126+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
127+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
128+
*
129+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee
130+
* that the SDK will work correctly when using an incompatible Jackson version.
131+
*/
117132
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
118133
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
119134
}
@@ -137,6 +152,15 @@ private constructor(
137152

138153
fun timeout(timeout: Timeout) = apply { this.timeout = timeout }
139154

155+
/**
156+
* Sets the maximum time allowed for a complete HTTP call, not including retries.
157+
*
158+
* See [Timeout.request] for more details.
159+
*
160+
* For fine-grained control, pass a [Timeout] object.
161+
*/
162+
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
163+
140164
fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }
141165

142166
fun accessToken(accessToken: String?) = apply { this.accessToken = accessToken }
@@ -240,6 +264,8 @@ private constructor(
240264

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

267+
fun timeout(): Timeout = timeout
268+
243269
fun fromEnv() = apply {
244270
System.getenv("FINCH_BASE_URL")?.let { baseUrl(it) }
245271
System.getenv("FINCH_CLIENT_ID")?.let { clientId(it) }

0 commit comments

Comments
 (0)