Skip to content

Commit 710b626

Browse files
feat(client): implement per-endpoint base URL support
Refactor `HttpRequest` to always take a `baseUrl`, instead of storing this in `OkHttpClient`. This allows better reuse of `OkHttpClient` when changing the `baseUrl`.
1 parent 0e78fa9 commit 710b626

28 files changed

+79
-44
lines changed

openlayer-java-client-okhttp/src/main/kotlin/com/openlayer/api/client/okhttp/OkHttpClient.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.openlayer.api.client.okhttp
22

33
import com.openlayer.api.core.RequestOptions
44
import com.openlayer.api.core.Timeout
5-
import com.openlayer.api.core.checkRequired
65
import com.openlayer.api.core.http.Headers
76
import com.openlayer.api.core.http.HttpClient
87
import com.openlayer.api.core.http.HttpMethod
@@ -17,7 +16,6 @@ import java.time.Duration
1716
import java.util.concurrent.CompletableFuture
1817
import okhttp3.Call
1918
import okhttp3.Callback
20-
import okhttp3.HttpUrl
2119
import okhttp3.HttpUrl.Companion.toHttpUrl
2220
import okhttp3.MediaType
2321
import okhttp3.MediaType.Companion.toMediaType
@@ -28,8 +26,7 @@ import okhttp3.Response
2826
import okhttp3.logging.HttpLoggingInterceptor
2927
import okio.BufferedSink
3028

31-
class OkHttpClient
32-
private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val baseUrl: HttpUrl) :
29+
class OkHttpClient private constructor(private val okHttpClient: okhttp3.OkHttpClient) :
3330
HttpClient {
3431

3532
override fun execute(request: HttpRequest, requestOptions: RequestOptions): HttpResponse {
@@ -140,11 +137,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
140137
}
141138

142139
private fun HttpRequest.toUrl(): String {
143-
url?.let {
144-
return it
145-
}
146-
147-
val builder = baseUrl.newBuilder()
140+
val builder = baseUrl.toHttpUrl().newBuilder()
148141
pathSegments.forEach(builder::addPathSegment)
149142
queryParams.keys().forEach { key ->
150143
queryParams.values(key).forEach { builder.addQueryParameter(key, it) }
@@ -194,12 +187,9 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
194187

195188
class Builder internal constructor() {
196189

197-
private var baseUrl: HttpUrl? = null
198190
private var timeout: Timeout = Timeout.default()
199191
private var proxy: Proxy? = null
200192

201-
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl.toHttpUrl() }
202-
203193
fun timeout(timeout: Timeout) = apply { this.timeout = timeout }
204194

205195
fun timeout(timeout: Duration) = timeout(Timeout.builder().request(timeout).build())
@@ -214,8 +204,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
214204
.writeTimeout(timeout.write())
215205
.callTimeout(timeout.request())
216206
.proxy(proxy)
217-
.build(),
218-
checkRequired("baseUrl", baseUrl),
207+
.build()
219208
)
220209
}
221210
}

openlayer-java-client-okhttp/src/main/kotlin/com/openlayer/api/client/okhttp/OpenlayerOkHttpClient.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,7 @@ class OpenlayerOkHttpClient private constructor() {
166166
fun build(): OpenlayerClient =
167167
OpenlayerClientImpl(
168168
clientOptions
169-
.httpClient(
170-
OkHttpClient.builder()
171-
.baseUrl(clientOptions.baseUrl())
172-
.timeout(timeout)
173-
.proxy(proxy)
174-
.build()
175-
)
169+
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
176170
.build()
177171
)
178172
}

openlayer-java-client-okhttp/src/main/kotlin/com/openlayer/api/client/okhttp/OpenlayerOkHttpClientAsync.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,7 @@ class OpenlayerOkHttpClientAsync private constructor() {
168168
fun build(): OpenlayerClientAsync =
169169
OpenlayerClientAsyncImpl(
170170
clientOptions
171-
.httpClient(
172-
OkHttpClient.builder()
173-
.baseUrl(clientOptions.baseUrl())
174-
.timeout(timeout)
175-
.proxy(proxy)
176-
.build()
177-
)
171+
.httpClient(OkHttpClient.builder().timeout(timeout).proxy(proxy).build())
178172
.build()
179173
)
180174
}

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private constructor(
1919
@get:JvmName("checkJacksonVersionCompatibility") val checkJacksonVersionCompatibility: Boolean,
2020
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
2121
@get:JvmName("clock") val clock: Clock,
22-
@get:JvmName("baseUrl") val baseUrl: String,
22+
private val baseUrl: String?,
2323
@get:JvmName("headers") val headers: Headers,
2424
@get:JvmName("queryParams") val queryParams: QueryParams,
2525
@get:JvmName("responseValidation") val responseValidation: Boolean,
@@ -34,6 +34,8 @@ private constructor(
3434
}
3535
}
3636

