Skip to content

Commit fd5752e

Browse files
feat(api): manual updates
1 parent e48acb3 commit fd5752e

35 files changed

Lines changed: 285 additions & 38 deletions

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.openlayer.api.client.OpenlayerClient
77
import com.openlayer.api.client.OpenlayerClientImpl
88
import com.openlayer.api.core.ClientOptions
9+
import com.openlayer.api.core.Sleeper
910
import com.openlayer.api.core.Timeout
1011
import com.openlayer.api.core.http.Headers
1112
import com.openlayer.api.core.http.HttpClient
@@ -120,6 +121,17 @@ class OpenlayerOkHttpClient private constructor() {
120121
*/
121122
fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) }
122123

124+
/**
125+
* The interface to use for delaying execution, like during retries.
126+
*
127+
* This is primarily useful for using fake delays in tests.
128+
*
129+
* Defaults to real execution delays.
130+
*
131+
* This class takes ownership of the sleeper and closes it when closed.
132+
*/
133+
fun sleeper(sleeper: Sleeper) = apply { clientOptions.sleeper(sleeper) }
134+
123135
/**
124136
* The clock to use for operations that require timing, like retries.
125137
*

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.openlayer.api.client.OpenlayerClientAsync
77
import com.openlayer.api.client.OpenlayerClientAsyncImpl
88
import com.openlayer.api.core.ClientOptions
9+
import com.openlayer.api.core.Sleeper
910
import com.openlayer.api.core.Timeout
1011
import com.openlayer.api.core.http.Headers
1112
import com.openlayer.api.core.http.HttpClient
@@ -120,6 +121,17 @@ class OpenlayerOkHttpClientAsync private constructor() {
120121
*/
121122
fun jsonMapper(jsonMapper: JsonMapper) = apply { clientOptions.jsonMapper(jsonMapper) }
122123

124+
/**
125+
* The interface to use for delaying execution, like during retries.
126+
*
127+
* This is primarily useful for using fake delays in tests.
128+
*
129+
* Defaults to real execution delays.
130+
*
131+
* This class takes ownership of the sleeper and closes it when closed.
132+
*/
133+
fun sleeper(sleeper: Sleeper) = apply { clientOptions.sleeper(sleeper) }
134+
123135
/**
124136
* The clock to use for operations that require timing, like retries.
125137
*

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ private constructor(
4040
* needs to be overridden.
4141
*/
4242
@get:JvmName("jsonMapper") val jsonMapper: JsonMapper,
43+
/**
44+
* The interface to use for delaying execution, like during retries.
45+
*
46+
* This is primarily useful for using fake delays in tests.
47+
*
48+
* Defaults to real execution delays.
49+
*
50+
* This class takes ownership of the sleeper and closes it when closed.
51+
*/
52+
@get:JvmName("sleeper") val sleeper: Sleeper,
4353
/**
4454
* The clock to use for operations that require timing, like retries.
4555
*
@@ -131,6 +141,7 @@ private constructor(
131141
private var httpClient: HttpClient? = null
132142
private var checkJacksonVersionCompatibility: Boolean = true
133143
private var jsonMapper: JsonMapper = jsonMapper()
144+
private var sleeper: Sleeper? = null
134145
private var clock: Clock = Clock.systemUTC()
135146
private var baseUrl: String? = null
136147
private var headers: Headers.Builder = Headers.builder()
@@ -145,6 +156,7 @@ private constructor(
145156
httpClient = clientOptions.originalHttpClient
146157
checkJacksonVersionCompatibility = clientOptions.checkJacksonVersionCompatibility
147158
jsonMapper = clientOptions.jsonMapper
159+
sleeper = clientOptions.sleeper
148160
clock = clientOptions.clock
149161
baseUrl = clientOptions.baseUrl
150162
headers = clientOptions.headers.toBuilder()
@@ -185,6 +197,17 @@ private constructor(
185197
*/
186198
fun jsonMapper(jsonMapper: JsonMapper) = apply { this.jsonMapper = jsonMapper }
187199

