Skip to content

Commit 744d7a6

Browse files
feat(client): add a withOptions method
1 parent 6172f77 commit 744d7a6

File tree

117 files changed

+1686
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1686
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,21 @@ See this table for the available options:
104104
> Don't create more than one client in the same application. Each client has a connection pool and
105105
> thread pools, which are more efficient to share between requests.
106106
107+
### Modifying configuration
108+
109+
To temporarily use a modified client configuration, while reusing the same connection and thread pools, call `withOptions()` on any client or service:
110+
111+
```java
112+
import com.withorb.api.client.OrbClient;
113+
114+
OrbClient clientWithOptions = client.withOptions(optionsBuilder -> {
115+
optionsBuilder.baseUrl("https://example.com");
116+
optionsBuilder.maxRetries(42);
117+
});
118+
```
119+
120+
The `withOptions()` method does not affect the original client or service.
121+
107122
## Requests and responses
108123

109124
To send a request to the Orb API, build an instance of some `Params` class and pass it to the corresponding client method. When the response is received, it will be deserialized into an instance of a Java class.

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.client
44

5+
import com.withorb.api.core.ClientOptions
56
import com.withorb.api.services.blocking.AlertService
67
import com.withorb.api.services.blocking.BetaService
78
import com.withorb.api.services.blocking.CouponService
@@ -20,6 +21,8 @@ import com.withorb.api.services.blocking.SubscriptionService
2021
import com.withorb.api.services.blocking.TopLevelService
2122
import com.withorb.api.services.blocking.WebhookService
2223

24+
import java.util.function.Consumer
25+
2326
/**
2427
* A client for interacting with the Orb REST API synchronously. You can also switch to asynchronous
2528
* execution via the [async] method.
@@ -49,6 +52,13 @@ interface OrbClient {
4952
*/
5053
fun withRawResponse(): WithRawResponse
5154

55+
/**
56+
* Returns a view of this service with the given option modifications applied.
57+
*
58+
* The original service is not modified.
59+
*/
60+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClient
61+
5262
fun topLevel(): TopLevelService
5363

