Skip to content

Commit 84bdc22

Browse files
feat(client): add various convenience setters to models (#184)
feat(client): allow setting arbitrary JSON for top-level body params feat(client): expose getters for `JsonField` of body params fix(client): consistently throw on omitting required fields fix(client): convert `JsonField` containing list type to mutable in builder style(internal): simplify existing convenience setters on params style(internal): move headers and query params setters below others style(internal): explicitly add some method return types
1 parent 3a47a01 commit 84bdc22

107 files changed

Lines changed: 113617 additions & 32770 deletions

File tree

Some content is hidden

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

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ private constructor(
7272

7373
fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
7474

75+
fun responseValidation(responseValidation: Boolean) = apply {
76+
this.responseValidation = responseValidation
77+
}
78+
79+
fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }
80+
81+
fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }
82+
83+
fun webhookSecret(webhookSecret: String?) = apply { this.webhookSecret = webhookSecret }
84+
85+
fun webhookSecret(webhookSecret: Optional<String>) =
86+
webhookSecret(webhookSecret.orElse(null))
87+
7588
fun headers(headers: Headers) = apply {
7689
this.headers.clear()
7790
putAllHeaders(headers)
@@ -152,19 +165,6 @@ private constructor(
152165

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

155-
fun responseValidation(responseValidation: Boolean) = apply {
156-
this.responseValidation = responseValidation
157-
}
158-
159-
fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }
160-
161-
fun apiKey(apiKey: String) = apply { this.apiKey = apiKey }
162-
163-
fun webhookSecret(webhookSecret: String?) = apply { this.webhookSecret = webhookSecret }
164-
165-
fun webhookSecret(webhookSecret: Optional<String>) =
166-
webhookSecret(webhookSecret.orElse(null))
167-
168168
fun fromEnv() = apply {
169169
System.getenv("ORB_API_KEY")?.let { apiKey(it) }
170170
System.getenv("ORB_WEBHOOK_SECRET")?.let { webhookSecret(it) }

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

Lines changed: 145 additions & 71 deletions
Large diffs are not rendered by default.

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

Lines changed: 133 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonProperty
99
import com.withorb.api.core.Enum
1010
import com.withorb.api.core.ExcludeMissing
1111
import com.withorb.api.core.JsonField
12+
import com.withorb.api.core.JsonMissing
1213
import com.withorb.api.core.JsonValue
1314
import com.withorb.api.core.NoAutoDetect
1415
import com.withorb.api.core.http.Headers
@@ -46,12 +47,21 @@ constructor(
4647
/** The thresholds that define the values at which the alert will be triggered. */
4748
fun thresholds(): Optional<List<Threshold>> = body.thresholds()
4849

49-
fun _additionalHeaders(): Headers = additionalHeaders
50+
/** The case sensitive currency or custom pricing unit to use for this alert. */
51+
fun _currency(): JsonField<String> = body._currency()
5052

51-
fun _additionalQueryParams(): QueryParams = additionalQueryParams
53+
/** The type of alert to create. This must be a valid alert type. */
54+
fun _type(): JsonField<Type> = body._type()
55+
56+
/** The thresholds that define the values at which the alert will be triggered. */
57+
fun _thresholds(): JsonField<List<Threshold>> = body._thresholds()
5258

5359
fun _additionalBodyProperties(): Map<String, JsonValue> = body._additionalProperties()
5460

61+
fun _additionalHeaders(): Headers = additionalHeaders
62+
63+
fun _additionalQueryParams(): QueryParams = additionalQueryParams
64+
5565
@JvmSynthetic internal fun getBody(): AlertCreateForCustomerBody = body
5666

5767
@JvmSynthetic internal fun getHeaders(): Headers = additionalHeaders
@@ -69,27 +79,53 @@ constructor(
6979
class AlertCreateForCustomerBody
7080
@JsonCreator
7181
internal constructor(
72-
@JsonProperty("currency") private val currency: String,
73-
@JsonProperty("type") private val type: Type,
74-
@JsonProperty("thresholds") private val thresholds: List<Threshold>?,
82+
@JsonProperty("currency")
83+
@ExcludeMissing
84+
private val currency: JsonField<String> = JsonMissing.of(),
85+
@JsonProperty("type") @ExcludeMissing private val type: JsonField<Type> = JsonMissing.of(),
86+
@JsonProperty("thresholds")
87+
@ExcludeMissing
88+
private val thresholds: JsonField<List<Threshold>> = JsonMissing.of(),
7589
@JsonAnySetter
7690
private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
7791
) {
7892

7993
/** The case sensitive currency or custom pricing unit to use for this alert. */
80-
@JsonProperty("currency") fun currency(): String = currency
94+
fun currency(): String = currency.getRequired("currency")
95+
96+
/** The type of alert to create. This must be a valid alert type. */
97+
fun type(): Type = type.getRequired("type")
98+
99+
/** The thresholds that define the values at which the alert will be triggered. */
100+
fun thresholds(): Optional<List<Threshold>> =
101+
Optional.ofNullable(thresholds.getNullable("thresholds"))
102+
103+
/** The case sensitive currency or custom pricing unit to use for this alert. */
104+
@JsonProperty("currency") @ExcludeMissing fun _currency(): JsonField<String> = currency
81105

82106
/** The type of alert to create. This must be a valid alert type. */
83-
@JsonProperty("type") fun type(): Type = type
107+
@JsonProperty("type") @ExcludeMissing fun _type(): JsonField<Type> = type
84108

85109
/** The thresholds that define the values at which the alert will be triggered. */
86110
@JsonProperty("thresholds")
87-
fun thresholds(): Optional<List<Threshold>> = Optional.ofNullable(thresholds)
111+
@ExcludeMissing
112+
fun _thresholds(): JsonField<List<Threshold>> = thresholds
88113

89114
@JsonAnyGetter
90115
@ExcludeMissing
91116
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
92117

118+
private var validated: Boolean = false
119+
120+
fun validate(): AlertCreateForCustomerBody = apply {
121+
if (!validated) {
122+
currency()
123+
type()
124+
thresholds().map { it.forEach { it.validate() } }
125+
validated = true
126+
}
127+
}
128+
93129
fun toBuilder() = Builder().from(this)
94130

95131
companion object {
@@ -99,38 +135,57 @@ constructor(
99135

100136
class Builder {
101137

102-
private var currency: String? = null
103-
private var type: Type? = null
104-
private var thresholds: MutableList<Threshold>? = null
138+
private var currency: JsonField<String>? = null
139+
private var type: JsonField<Type>? = null
140+
private var thresholds: JsonField<MutableList<Threshold>>? = null
105141
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
106142

107143
@JvmSynthetic
108144
internal fun from(alertCreateForCustomerBody: AlertCreateForCustomerBody) = apply {
109145
currency = alertCreateForCustomerBody.currency
110146
type = alertCreateForCustomerBody.type
111-
thresholds = alertCreateForCustomerBody.thresholds?.toMutableList()
147+
thresholds = alertCreateForCustomerBody.thresholds.map { it.toMutableList() }
112148
additionalProperties =
113149
alertCreateForCustomerBody.additionalProperties.toMutableMap()
114150
}
115151

116152
/** The case sensitive currency or custom pricing unit to use for this alert. */
117-
fun currency(currency: String) = apply { this.currency = currency }
153+
fun currency(currency: String) = currency(JsonField.of(currency))
154+
155+
/** The case sensitive currency or custom pricing unit to use for this alert. */
156+
fun currency(currency: JsonField<String>) = apply { this.currency = currency }
157+
158+
/** The type of alert to create. This must be a valid alert type. */
159+
fun type(type: Type) = type(JsonField.of(type))
118160

119161
/** The type of alert to create. This must be a valid alert type. */
120-
fun type(type: Type) = apply { this.type = type }
162+
fun type(type: JsonField<Type>) = apply { this.type = type }
121163

122164
/** The thresholds that define the values at which the alert will be triggered. */
123-
fun thresholds(thresholds: List<Threshold>?) = apply {
124-
this.thresholds = thresholds?.toMutableList()
125-
}
165+
fun thresholds(thresholds: List<Threshold>?) =
166+
thresholds(JsonField.ofNullable(thresholds))
126167

127168
/** The thresholds that define the values at which the alert will be triggered. */
128169
fun thresholds(thresholds: Optional<List<Threshold>>) =
129170
thresholds(thresholds.orElse(null))
130171

172+
/** The thresholds that define the values at which the alert will be triggered. */
173+
fun thresholds(thresholds: JsonField<List<Threshold>>) = apply {
174+
this.thresholds = thresholds.map { it.toMutableList() }
175+
}
176+
131177
/** The thresholds that define the values at which the alert will be triggered. */
132178
fun addThreshold(threshold: Threshold) = apply {
133-
thresholds = (thresholds ?: mutableListOf()).apply { add(threshold) }
179+
thresholds =
180+
(thresholds ?: JsonField.of(mutableListOf())).apply {
181+
asKnown()
182+
.orElseThrow {
183+
IllegalStateException(
184+
"Field was set to non-list type: ${javaClass.simpleName}"
185+
)
186+
}
187+
.add(threshold)
188+
}
134189
}
135190

136191
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
@@ -156,7 +211,7 @@ constructor(
156211
AlertCreateForCustomerBody(
157212
checkNotNull(currency) { "`currency` is required but was not set" },
158213
checkNotNull(type) { "`type` is required but was not set" },
159-
thresholds?.toImmutable(),
214+
(thresholds ?: JsonMissing.of()).map { it.toImmutable() },
160215
additionalProperties.toImmutable(),
161216
)
162217
}
@@ -207,18 +262,48 @@ constructor(
207262
/** The case sensitive currency or custom pricing unit to use for this alert. */
208263
fun currency(currency: String) = apply { body.currency(currency) }
209264

265+
/** The case sensitive currency or custom pricing unit to use for this alert. */
266+
fun currency(currency: JsonField<String>) = apply { body.currency(currency) }
267+
210268
/** The type of alert to create. This must be a valid alert type. */
211269
fun type(type: Type) = apply { body.type(type) }
212270

271+
/** The type of alert to create. This must be a valid alert type. */
272+
fun type(type: JsonField<Type>) = apply { body.type(type) }
273+
213274
/** The thresholds that define the values at which the alert will be triggered. */
214275
fun thresholds(thresholds: List<Threshold>?) = apply { body.thresholds(thresholds) }
215276

216277
/** The thresholds that define the values at which the alert will be triggered. */
217278
fun thresholds(thresholds: Optional<List<Threshold>>) = thresholds(thresholds.orElse(null))
218279

280+
/** The thresholds that define the values at which the alert will be triggered. */
281+
fun thresholds(thresholds: JsonField<List<Threshold>>) = apply {
282+
body.thresholds(thresholds)
283+
}
284+
219285
/** The thresholds that define the values at which the alert will be triggered. */
220286
fun addThreshold(threshold: Threshold) = apply { body.addThreshold(threshold) }
221287

288+
fun additionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) = apply {
289+
body.additionalProperties(additionalBodyProperties)
290+
}
291+
292+
fun putAdditionalBodyProperty(key: String, value: JsonValue) = apply {
293+
body.putAdditionalProperty(key, value)
294+
}
295+
296+
fun putAllAdditionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) =
297+
apply {
298+
body.putAllAdditionalProperties(additionalBodyProperties)
299+
}
300+
301+
fun removeAdditionalBodyProperty(key: String) = apply { body.removeAdditionalProperty(key) }
302+
303+
fun removeAllAdditionalBodyProperties(keys: Set<String>) = apply {
304+
body.removeAllAdditionalProperties(keys)
305+
}
306+
222307
fun additionalHeaders(additionalHeaders: Headers) = apply {
223308
this.additionalHeaders.clear()
224309
putAllAdditionalHeaders(additionalHeaders)
@@ -317,25 +402,6 @@ constructor(
317402
additionalQueryParams.removeAll(keys)
318403
}
319404

320-
fun additionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) = apply {
321-
body.additionalProperties(additionalBodyProperties)
322-
}
323-
324-
fun putAdditionalBodyProperty(key: String, value: JsonValue) = apply {
325-
body.putAdditionalProperty(key, value)
326-
}
327-
328-
fun putAllAdditionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) =
329-
apply {
330-
body.putAllAdditionalProperties(additionalBodyProperties)
331-
}
332-
333-
fun removeAdditionalBodyProperty(key: String) = apply { body.removeAdditionalProperty(key) }
334-
335-
fun removeAllAdditionalBodyProperties(keys: Set<String>) = apply {
336-
body.removeAllAdditionalProperties(keys)
337-
}
338-
339405
fun build(): AlertCreateForCustomerParams =
340406
AlertCreateForCustomerParams(
341407
checkNotNull(customerId) { "`customerId` is required but was not set" },
@@ -425,7 +491,9 @@ constructor(
425491
class Threshold
426492
@JsonCreator
427493
private constructor(
428-
@JsonProperty("value") private val value: Double,
494+
@JsonProperty("value")
495+
@ExcludeMissing
496+
private val value: JsonField<Double> = JsonMissing.of(),
429497
@JsonAnySetter
430498
private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
431499
) {
@@ -435,12 +503,28 @@ constructor(
435503
* or below this value. For usage and cost alerts, the alert will fire at or above this
436504
* value.
437505
*/
438-
@JsonProperty("value") fun value(): Double = value
506+
fun value(): Double = value.getRequired("value")
507+
508+
/**
509+
* The value at which an alert will fire. For credit balance alerts, the alert will fire at
510+
* or below this value. For usage and cost alerts, the alert will fire at or above this
511+
* value.
512+
*/
513+
@JsonProperty("value") @ExcludeMissing fun _value(): JsonField<Double> = value
439514

440515
@JsonAnyGetter
441516
@ExcludeMissing
442517
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
443518

519+
private var validated: Boolean = false
520+
521+
fun validate(): Threshold = apply {
522+
if (!validated) {
523+
value()
524+
validated = true
525+
}
526+
}
527+
444528
fun toBuilder() = Builder().from(this)
445529

446530
companion object {
@@ -450,7 +534,7 @@ constructor(
450534

451535
class Builder {
452536

453-
private var value: Double? = null
537+
private var value: JsonField<Double>? = null
454538
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
455539

456540
@JvmSynthetic
@@ -464,7 +548,14 @@ constructor(
464548
* at or below this value. For usage and cost alerts, the alert will fire at or above
465549
* this value.
466550
*/
467-
fun value(value: Double) = apply { this.value = value }
551+
fun value(value: Double) = value(JsonField.of(value))
552+
553+
/**
554+
* The value at which an alert will fire. For credit balance alerts, the alert will fire
555+
* at or below this value. For usage and cost alerts, the alert will fire at or above
556+
* this value.
557+
*/
558+
fun value(value: JsonField<Double>) = apply { this.value = value }
468559

469560
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
470561
this.additionalProperties.clear()

0 commit comments

Comments
 (0)