Skip to content

Commit 9ebe803

Browse files
feat(client): add a withOptions method
1 parent 710b626 commit 9ebe803

Some content is hidden

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

53 files changed

+751
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,21 @@ See this table for the available options:
122122
> Don't create more than one client in the same application. Each client has a connection pool and
123123
> thread pools, which are more efficient to share between requests.
124124
125+
### Modifying configuration
126+
127+
To temporarily use a modified client configuration, while reusing the same connection and thread pools, call `withOptions()` on any client or service:
128+
129+
```java
130+
import com.openlayer.api.client.OpenlayerClient;
131+
132+
OpenlayerClient clientWithOptions = client.withOptions(optionsBuilder -> {
133+
optionsBuilder.baseUrl("https://example.com");
134+
optionsBuilder.maxRetries(42);
135+
});
136+
```
137+
138+
The `withOptions()` method does not affect the original client or service.
139+
125140
## Requests and responses
126141

127142
To send a request to the Openlayer 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.

openlayer-java-core/src/main/kotlin/com/openlayer/api/client/OpenlayerClient.kt

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

33
package com.openlayer.api.client
44

5+
import com.openlayer.api.core.ClientOptions
56
import com.openlayer.api.services.blocking.CommitService
67
import com.openlayer.api.services.blocking.InferencePipelineService
78
import com.openlayer.api.services.blocking.ProjectService
89
import com.openlayer.api.services.blocking.StorageService
10+
import java.util.function.Consumer
911

1012
/**
1113
* A client for interacting with the Openlayer REST API synchronously. You can also switch to
@@ -36,6 +38,13 @@ interface OpenlayerClient {
3638
*/
3739
fun withRawResponse(): WithRawResponse
3840

41+
/**
42+
* Returns a view of this service with the given option modifications applied.
43+
*
44+
* The original service is not modified.
45+
*/
46+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OpenlayerClient
47+
3948
fun projects(): ProjectService
4049