37+
fun baseUrl(): String = baseUrl ?: PRODUCTION_URL
38+
3739
fun apiKey(): Optional<String> = Optional.ofNullable(apiKey)
3840

3941
fun toBuilder() = Builder().from(this)
@@ -62,7 +64,7 @@ private constructor(
6264
private var checkJacksonVersionCompatibility: Boolean = true
6365
private var jsonMapper: JsonMapper = jsonMapper()
6466
private var clock: Clock = Clock.systemUTC()
65-
private var baseUrl: String = PRODUCTION_URL
67+
private var baseUrl: String? = null
6668
private var headers: Headers.Builder = Headers.builder()
6769
private var queryParams: QueryParams.Builder = QueryParams.builder()
6870
private var responseValidation: Boolean = false
@@ -95,7 +97,10 @@ private constructor(
9597

9698
fun clock(clock: Clock) = apply { this.clock = clock }
9799

98-
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
100+
fun baseUrl(baseUrl: String?) = apply { this.baseUrl = baseUrl }
101+
102+
/** Alias for calling [Builder.baseUrl] with `baseUrl.orElse(null)`. */
103+
fun baseUrl(baseUrl: Optional<String>) = baseUrl(baseUrl.getOrNull())
99104

100105
fun responseValidation(responseValidation: Boolean) = apply {
101106
this.responseValidation = responseValidation
@@ -190,8 +195,6 @@ private constructor(
190195

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

193-
fun baseUrl(): String = baseUrl
194-
195198
fun fromEnv() = apply {
196199
System.getenv("OPENLAYER_BASE_URL")?.let { baseUrl(it) }
197200
System.getenv("OPENLAYER_API_KEY")?.let { apiKey(it) }

openlayer-java-core/src/main/kotlin/com/openlayer/api/core/http/HttpRequest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.openlayer.api.core.toImmutable
66
class HttpRequest
77
private constructor(
88
@get:JvmName("method") val method: HttpMethod,
9-
@get:JvmName("url") val url: String?,
9+
@get:JvmName("baseUrl") val baseUrl: String,
1010
@get:JvmName("pathSegments") val pathSegments: List<String>,
1111
@get:JvmName("headers") val headers: Headers,
1212
@get:JvmName("queryParams") val queryParams: QueryParams,
@@ -16,7 +16,7 @@ private constructor(
1616
fun toBuilder(): Builder = Builder().from(this)
1717

1818
override fun toString(): String =
19-
"HttpRequest{method=$method, url=$url, pathSegments=$pathSegments, headers=$headers, queryParams=$queryParams, body=$body}"
19+
"HttpRequest{method=$method, baseUrl=$baseUrl, pathSegments=$pathSegments, headers=$headers, queryParams=$queryParams, body=$body}"
2020

2121
companion object {
2222
@JvmStatic fun builder() = Builder()
@@ -25,7 +25,7 @@ private constructor(
2525
class Builder internal constructor() {
2626

2727
private var method: HttpMethod? = null
28-
private var url: String? = null
28+
private var baseUrl: String? = null
2929
private var pathSegments: MutableList<String> = mutableListOf()
3030
private var headers: Headers.Builder = Headers.builder()
3131
private var queryParams: QueryParams.Builder = QueryParams.builder()
@@ -34,7 +34,7 @@ private constructor(
3434
@JvmSynthetic
3535
internal fun from(request: HttpRequest) = apply {
3636
method = request.method
37-
url = request.url
37+
baseUrl = request.baseUrl
3838
pathSegments = request.pathSegments.toMutableList()
3939
headers = request.headers.toBuilder()
4040
queryParams = request.queryParams.toBuilder()
@@ -43,7 +43,7 @@ private constructor(
4343

4444
fun method(method: HttpMethod) = apply { this.method = method }
4545

46-
fun url(url: String) = apply { this.url = url }
46+
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
4747

4848
fun addPathSegment(pathSegment: String) = apply { pathSegments.add(pathSegment) }
4949

@@ -136,7 +136,7 @@ private constructor(
136136
fun build(): HttpRequest =
137137
HttpRequest(
138138
checkRequired("method", method),
139-
url,
139+
checkRequired("baseUrl", baseUrl),
140140
pathSegments.toImmutable(),
141141
headers.build(),
142142
queryParams.build(),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class CommitServiceAsyncImpl internal constructor(private val clientOptions: Cli
6969
val request =
7070
HttpRequest.builder()
7171
.method(HttpMethod.GET)
72+
.baseUrl(clientOptions.baseUrl())
7273
.addPathSegments("versions", params._pathParam(0))
7374
.build()
7475
.prepareAsync(clientOptions, params)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
113113
val request =
114114
HttpRequest.builder()
115115
.method(HttpMethod.GET)
116+
.baseUrl(clientOptions.baseUrl())
116117
.addPathSegments("inference-pipelines", params._pathParam(0))
117118
.build()
118119
.prepareAsync(clientOptions, params)
@@ -146,6 +147,7 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
146147
val request =
147148
HttpRequest.builder()
148149
.method(HttpMethod.PUT)
150+
.baseUrl(clientOptions.baseUrl())
149151
.addPathSegments("inference-pipelines", params._pathParam(0))
150152
.body(json(clientOptions.jsonMapper, params._body()))
151153
.build()
@@ -178,6 +180,7 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
178180
val request =
179181
HttpRequest.builder()
180182
.method(HttpMethod.DELETE)
183+
.baseUrl(clientOptions.baseUrl())
181184
.addPathSegments("inference-pipelines", params._pathParam(0))
182185
.apply { params._body().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
183186
.build()

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ class ProjectServiceAsyncImpl internal constructor(private val clientOptions: Cl
9999
val request =
100100
HttpRequest.builder()
101101
.method(HttpMethod.POST)
102+
.baseUrl(clientOptions.baseUrl())
102103
.addPathSegments("projects")
103104
.body(json(clientOptions.jsonMapper, params._body()))
104105
.build()
@@ -130,6 +131,7 @@ class ProjectServiceAsyncImpl internal constructor(private val clientOptions: Cl
130131
val request =
131132
HttpRequest.builder()
132133
.method(HttpMethod.GET)
134+
.baseUrl(clientOptions.baseUrl())
133135
.addPathSegments("projects")
134136
.build()
135137
.prepareAsync(clientOptions, params)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestResultServiceAsyncImpl internal constructor(private val clientOptions:
5555
val request =
5656
HttpRequest.builder()
5757
.method(HttpMethod.GET)
58+
.baseUrl(clientOptions.baseUrl())
5859
.addPathSegments("versions", params._pathParam(0), "results")
5960
.build()
6061
.prepareAsync(clientOptions, params)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class DataServiceAsyncImpl internal constructor(private val clientOptions: Clien
5555
val request =
5656
HttpRequest.builder()
5757
.method(HttpMethod.POST)
58+
.baseUrl(clientOptions.baseUrl())
5859
.addPathSegments("inference-pipelines", params._pathParam(0), "data-stream")
5960
.body(json(clientOptions.jsonMapper, params._body()))
6061
.build()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class RowServiceAsyncImpl internal constructor(private val clientOptions: Client
5555
val request =
5656
HttpRequest.builder()
5757
.method(HttpMethod.PUT)
58+
.baseUrl(clientOptions.baseUrl())
5859
.addPathSegments("inference-pipelines", params._pathParam(0), "rows")
5960
.body(json(clientOptions.jsonMapper, params._body()))
6061
.build()

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestResultServiceAsyncImpl internal constructor(private val clientOptions:
5555
val request =
5656
HttpRequest.builder()
5757
.method(HttpMethod.GET)
58+
.baseUrl(clientOptions.baseUrl())
5859
.addPathSegments("inference-pipelines", params._pathParam(0), "results")
5960
.build()
6061
.prepareAsync(clientOptions, params)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class CommitServiceAsyncImpl internal constructor(private val clientOptions: Cli
6565
val request =
6666
HttpRequest.builder()
6767
.method(HttpMethod.POST)
68+
.baseUrl(clientOptions.baseUrl())
6869
.addPathSegments("projects", params._pathParam(0), "versions")
6970
.body(json(clientOptions.jsonMapper, params._body()))
7071
.build()
@@ -98,6 +99,7 @@ class CommitServiceAsyncImpl internal constructor(private val clientOptions: Cli
9899
val request =
99100
HttpRequest.builder()
100101
.method(HttpMethod.GET)
102+
.baseUrl(clientOptions.baseUrl())
101103
.addPathSegments("projects", params._pathParam(0), "versions")
102104
.build()
103105
.prepareAsync(clientOptions, params)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
6565
val request =
6666
HttpRequest.builder()
6767
.method(HttpMethod.POST)
68+
.baseUrl(clientOptions.baseUrl())
6869
.addPathSegments("projects", params._pathParam(0), "inference-pipelines")
6970
.body(json(clientOptions.jsonMapper, params._body()))
7071
.build()
@@ -99,6 +100,7 @@ internal constructor(private val clientOptions: ClientOptions) : InferencePipeli
99100
val request =
100101
HttpRequest.builder()
101102
.method(HttpMethod.GET)
103+
.baseUrl(clientOptions.baseUrl())
102104
.addPathSegments("projects", params._pathParam(0), "inference-pipelines")
103105
.build()
104106
.prepareAsync(clientOptions, params)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class TestServiceAsyncImpl internal constructor(private val clientOptions: Clien
7373
val request =
7474
HttpRequest.builder()
7575
.method(HttpMethod.POST)
76+
.baseUrl(clientOptions.baseUrl())
7677
.addPathSegments("projects", params._pathParam(0), "tests")
7778
.body(json(clientOptions.jsonMapper, params._body()))
7879
.build()
@@ -106,6 +107,7 @@ class TestServiceAsyncImpl internal constructor(private val clientOptions: Clien
106107
val request =
107108
HttpRequest.builder()
108109
.method(HttpMethod.PUT)
110+
.baseUrl(clientOptions.baseUrl())
109111
.addPathSegments("projects", params._pathParam(0), "tests")
110112
.body(json(clientOptions.jsonMapper, params._body()))
111113
.build()
@@ -139,6 +141,7 @@ class TestServiceAsyncImpl internal constructor(private val clientOptions: Clien
139141
val request =
140142
HttpRequest.builder()
141143
.method(HttpMethod.GET)
144+
.baseUrl(clientOptions.baseUrl())
142145
.addPathSegments("projects", params._pathParam(0), "tests")
143146
.build()
144147
.prepareAsync(clientOptions, params)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class PresignedUrlServiceAsyncImpl internal constructor(private val clientOption
5151
val request =
5252
HttpRequest.builder()
5353
.method(HttpMethod.POST)
54+
.baseUrl(clientOptions.baseUrl())
5455
.addPathSegments("storage", "presigned-url")
5556
.apply { params._body().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
5657
.build()

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/blocking/CommitServiceImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class CommitServiceImpl internal constructor(private val clientOptions: ClientOp
6666
val request =
6767
HttpRequest.builder()
6868
.method(HttpMethod.GET)
69+
.baseUrl(clientOptions.baseUrl())
6970
.addPathSegments("versions", params._pathParam(0))
7071
.build()
7172
.prepare(clientOptions, params)

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/blocking/InferencePipelineServiceImpl.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class InferencePipelineServiceImpl internal constructor(private val clientOption
108108
val request =
109109
HttpRequest.builder()
110110
.method(HttpMethod.GET)
111+
.baseUrl(clientOptions.baseUrl())
111112
.addPathSegments("inference-pipelines", params._pathParam(0))
112113
.build()
113114
.prepare(clientOptions, params)
@@ -138,6 +139,7 @@ class InferencePipelineServiceImpl internal constructor(private val clientOption
138139
val request =
139140
HttpRequest.builder()
140141
.method(HttpMethod.PUT)
142+
.baseUrl(clientOptions.baseUrl())
141143
.addPathSegments("inference-pipelines", params._pathParam(0))
142144
.body(json(clientOptions.jsonMapper, params._body()))
143145
.build()
@@ -167,6 +169,7 @@ class InferencePipelineServiceImpl internal constructor(private val clientOption
167169
val request =
168170
HttpRequest.builder()
169171
.method(HttpMethod.DELETE)
172+
.baseUrl(clientOptions.baseUrl())
170173
.addPathSegments("inference-pipelines", params._pathParam(0))
171174
.apply { params._body().ifPresent { body(json(clientOptions.jsonMapper, it)) } }
172175
.build()

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/blocking/ProjectServiceImpl.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ProjectServiceImpl internal constructor(private val clientOptions: ClientO
9898
val request =
9999
HttpRequest.builder()
100100
.method(HttpMethod.POST)
101+
.baseUrl(clientOptions.baseUrl())
101102
.addPathSegments("projects")
102103
.body(json(clientOptions.jsonMapper, params._body()))
103104
.build()
@@ -126,6 +127,7 @@ class ProjectServiceImpl internal constructor(private val clientOptions: ClientO
126127
val request =
127128
HttpRequest.builder()
128129
.method(HttpMethod.GET)
130+
.baseUrl(clientOptions.baseUrl())
129131
.addPathSegments("projects")
130132
.build()
131133
.prepare(clientOptions, params)

openlayer-java-core/src/main/kotlin/com/openlayer/api/services/blocking/commits/TestResultServiceImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class TestResultServiceImpl internal constructor(private val clientOptions: Clie
5454
val request =
5555
HttpRequest.builder()
5656
.method(HttpMethod.GET)
57+
.baseUrl(clientOptions.baseUrl())
5758
.addPathSegments("versions", params._pathParam(0), "results")
5859
.build()
5960
.prepare(clientOptions, params)

0 commit comments

Comments
 (0)