Skip to content

Commit 9b8b51a

Browse files
chore(internal): refactor delegating from client to options
1 parent e8386f8 commit 9b8b51a

File tree

3 files changed

+100
-70
lines changed

3 files changed

+100
-70
lines changed

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClient.kt

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.withorb.api.core.ClientOptions
99
import com.withorb.api.core.Timeout
1010
import com.withorb.api.core.http.Headers
1111
import com.withorb.api.core.http.QueryParams
12+
import com.withorb.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,10 +31,9 @@ class OrbOkHttpClient 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,38 @@ class OrbOkHttpClient 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 apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
80+
81+
fun webhookSecret(webhookSecret: String?) = apply {
82+
clientOptions.webhookSecret(webhookSecret)
83+
}
84+
85+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
86+
fun webhookSecret(webhookSecret: Optional<String>) =
87+
webhookSecret(webhookSecret.getOrNull())
88+
5789
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
5890

5991
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -134,38 +166,6 @@ class OrbOkHttpClient private constructor() {
134166
clientOptions.removeAllQueryParams(keys)
135167
}
136168

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 apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
160-
161-
fun webhookSecret(webhookSecret: String?) = apply {
162-
clientOptions.webhookSecret(webhookSecret)
163-
}
164-
165-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
166-
fun webhookSecret(webhookSecret: Optional<String>) =
167-
webhookSecret(webhookSecret.getOrNull())
168-
169169
fun fromEnv() = apply { clientOptions.fromEnv() }
170170

171171
/**
@@ -176,7 +176,9 @@ class OrbOkHttpClient private constructor() {
176176
fun build(): OrbClient =
177177
OrbClientImpl(
178178
clientOptions
179-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
179+
.httpClient(
180+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
181+
)
180182
.build()
181183
)
182184
}

orb-java-client-okhttp/src/main/kotlin/com/withorb/api/client/okhttp/OrbOkHttpClientAsync.kt

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.withorb.api.core.ClientOptions
99
import com.withorb.api.core.Timeout
1010
import com.withorb.api.core.http.Headers
1111
import com.withorb.api.core.http.QueryParams
12+
import com.withorb.api.core.jsonMapper
1213
import java.net.Proxy
1314
import java.time.Clock
1415
import java.time.Duration
@@ -30,10 +31,9 @@ class OrbOkHttpClientAsync 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,38 @@ class OrbOkHttpClientAsync 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 apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
80+
81+
fun webhookSecret(webhookSecret: String?) = apply {
82+
clientOptions.webhookSecret(webhookSecret)
83+
}
84+
85+
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
86+
fun webhookSecret(webhookSecret: Optional<String>) =
87+
webhookSecret(webhookSecret.getOrNull())
88+
5789
fun headers(headers: Headers) = apply { clientOptions.headers(headers) }
5890

5991
fun headers(headers: Map<String, Iterable<String>>) = apply {
@@ -134,38 +166,6 @@ class OrbOkHttpClientAsync private constructor() {
134166
clientOptions.removeAllQueryParams(keys)
135167
}
136168

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 apiKey(apiKey: String) = apply { clientOptions.apiKey(apiKey) }
160-
161-
fun webhookSecret(webhookSecret: String?) = apply {
162-
clientOptions.webhookSecret(webhookSecret)
163-
}
164-
165-
/** Alias for calling [Builder.webhookSecret] with `webhookSecret.orElse(null)`. */
166-
fun webhookSecret(webhookSecret: Optional<String>) =
167-
webhookSecret(webhookSecret.getOrNull())
168-
169169
fun fromEnv() = apply { clientOptions.fromEnv() }
170170

171171
/**
@@ -176,7 +176,9 @@ class OrbOkHttpClientAsync private constructor() {
176176
fun build(): OrbClientAsync =
177177
OrbClientAsyncImpl(
178178
clientOptions
179-
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
179+
.httpClient(
180+
OkHttpClient.builder().timeout(clientOptions.timeout()).proxy(proxy).build()
181+
)
180182
.build()
181183
)
182184
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.withorb.api.core.http.PhantomReachableClosingHttpClient
99
import com.withorb.api.core.http.QueryParams
1010
import com.withorb.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,
@@ -102,6 +110,13 @@ private constructor(
102110
this.httpClient = PhantomReachableClosingHttpClient(httpClient)
103111
}
104112

113+
/**
114+
* Whether to throw an exception if any of the Jackson versions detected at runtime are
115+
* incompatible with the SDK's minimum supported Jackson version (2.13.4).
116+
*
117+
* Defaults to true. Use extreme caution when disabling this option. There is no guarantee
118+
* that the SDK will work correctly when using an incompatible Jackson version.
119+
*/
105120
fun checkJacksonVersionCompatibility(checkJacksonVersionCompatibility: Boolean) = apply {
106121
this.checkJacksonVersionCompatibility = checkJacksonVersionCompatibility
107122
}
@@ -125,6 +140,15 @@ private constructor(
125140

126141
fun timeout(timeout: Timeout) = apply { this.timeout = timeout }
127142

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

130154
fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }
@@ -215,6 +239,8 @@ private constructor(
215239

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

242+
fun timeout(): Timeout = timeout
243+
218244
fun fromEnv() = apply {
219245
System.getenv("ORB_BASE_URL")?.let { baseUrl(it) }
220246
System.getenv("ORB_API_KEY")?.let { apiKey(it) }

0 commit comments

Comments
 (0)