4150
fun commits(): CommitService
@@ -60,6 +69,13 @@ interface OpenlayerClient {
6069
/** A view of [OpenlayerClient] that provides access to raw HTTP responses for each method. */
6170
interface WithRawResponse {
6271

72+
/**
73+
* Returns a view of this service with the given option modifications applied.
74+
*
75+
* The original service is not modified.
76+
*/
77+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OpenlayerClient.WithRawResponse
78+
6379
fun projects(): ProjectService.WithRawResponse
6480

6581
fun commits(): CommitService.WithRawResponse

openlayer-java-core/src/main/kotlin/com/openlayer/api/client/OpenlayerClientAsync.kt

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

33
package com.openlayer.api.client
44

5+
import com.openlayer.api.core.ClientOptions
56
import com.openlayer.api.services.async.CommitServiceAsync
67
import com.openlayer.api.services.async.InferencePipelineServiceAsync
78
import com.openlayer.api.services.async.ProjectServiceAsync
89
import com.openlayer.api.services.async.StorageServiceAsync
10+
import java.util.function.Consumer
911

1012
/**
1113
* A client for interacting with the Openlayer REST API asynchronously. You can also switch to
@@ -36,6 +38,13 @@ interface OpenlayerClientAsync {
3638
*/
3739
fun withRawResponse(): WithRawResponse
3840

41+
/**
42+
* Returns a view of this service with the given option modifications applied.
43+
*
44+
* The original service is not modified.
45+
*/
46+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): OpenlayerClientAsync
47+
3948
fun projects(): ProjectServiceAsync
4049

4150
fun commits(): CommitServiceAsync
@@ -62,6 +71,15 @@ interface OpenlayerClientAsync {
6271
*/
6372
interface WithRawResponse {
6473

74+
/**
75+
* Returns a view of this service with the given option modifications applied.
76+
*
77+
* The original service is not modified.
78+
*/
79+
fun withOptions(
80+
modifier: Consumer<ClientOptions.Builder>
81+
): OpenlayerClientAsync.WithRawResponse
82+
6583
fun projects(): ProjectServiceAsync.WithRawResponse
6684

6785
fun commits(): CommitServiceAsync.WithRawResponse

openlayer-java-core/src/main/kotlin/com/openlayer/api/client/OpenlayerClientAsyncImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.openlayer.api.services.async.ProjectServiceAsync
1212
import com.openlayer.api.services.async.ProjectServiceAsyncImpl
1313
import com.openlayer.api.services.async.StorageServiceAsync
1414
import com.openlayer.api.services.async.StorageServiceAsyncImpl
15+
import java.util.function.Consumer
1516

1617
class OpenlayerClientAsyncImpl(private val clientOptions: ClientOptions) : OpenlayerClientAsync {
1718

@@ -50,6 +51,9 @@ class OpenlayerClientAsyncImpl(private val clientOptions: ClientOptions) : Openl
5051

5152
override fun withRawResponse(): OpenlayerClientAsync.WithRawResponse = withRawResponse
5253

54+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): OpenlayerClientAsync =
55+
OpenlayerClientAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
56+
5357
override fun projects(): ProjectServiceAsync = projects
5458

5559
override fun commits(): CommitServiceAsync = commits
@@ -79,6 +83,13 @@ class OpenlayerClientAsyncImpl(private val clientOptions: ClientOptions) : Openl
7983
StorageServiceAsyncImpl.WithRawResponseImpl(clientOptions)
8084
}
8185

86+
override fun withOptions(
87+
modifier: Consumer<ClientOptions.Builder>
88+
): OpenlayerClientAsync.WithRawResponse =
89+
OpenlayerClientAsyncImpl.WithRawResponseImpl(
90+
clientOptions.toBuilder().apply(modifier::accept).build()
91+
)
92+
8293
override fun projects(): ProjectServiceAsync.WithRawResponse = projects
8394

8495
override fun commits(): CommitServiceAsync.WithRawResponse = commits

openlayer-java-core/src/main/kotlin/com/openlayer/api/client/OpenlayerClientImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.openlayer.api.services.blocking.ProjectService
1212
import com.openlayer.api.services.blocking.ProjectServiceImpl
1313
import com.openlayer.api.services.blocking.StorageService
1414
import com.openlayer.api.services.blocking.StorageServiceImpl
15+
import java.util.function.Consumer
1516

1617
class OpenlayerClientImpl(private val clientOptions: ClientOptions) : OpenlayerClient {
1718

@@ -44,6 +45,9 @@ class OpenlayerClientImpl(private val clientOptions: ClientOptions) : OpenlayerC
4445

4546
override fun withRawResponse(): OpenlayerClient.WithRawResponse = withRawResponse
4647

48+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): OpenlayerClient =
49+
OpenlayerClientImpl(clientOptions.toBuilder().apply(modifier::accept).build())
50+
4751
override fun projects(): ProjectService = projects
4852

4953
override fun commits(): CommitService = commits
@@ -73,6 +77,13 @@ class OpenlayerClientImpl(private val clientOptions: ClientOptions) : OpenlayerC
7377
StorageServiceImpl.WithRawResponseImpl(clientOptions)
7478
}
7579

80+
override fun withOptions(
81+
modifier: Consumer<ClientOptions.Builder>
82+
): OpenlayerClient.WithRawResponse =
83+
OpenlayerClientImpl.WithRawResponseImpl(
84+
clientOptions.toBuilder().apply(modifier::accept).build()
85+
)
86+
7687
override fun projects(): ProjectService.WithRawResponse = projects
7788

7889
override fun commits(): CommitService.WithRawResponse = commits

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/async/CommitServiceAsync.kt

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

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

5+
import com.openlayer.api.core.ClientOptions
56
import com.openlayer.api.core.RequestOptions
67
import com.openlayer.api.core.http.HttpResponseFor
78
import com.openlayer.api.models.commits.CommitRetrieveParams
89
import com.openlayer.api.models.commits.CommitRetrieveResponse
910
import com.openlayer.api.services.async.commits.TestResultServiceAsync
1011
import java.util.concurrent.CompletableFuture
12+
import java.util.function.Consumer
1113

1214
interface CommitServiceAsync {
1315

@@ -16,6 +18,13 @@ interface CommitServiceAsync {
1618
*/
1719
fun withRawResponse(): WithRawResponse
1820

21+
/**
22+
* Returns a view of this service with the given option modifications applied.
23+
*
24+
* The original service is not modified.
25+
*/
26+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): CommitServiceAsync
27+
1928
fun testResults(): TestResultServiceAsync
2029

2130
/** Retrieve a project version (commit) by its id. */
@@ -59,6 +68,15 @@ interface CommitServiceAsync {
5968
*/
6069
interface WithRawResponse {
6170

71+
/**
72+
* Returns a view of this service with the given option modifications applied.
73+
*
74+
* The original service is not modified.
75+
*/
76+
fun withOptions(
77+
modifier: Consumer<ClientOptions.Builder>
78+
): CommitServiceAsync.WithRawResponse
79+
6280
fun testResults(): TestResultServiceAsync.WithRawResponse
6381

6482
/**

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/async/CommitServiceAsyncImpl.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.openlayer.api.models.commits.CommitRetrieveResponse
2020
import com.openlayer.api.services.async.commits.TestResultServiceAsync
2121
import com.openlayer.api.services.async.commits.TestResultServiceAsyncImpl
2222
import java.util.concurrent.CompletableFuture
23+
import java.util.function.Consumer
2324
import kotlin.jvm.optionals.getOrNull
2425

2526
class CommitServiceAsyncImpl internal constructor(private val clientOptions: ClientOptions) :
@@ -35,6 +36,9 @@ class CommitServiceAsyncImpl internal constructor(private val clientOptions: Cli
3536

3637
override fun withRawResponse(): CommitServiceAsync.WithRawResponse = withRawResponse
3738

39+
override fun withOptions(modifier: Consumer<ClientOptions.Builder>): CommitServiceAsync =
40+
CommitServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
41+
3842
override fun testResults(): TestResultServiceAsync = testResults
3943

4044
override fun retrieve(
@@ -53,6 +57,13 @@ class CommitServiceAsyncImpl internal constructor(private val clientOptions: Cli
5357
TestResultServiceAsyncImpl.WithRawResponseImpl(clientOptions)
5458
}
5559

60+
override fun withOptions(
61+
modifier: Consumer<ClientOptions.Builder>
62+
): CommitServiceAsync.WithRawResponse =
63+
CommitServiceAsyncImpl.WithRawResponseImpl(
64+
clientOptions.toBuilder().apply(modifier::accept).build()
65+
)
66+
5667
override fun testResults(): TestResultServiceAsync.WithRawResponse = testResults
5768

5869
private val retrieveHandler: Handler<CommitRetrieveResponse> =

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/async/InferencePipelineServiceAsync.kt

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

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

5+
import com.openlayer.api.core.ClientOptions
56
import com.openlayer.api.core.RequestOptions
67
import com.openlayer.api.core.http.HttpResponse
78
import com.openlayer.api.core.http.HttpResponseFor
@@ -14,6 +15,7 @@ import com.openlayer.api.services.async.inferencepipelines.DataServiceAsync
1415
import com.openlayer.api.services.async.inferencepipelines.RowServiceAsync
1516
import com.openlayer.api.services.async.inferencepipelines.TestResultServiceAsync
1617
import java.util.concurrent.CompletableFuture
18+
import java.util.function.Consumer
1719

1820
interface InferencePipelineServiceAsync {
1921

@@ -22,6 +24,13 @@ interface InferencePipelineServiceAsync {
2224
*/
2325
fun withRawResponse(): WithRawResponse
2426

27+
/**
28+
* Returns a view of this service with the given option modifications applied.
29+
*
30+
* The original service is not modified.
31+
*/
32+
fun withOptions(modifier: Consumer<ClientOptions.Builder>): InferencePipelineServiceAsync
33+
2534
fun data(): DataServiceAsync
2635

2736
fun rows(): RowServiceAsync
@@ -149,6 +158,15 @@ interface InferencePipelineServiceAsync {
149158
*/
150159
interface WithRawResponse {
151160

161+
/**
162+
* Returns a view of this service with the given option modifications applied.
163+
*
164+
* The original service is not modified.
165+
*/
166+
fun withOptions(
167+
modifier: Consumer<ClientOptions.Builder>
168+
): InferencePipelineServiceAsync.WithRawResponse
169+
152170
fun data(): DataServiceAsync.WithRawResponse
153171

154172
fun rows(): RowServiceAsync.WithRawResponse

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/async/InferencePipelineServiceAsyncImpl.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.openlayer.api.services.async.inferencepipelines.RowServiceAsyncImpl
3030
import com.openlayer.api.services.async.inferencepipelines.TestResultServiceAsync
3131
import com.openlayer.api.services.async.inferencepipelines.TestResultServiceAsyncImpl
3232
import java.util.concurrent.CompletableFuture
33+
import java.util.function.Consumer
3334
import kotlin.jvm.optionals.getOrNull
3435

3536
class InferencePipelineServiceAsyncImpl
@@ -49,6 +50,11 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
4950

5051
override fun withRawResponse(): InferencePipelineServiceAsync.WithRawResponse = withRawResponse
5152

53+
override fun withOptions(
54+
modifier: Consumer<ClientOptions.Builder>
55+
): InferencePipelineServiceAsync =
56+
InferencePipelineServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())
57+
5258
override fun data(): DataServiceAsync = data
5359

5460
override fun rows(): RowServiceAsync = rows
@@ -93,6 +99,13 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
9399
TestResultServiceAsyncImpl.WithRawResponseImpl(clientOptions)
94100
}
95101

102+
override fun withOptions(
103+
modifier: Consumer<ClientOptions.Builder>
104+
): InferencePipelineServiceAsync.WithRawResponse =
105+
InferencePipelineServiceAsyncImpl.WithRawResponseImpl(
106+
clientOptions.toBuilder().apply(modifier::accept).build()
107+
)
108+
96109
override fun data(): DataServiceAsync.WithRawResponse = data
97110

98111
override fun rows(): RowServiceAsync.WithRawResponse = rows

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/async/ProjectServiceAsync.kt

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

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

5+
import com.openlayer.api.core.ClientOptions
56
import com.openlayer.api.core.RequestOptions
67
import com.openlayer.api.core.http.HttpResponseFor
78
import com.openlayer.api.models.projects.ProjectCreateParams
@@ -12,6 +13,7 @@ import com.openlayer.api.services.async.projects.CommitServiceAsync
1213
import com.openlayer.api.services.async.projects.InferencePipelineServiceAsync
1314
import com.openlayer.api.services.async.projects.TestServiceAsync
1415
import java.util.concurrent.CompletableFuture
16+
import java.util.function.Consumer
1517

1618
interface ProjectServiceAsync {
1719

@@ -20,6 +22,13 @@ interface ProjectServiceAsync {
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>): ProjectServiceAsync
31+
2332
fun commits(): CommitServiceAsync
2433

2534
fun inferencePipelines(): InferencePipelineServiceAsync
@@ -59,6 +68,15 @@ interface ProjectServiceAsync {
5968
*/
6069
interface WithRawResponse {
6170

71+
/**
72+
* Returns a view of this service with the given option modifications applied.
73+
*
74+
* The original service is not modified.
75+
*/
76+
fun withOptions(
77+
modifier: Consumer<ClientOptions.Builder>
78+
): ProjectServiceAsync.WithRawResponse
79+
6280
fun commits(): CommitServiceAsync.WithRawResponse
6381

6482
fun inferencePipelines(): InferencePipelineServiceAsync.WithRawResponse

0 commit comments

Comments
 (0)