200+
/**
201+
* The interface to use for delaying execution, like during retries.
202+
*
203+
* This is primarily useful for using fake delays in tests.
204+
*
205+
* Defaults to real execution delays.
206+
*
207+
* This class takes ownership of the sleeper and closes it when closed.
208+
*/
209+
fun sleeper(sleeper: Sleeper) = apply { this.sleeper = PhantomReachableSleeper(sleeper) }
210+
188211
/**
189212
* The clock to use for operations that require timing, like retries.
190213
*
@@ -370,6 +393,7 @@ private constructor(
370393
*/
371394
fun build(): ClientOptions {
372395
val httpClient = checkRequired("httpClient", httpClient)
396+
val sleeper = sleeper ?: PhantomReachableSleeper(DefaultSleeper())
373397

374398
val headers = Headers.builder()
375399
val queryParams = QueryParams.builder()
@@ -392,11 +416,13 @@ private constructor(
392416
httpClient,
393417
RetryingHttpClient.builder()
394418
.httpClient(httpClient)
419+
.sleeper(sleeper)
395420
.clock(clock)
396421
.maxRetries(maxRetries)
397422
.build(),
398423
checkJacksonVersionCompatibility,
399424
jsonMapper,
425+
sleeper,
400426
clock,
401427
baseUrl,
402428
headers.build(),
@@ -421,5 +447,6 @@ private constructor(
421447
*/
422448
fun close() {
423449
httpClient.close()
450+
sleeper.close()
424451
}
425452
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.openlayer.api.core
2+
3+
import java.time.Duration
4+
import java.util.Timer
5+
import java.util.TimerTask
6+
import java.util.concurrent.CompletableFuture
7+
8+
class DefaultSleeper : Sleeper {
9+
10+
private val timer = Timer("DefaultSleeper", true)
11+
12+
override fun sleep(duration: Duration) = Thread.sleep(duration.toMillis())
13+
14+
override fun sleepAsync(duration: Duration): CompletableFuture<Void> {
15+
val future = CompletableFuture<Void>()
16+
timer.schedule(
17+
object : TimerTask() {
18+
override fun run() {
19+
future.complete(null)
20+
}
21+
},
22+
duration.toMillis(),
23+
)
24+
return future
25+
}
26+
27+
override fun close() = timer.cancel()
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.openlayer.api.core
2+
3+
import java.time.Duration
4+
import java.util.concurrent.CompletableFuture
5+
6+
/**
7+
* A delegating wrapper around a [Sleeper] that closes it once it's only phantom reachable.
8+
*
9+
* This class ensures the [Sleeper] is closed even if the user forgets to do it.
10+
*/
11+
internal class PhantomReachableSleeper(private val sleeper: Sleeper) : Sleeper {
12+
13+
init {
14+
closeWhenPhantomReachable(this, sleeper)
15+
}
16+
17+
override fun sleep(duration: Duration) = sleeper.sleep(duration)
18+
19+
override fun sleepAsync(duration: Duration): CompletableFuture<Void> =
20+
sleeper.sleepAsync(duration)
21+
22+
override fun close() = sleeper.close()
23+
}

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

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

33
package com.openlayer.api.core
44

5-
import java.util.Properties
5+
import com.openlayer.api.client.OpenlayerClient
66

77
fun getOsArch(): String {
88
val osArch = System.getProperty("os.arch")
@@ -16,7 +16,7 @@ fun getOsArch(): String {
1616
"x86_64" -> "x64"
1717
"arm" -> "arm"
1818
"aarch64" -> "arm64"
19-
else -> "other:${osArch}"
19+
else -> "other:$osArch"
2020
}
2121
}
2222

@@ -30,13 +30,13 @@ fun getOsName(): String {
3030
osName.startsWith("Linux") -> "Linux"
3131
osName.startsWith("Mac OS") -> "MacOS"
3232
osName.startsWith("Windows") -> "Windows"
33-
else -> "Other:${osName}"
33+
else -> "Other:$osName"
3434
}
3535
}
3636

3737
fun getOsVersion(): String = System.getProperty("os.version", "unknown")
3838

3939
fun getPackageVersion(): String =
40-
Properties::class.java.`package`.implementationVersion ?: "unknown"
40+
OpenlayerClient::class.java.`package`.implementationVersion ?: "unknown"
4141

4242
fun getJavaVersion(): String = System.getProperty("java.version", "unknown")
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.openlayer.api.core
2+
3+
import java.time.Duration
4+
import java.util.concurrent.CompletableFuture
5+
6+
/**
7+
* An interface for delaying execution for a specified amount of time.
8+
*
9+
* Useful for testing and cleaning up resources.
10+
*/
11+
interface Sleeper : AutoCloseable {
12+
13+
/** Synchronously pauses execution for the given [duration]. */
14+
fun sleep(duration: Duration)
15+
16+
/** Asynchronously pauses execution for the given [duration]. */
17+
fun sleepAsync(duration: Duration): CompletableFuture<Void>
18+
19+
/** Overridden from [AutoCloseable] to not have a checked exception in its signature. */
20+
override fun close()
21+
}

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.openlayer.api.core.http
22

3+
import com.openlayer.api.core.DefaultSleeper
34
import com.openlayer.api.core.RequestOptions
5+
import com.openlayer.api.core.Sleeper
46
import com.openlayer.api.core.checkRequired
57
import com.openlayer.api.errors.OpenlayerIoException
68
import com.openlayer.api.errors.OpenlayerRetryableException
@@ -11,8 +13,6 @@ import java.time.OffsetDateTime
1113
import java.time.format.DateTimeFormatter
1214
import java.time.format.DateTimeParseException
1315
import java.time.temporal.ChronoUnit
14-
import java.util.Timer
15-
import java.util.TimerTask
1616
import java.util.UUID
1717
import java.util.concurrent.CompletableFuture
1818
import java.util.concurrent.ThreadLocalRandom
@@ -130,7 +130,10 @@ private constructor(
130130
return executeWithRetries(modifiedRequest, requestOptions)
131131
}
132132

133-
override fun close() = httpClient.close()
133+
override fun close() {
134+
httpClient.close()
135+
sleeper.close()
136+
}
134137

135138
private fun isRetryable(request: HttpRequest): Boolean =
136139
// Some requests, such as when a request body is being streamed, cannot be retried because
@@ -235,33 +238,14 @@ private constructor(
235238
class Builder internal constructor() {
236239

237240
private var httpClient: HttpClient? = null
238-
private var sleeper: Sleeper =
239-
object : Sleeper {
240-
241-
private val timer = Timer("RetryingHttpClient", true)
242-
243-
override fun sleep(duration: Duration) = Thread.sleep(duration.toMillis())
244-
245-
override fun sleepAsync(duration: Duration): CompletableFuture<Void> {
246-
val future = CompletableFuture<Void>()
247-
timer.schedule(
248-
object : TimerTask() {
249-
override fun run() {
250-
future.complete(null)
251-
}
252-
},
253-
duration.toMillis(),
254-
)
255-
return future
256-
}
257-
}
241+
private var sleeper: Sleeper? = null
258242
private var clock: Clock = Clock.systemUTC()
259243
private var maxRetries: Int = 2
260244
private var idempotencyHeader: String? = null
261245

262246
fun httpClient(httpClient: HttpClient) = apply { this.httpClient = httpClient }
263247

264-
@JvmSynthetic internal fun sleeper(sleeper: Sleeper) = apply { this.sleeper = sleeper }
248+
fun sleeper(sleeper: Sleeper) = apply { this.sleeper = sleeper }
265249

266250
fun clock(clock: Clock) = apply { this.clock = clock }
267251

@@ -272,17 +256,10 @@ private constructor(
272256
fun build(): HttpClient =
273257
RetryingHttpClient(
274258
checkRequired("httpClient", httpClient),
275-
sleeper,
259+
sleeper ?: DefaultSleeper(),
276260
clock,
277261
maxRetries,
278262
idempotencyHeader,
279263
)
280264
}
281-
282-
internal interface Sleeper {
283-
284-
fun sleep(duration: Duration)
285-
286-
fun sleepAsync(duration: Duration): CompletableFuture<Void>
287-
}
288265
}

openlayer-java-core/src/main/kotlin/com/openlayer/api/models/commits/CommitRetrieveResponse.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import java.util.Optional
2020
import kotlin.jvm.optionals.getOrNull
2121

2222
class CommitRetrieveResponse
23+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
2324
private constructor(
2425
private val id: JsonField<String>,
2526
private val commit: JsonField<Commit>,
@@ -848,6 +849,7 @@ private constructor(
848849

849850
/** The details of a commit (project version). */
850851
class Commit
852+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
851853
private constructor(
852854
private val id: JsonField<String>,
853855
private val authorId: JsonField<String>,
@@ -1671,6 +1673,7 @@ private constructor(
16711673
}
16721674

16731675
class Links
1676+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
16741677
private constructor(
16751678
private val app: JsonField<String>,
16761679
private val additionalProperties: MutableMap<String, JsonValue>,

openlayer-java-core/src/main/kotlin/com/openlayer/api/models/commits/testresults/TestResultListResponse.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import java.util.Optional
3333
import kotlin.jvm.optionals.getOrNull
3434

3535
class TestResultListResponse
36+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
3637
private constructor(
3738
private val items: JsonField<List<Item>>,
3839
private val additionalProperties: MutableMap<String, JsonValue>,
@@ -183,6 +184,7 @@ private constructor(
183184
(items.asKnown().getOrNull()?.sumOf { it.validity().toInt() } ?: 0)
184185

185186
class Item
187+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
186188
private constructor(
187189
private val id: JsonField<String>,
188190
private val dateCreated: JsonField<OffsetDateTime>,
@@ -921,6 +923,7 @@ private constructor(
921923
}
922924

923925
class Goal
926+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
924927
private constructor(
925928
private val id: JsonField<String>,
926929
private val commentCount: JsonField<Long>,
@@ -2359,6 +2362,7 @@ private constructor(
23592362
}
23602363

23612364
class Threshold
2365+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
23622366
private constructor(
23632367
private val insightName: JsonField<InsightName>,
23642368
private val insightParameters: JsonField<List<InsightParameter>>,
@@ -3081,6 +3085,7 @@ private constructor(
30813085
}
30823086

30833087
class InsightParameter
3088+
@JsonCreator(mode = JsonCreator.Mode.DISABLED)
30843089
private constructor(
30853090
private val name: JsonField<String>,
30863091
private val value: JsonValue,

0 commit comments

Comments
 (0)