5464
fun beta(): BetaService
@@ -99,6 +109,13 @@ interface OrbClient {
99109
/** A view of [OrbClient] that provides access to raw HTTP responses for each method. */
100110
interface WithRawResponse {
101111

112+
/**
113+
* Returns a view of this service with the given option modifications applied.
114+
*
115+
* The original service is not modified.
116+
*/
117+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClient.WithRawResponse
118+
102119
fun topLevel(): TopLevelService.WithRawResponse
103120

104121
fun beta(): BetaService.WithRawResponse

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.client
44

5+
import com.withorb.api.core.ClientOptions
56
import com.withorb.api.services.async.AlertServiceAsync
67
import com.withorb.api.services.async.BetaServiceAsync
78
import com.withorb.api.services.async.CouponServiceAsync
@@ -18,6 +19,7 @@ import com.withorb.api.services.async.PriceServiceAsync
1819
import com.withorb.api.services.async.SubscriptionChangeServiceAsync
1920
import com.withorb.api.services.async.SubscriptionServiceAsync
2021
import com.withorb.api.services.async.TopLevelServiceAsync
22+
import java.util.function.Consumer
2123

2224
/**
2325
* A client for interacting with the Orb REST API asynchronously. You can also switch to synchronous
@@ -48,6 +50,13 @@ interface OrbClientAsync {
4850
*/
4951
fun withRawResponse(): WithRawResponse
5052

53+
/**
54+
* Returns a view of this service with the given option modifications applied.
55+
*
56+
* The original service is not modified.
57+
*/
58+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClientAsync
59+
5160
fun topLevel(): TopLevelServiceAsync
5261

5362
fun beta(): BetaServiceAsync
@@ -96,6 +105,13 @@ interface OrbClientAsync {
96105
/** A view of [OrbClientAsync] that provides access to raw HTTP responses for each method. */
97106
interface WithRawResponse {
98107

108+
/**
109+
* Returns a view of this service with the given option modifications applied.
110+
*
111+
* The original service is not modified.
112+
*/
113+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClientAsync.WithRawResponse
114+
99115
fun topLevel(): TopLevelServiceAsync.WithRawResponse
100116

101117
fun beta(): BetaServiceAsync.WithRawResponse

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import com.withorb.api.services.async.SubscriptionServiceAsync
3636
import com.withorb.api.services.async.SubscriptionServiceAsyncImpl
3737
import com.withorb.api.services.async.TopLevelServiceAsync
3838
import com.withorb.api.services.async.TopLevelServiceAsyncImpl
39+
import java.util.function.Consumer
3940

4041
class OrbClientAsyncImpl(private val clientOptions: ClientOptions) : OrbClientAsync {
4142

@@ -116,6 +117,9 @@ class OrbClientAsyncImpl(private val clientOptions: ClientOptions) : OrbClientAs
116117

117118
override fun withRawResponse(): OrbClientAsync.WithRawResponse = withRawResponse
118119

120+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClientAsync =
121+
OrbClientAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
122+
119123
override fun topLevel(): TopLevelServiceAsync = topLevel
120124

121125
override fun beta(): BetaServiceAsync = beta
@@ -219,6 +223,13 @@ class OrbClientAsyncImpl(private val clientOptions: ClientOptions) : OrbClientAs
219223
SubscriptionChangeServiceAsyncImpl.WithRawResponseImpl(clientOptions)
220224
}
221225

226+
override fun withOptions(
227+
modifier: Consumer<ClientOptions.Builder>
228+
): OrbClientAsync.WithRawResponse =
229+
OrbClientAsyncImpl.WithRawResponseImpl(
230+
clientOptions.toBuilder().apply(modifier::accept).build()
231+
)
232+
222233
override fun topLevel(): TopLevelServiceAsync.WithRawResponse = topLevel
223234

224235
override fun beta(): BetaServiceAsync.WithRawResponse = beta

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import com.withorb.api.services.blocking.TopLevelService
3838
import com.withorb.api.services.blocking.TopLevelServiceImpl
3939
import com.withorb.api.services.blocking.WebhookService
4040
import com.withorb.api.services.blocking.WebhookServiceImpl
41+
import java.util.function.Consumer
4142

4243
class OrbClientImpl(private val clientOptions: ClientOptions) : OrbClient {
4344

@@ -108,6 +109,9 @@ class OrbClientImpl(private val clientOptions: ClientOptions) : OrbClient {
108109

109110
override fun withRawResponse(): OrbClient.WithRawResponse = withRawResponse
110111

112+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): OrbClient =
113+
OrbClientImpl(clientOptions.toBuilder().apply(modifier::accept).build())
114+
111115
override fun topLevel(): TopLevelService = topLevel
112116

113117
override fun beta(): BetaService = beta
@@ -211,6 +215,13 @@ class OrbClientImpl(private val clientOptions: ClientOptions) : OrbClient {
211215
SubscriptionChangeServiceImpl.WithRawResponseImpl(clientOptions)
212216
}
213217

218+
override fun withOptions(
219+
modifier: Consumer<ClientOptions.Builder>
220+
): OrbClient.WithRawResponse =
221+
OrbClientImpl.WithRawResponseImpl(
222+
clientOptions.toBuilder().apply(modifier::accept).build()
223+
)
224+
214225
override fun topLevel(): TopLevelService.WithRawResponse = topLevel
215226

216227
override fun beta(): BetaService.WithRawResponse = beta

orb-java-core/src/main/kotlin/com/withorb/api/services/async/AlertServiceAsync.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.services.async
44

5+
import com.withorb.api.core.ClientOptions
56
import com.withorb.api.core.RequestOptions
67
import com.withorb.api.core.http.HttpResponseFor
78
import com.withorb.api.models.Alert
@@ -15,6 +16,7 @@ import com.withorb.api.models.AlertListParams
1516
import com.withorb.api.models.AlertRetrieveParams
1617
import com.withorb.api.models.AlertUpdateParams
1718
import java.util.concurrent.CompletableFuture
19+
import java.util.function.Consumer
1820

1921
interface AlertServiceAsync {
2022

@@ -23,6 +25,13 @@ interface AlertServiceAsync {
2325
*/
2426
fun withRawResponse(): WithRawResponse
2527

28+
/**
29+
* Returns a view of this service with the given option modifications applied.
30+
*
31+
* The original service is not modified.
32+
*/
33+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): AlertServiceAsync
34+
2635
/** This endpoint retrieves an alert by its ID. */
2736
fun retrieve(alertId: String): CompletableFuture<Alert> =
2837
retrieve(alertId, AlertRetrieveParams.none())
@@ -301,6 +310,15 @@ interface AlertServiceAsync {
301310
/** A view of [AlertServiceAsync] that provides access to raw HTTP responses for each method. */
302311
interface WithRawResponse {
303312

313+
/**
314+
* Returns a view of this service with the given option modifications applied.
315+
*
316+
* The original service is not modified.
317+
*/
318+
fun withOptions(
319+
modifier: Consumer<ClientOptions.Builder>
320+
): AlertServiceAsync.WithRawResponse
321+
304322
/**
305323
* Returns a raw HTTP response for `get /alerts/{alert_id}`, but is otherwise the same as
306324
* [AlertServiceAsync.retrieve].

orb-java-core/src/main/kotlin/com/withorb/api/services/async/AlertServiceAsyncImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.withorb.api.models.AlertListParams
2828
import com.withorb.api.models.AlertRetrieveParams
2929
import com.withorb.api.models.AlertUpdateParams
3030
import java.util.concurrent.CompletableFuture
31+
import java.util.function.Consumer
3132
import kotlin.jvm.optionals.getOrNull
3233

3334
class AlertServiceAsyncImpl internal constructor(private val clientOptions: ClientOptions) :
@@ -39,6 +40,9 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
3940

4041
override fun withRawResponse(): AlertServiceAsync.WithRawResponse = withRawResponse
4142

43+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): AlertServiceAsync =
44+
AlertServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
45+
4246
override fun retrieve(
4347
params: AlertRetrieveParams,
4448
requestOptions: RequestOptions,
@@ -100,6 +104,13 @@ class AlertServiceAsyncImpl internal constructor(private val clientOptions: Clie
100104

101105
private val errorHandler: Handler<JsonValue> = errorHandler(clientOptions.jsonMapper)
102106

107+
override fun withOptions(
108+
modifier: Consumer<ClientOptions.Builder>
109+
): AlertServiceAsync.WithRawResponse =
110+
AlertServiceAsyncImpl.WithRawResponseImpl(
111+
clientOptions.toBuilder().apply(modifier::accept).build()
112+
)
113+
103114
private val retrieveHandler: Handler<Alert> =
104115
jsonHandler<Alert>(clientOptions.jsonMapper).withErrorHandler(errorHandler)
105116

orb-java-core/src/main/kotlin/com/withorb/api/services/async/BetaServiceAsync.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.services.async
44

5+
import com.withorb.api.core.ClientOptions
56
import com.withorb.api.core.RequestOptions
67
import com.withorb.api.core.http.HttpResponseFor
78
import com.withorb.api.models.BetaCreatePlanVersionParams
@@ -11,6 +12,7 @@ import com.withorb.api.models.Plan
1112
import com.withorb.api.models.PlanVersion
1213
import com.withorb.api.services.async.beta.ExternalPlanIdServiceAsync
1314
import java.util.concurrent.CompletableFuture
15+
import java.util.function.Consumer
1416

1517
interface BetaServiceAsync {
1618

@@ -19,6 +21,13 @@ interface BetaServiceAsync {
1921
*/
2022
fun withRawResponse(): WithRawResponse
2123

24+
/**
25+
* Returns a view of this service with the given option modifications applied.
26+
*
27+
* The original service is not modified.
28+
*/
29+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): BetaServiceAsync
30+
2231
fun externalPlanId(): ExternalPlanIdServiceAsync
2332

2433
/**
@@ -112,6 +121,13 @@ interface BetaServiceAsync {
112121
/** A view of [BetaServiceAsync] that provides access to raw HTTP responses for each method. */
113122
interface WithRawResponse {
114123

124+
/**
125+
* Returns a view of this service with the given option modifications applied.
126+
*
127+
* The original service is not modified.
128+
*/
129+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): BetaServiceAsync.WithRawResponse
130+
115131
fun externalPlanId(): ExternalPlanIdServiceAsync.WithRawResponse
116132

117133
/**

orb-java-core/src/main/kotlin/com/withorb/api/services/async/BetaServiceAsyncImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.withorb.api.models.PlanVersion
2424
import com.withorb.api.services.async.beta.ExternalPlanIdServiceAsync
2525
import com.withorb.api.services.async.beta.ExternalPlanIdServiceAsyncImpl
2626
import java.util.concurrent.CompletableFuture
27+
import java.util.function.Consumer
2728
import kotlin.jvm.optionals.getOrNull
2829

2930
class BetaServiceAsyncImpl internal constructor(private val clientOptions: ClientOptions) :
@@ -39,6 +40,9 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
3940

4041
override fun withRawResponse(): BetaServiceAsync.WithRawResponse = withRawResponse
4142

43+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): BetaServiceAsync =
44+
BetaServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
45+
4246
override fun externalPlanId(): ExternalPlanIdServiceAsync = externalPlanId
4347

4448
override fun createPlanVersion(
@@ -71,6 +75,13 @@ class BetaServiceAsyncImpl internal constructor(private val clientOptions: Clien
7175
ExternalPlanIdServiceAsyncImpl.WithRawResponseImpl(clientOptions)
7276
}
7377

78+
override fun withOptions(
79+
modifier: Consumer<ClientOptions.Builder>
80+
): BetaServiceAsync.WithRawResponse =
81+
BetaServiceAsyncImpl.WithRawResponseImpl(
82+
clientOptions.toBuilder().apply(modifier::accept).build()
83+
)
84+
7485
override fun externalPlanId(): ExternalPlanIdServiceAsync.WithRawResponse = externalPlanId
7586

7687
private val createPlanVersionHandler: Handler<PlanVersion> =

orb-java-core/src/main/kotlin/com/withorb/api/services/async/CouponServiceAsync.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package com.withorb.api.services.async
44

5+
import com.withorb.api.core.ClientOptions
56
import com.withorb.api.core.RequestOptions
67
import com.withorb.api.core.http.HttpResponseFor
78
import com.withorb.api.models.Coupon
@@ -12,6 +13,7 @@ import com.withorb.api.models.CouponListPageAsync
1213
import com.withorb.api.models.CouponListParams
1314
import com.withorb.api.services.async.coupons.SubscriptionServiceAsync
1415
import java.util.concurrent.CompletableFuture
16+
import java.util.function.Consumer
1517

1618
interface CouponServiceAsync {
1719

@@ -20,6 +22,13 @@ interface CouponServiceAsync {
2022
*/
2123
fun withRawResponse(): WithRawResponse
2224

25+
/**
26+
* Returns a view of this service with the given option modifications applied.
27+
*
28+
* The original service is not modified.
29+
*/
30+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): CouponServiceAsync
31+
2332
fun subscriptions(): SubscriptionServiceAsync
2433

2534
/**
@@ -136,6 +145,15 @@ interface CouponServiceAsync {
136145
*/
137146
interface WithRawResponse {
138147

148+
/**
149+
* Returns a view of this service with the given option modifications applied.
150+
*
151+
* The original service is not modified.
152+
*/
153+
fun withOptions(
154+
modifier: Consumer<ClientOptions.Builder>
155+
): CouponServiceAsync.WithRawResponse
156+
139157
fun subscriptions(): SubscriptionServiceAsync.WithRawResponse
140158

141159
/**

0 commit comments

Comments
 (0)