Skip to content

Commit

Permalink
feat(client): add various convenience setters to models (#27)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
stainless-app[bot] committed Jan 7, 2025
1 parent f5509ab commit 43f12a3
Show file tree
Hide file tree
Showing 11 changed files with 986 additions and 401 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ private constructor(

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

fun responseValidation(responseValidation: Boolean) = apply {
this.responseValidation = responseValidation
}

fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }

fun apiToken(apiToken: String) = apply { this.apiToken = apiToken }

fun headers(headers: Headers) = apply {
this.headers.clear()
putAllHeaders(headers)
Expand Down Expand Up @@ -148,14 +156,6 @@ private constructor(

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

fun responseValidation(responseValidation: Boolean) = apply {
this.responseValidation = responseValidation
}

fun maxRetries(maxRetries: Int) = apply { this.maxRetries = maxRetries }

fun apiToken(apiToken: String) = apply { this.apiToken = apiToken }

fun fromEnv() = apply { System.getenv("API_TOKEN")?.let { apiToken(it) } }

fun build(): ClientOptions {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -75,31 +75,39 @@ private constructor(
fun from(): Optional<String> = Optional.ofNullable(from.getNullable("from"))

/** The message identifier. */
@JsonProperty("id") @ExcludeMissing fun _id() = id
@JsonProperty("id") @ExcludeMissing fun _id(): JsonField<String> = id

/** The message creation date. */
@JsonProperty("created_at") @ExcludeMissing fun _createdAt() = createdAt
@JsonProperty("created_at")
@ExcludeMissing
fun _createdAt(): JsonField<OffsetDateTime> = createdAt

/** The message expiration date. */
@JsonProperty("expires_at") @ExcludeMissing fun _expiresAt() = expiresAt
@JsonProperty("expires_at")
@ExcludeMissing
fun _expiresAt(): JsonField<OffsetDateTime> = expiresAt

/** The template identifier. */
@JsonProperty("template_id") @ExcludeMissing fun _templateId() = templateId
@JsonProperty("template_id") @ExcludeMissing fun _templateId(): JsonField<String> = templateId

/** The recipient's phone number. */
@JsonProperty("to") @ExcludeMissing fun _to() = to
@JsonProperty("to") @ExcludeMissing fun _to(): JsonField<String> = to

/** The variables to be replaced in the template. */
@JsonProperty("variables") @ExcludeMissing fun _variables() = variables
@JsonProperty("variables") @ExcludeMissing fun _variables(): JsonField<Variables> = variables

/** The callback URL. */
@JsonProperty("callback_url") @ExcludeMissing fun _callbackUrl() = callbackUrl
@JsonProperty("callback_url")
@ExcludeMissing
fun _callbackUrl(): JsonField<String> = callbackUrl

/** A unique, user-defined identifier that will be included in webhook events. */
@JsonProperty("correlation_id") @ExcludeMissing fun _correlationId() = correlationId
@JsonProperty("correlation_id")
@ExcludeMissing
fun _correlationId(): JsonField<String> = correlationId

/** The Sender ID. */
@JsonProperty("from") @ExcludeMissing fun _from() = from
@JsonProperty("from") @ExcludeMissing fun _from(): JsonField<String> = from

@JsonAnyGetter
@ExcludeMissing
Expand Down Expand Up @@ -131,12 +139,12 @@ private constructor(

class Builder {

private var id: JsonField<String> = JsonMissing.of()
private var createdAt: JsonField<OffsetDateTime> = JsonMissing.of()
private var expiresAt: JsonField<OffsetDateTime> = JsonMissing.of()
private var templateId: JsonField<String> = JsonMissing.of()
private var to: JsonField<String> = JsonMissing.of()
private var variables: JsonField<Variables> = JsonMissing.of()
private var id: JsonField<String>? = null
private var createdAt: JsonField<OffsetDateTime>? = null
private var expiresAt: JsonField<OffsetDateTime>? = null
private var templateId: JsonField<String>? = null
private var to: JsonField<String>? = null
private var variables: JsonField<Variables>? = null
private var callbackUrl: JsonField<String> = JsonMissing.of()
private var correlationId: JsonField<String> = JsonMissing.of()
private var from: JsonField<String> = JsonMissing.of()
Expand Down Expand Up @@ -233,12 +241,12 @@ private constructor(

fun build(): TransactionalSendResponse =
TransactionalSendResponse(
id,
createdAt,
expiresAt,
templateId,
to,
variables,
checkNotNull(id) { "`id` is required but was not set" },
checkNotNull(createdAt) { "`createdAt` is required but was not set" },
checkNotNull(expiresAt) { "`expiresAt` is required but was not set" },
checkNotNull(templateId) { "`templateId` is required but was not set" },
checkNotNull(to) { "`to` is required but was not set" },
checkNotNull(variables) { "`variables` is required but was not set" },
callbackUrl,
correlationId,
from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import java.util.Objects
import so.prelude.sdk.core.Enum
import so.prelude.sdk.core.ExcludeMissing
import so.prelude.sdk.core.JsonField
import so.prelude.sdk.core.JsonMissing
import so.prelude.sdk.core.JsonValue
import so.prelude.sdk.core.NoAutoDetect
import so.prelude.sdk.core.http.Headers
Expand All @@ -32,12 +33,18 @@ constructor(
/** The target. Currently this can only be an E.164 formatted phone number. */
fun target(): Target = body.target()

fun _additionalHeaders(): Headers = additionalHeaders
/** The OTP code to validate. */
fun _code(): JsonField<String> = body._code()

fun _additionalQueryParams(): QueryParams = additionalQueryParams
/** The target. Currently this can only be an E.164 formatted phone number. */
fun _target(): JsonField<Target> = body._target()

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

fun _additionalHeaders(): Headers = additionalHeaders

fun _additionalQueryParams(): QueryParams = additionalQueryParams

@JvmSynthetic internal fun getBody(): VerificationCheckBody = body

@JvmSynthetic internal fun getHeaders(): Headers = additionalHeaders
Expand All @@ -48,22 +55,42 @@ constructor(
class VerificationCheckBody
@JsonCreator
internal constructor(
@JsonProperty("code") private val code: String,
@JsonProperty("target") private val target: Target,
@JsonProperty("code")
@ExcludeMissing
private val code: JsonField<String> = JsonMissing.of(),
@JsonProperty("target")
@ExcludeMissing
private val target: JsonField<Target> = JsonMissing.of(),
@JsonAnySetter
private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
) {

/** The OTP code to validate. */
@JsonProperty("code") fun code(): String = code
fun code(): String = code.getRequired("code")

/** The target. Currently this can only be an E.164 formatted phone number. */
@JsonProperty("target") fun target(): Target = target
fun target(): Target = target.getRequired("target")

/** The OTP code to validate. */
@JsonProperty("code") @ExcludeMissing fun _code(): JsonField<String> = code

/** The target. Currently this can only be an E.164 formatted phone number. */
@JsonProperty("target") @ExcludeMissing fun _target(): JsonField<Target> = target

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

private var validated: Boolean = false

fun validate(): VerificationCheckBody = apply {
if (!validated) {
code()
target().validate()
validated = true
}
}

fun toBuilder() = Builder().from(this)

companion object {
Expand All @@ -73,8 +100,8 @@ constructor(

class Builder {

private var code: String? = null
private var target: Target? = null
private var code: JsonField<String>? = null
private var target: JsonField<Target>? = null
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

@JvmSynthetic
Expand All @@ -85,10 +112,16 @@ constructor(
}

/** The OTP code to validate. */
fun code(code: String) = apply { this.code = code }
fun code(code: String) = code(JsonField.of(code))

/** The OTP code to validate. */
fun code(code: JsonField<String>) = apply { this.code = code }

/** The target. Currently this can only be an E.164 formatted phone number. */
fun target(target: Target) = apply { this.target = target }
fun target(target: Target) = target(JsonField.of(target))

/** The target. Currently this can only be an E.164 formatted phone number. */
fun target(target: JsonField<Target>) = apply { this.target = target }

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
Expand Down Expand Up @@ -159,9 +192,34 @@ constructor(
/** The OTP code to validate. */
fun code(code: String) = apply { body.code(code) }

/** The OTP code to validate. */
fun code(code: JsonField<String>) = apply { body.code(code) }

/** The target. Currently this can only be an E.164 formatted phone number. */
fun target(target: Target) = apply { body.target(target) }

/** The target. Currently this can only be an E.164 formatted phone number. */
fun target(target: JsonField<Target>) = apply { body.target(target) }

fun additionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) = apply {
body.additionalProperties(additionalBodyProperties)
}

fun putAdditionalBodyProperty(key: String, value: JsonValue) = apply {
body.putAdditionalProperty(key, value)
}

fun putAllAdditionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) =
apply {
body.putAllAdditionalProperties(additionalBodyProperties)
}

fun removeAdditionalBodyProperty(key: String) = apply { body.removeAdditionalProperty(key) }

fun removeAllAdditionalBodyProperties(keys: Set<String>) = apply {
body.removeAllAdditionalProperties(keys)
}

fun additionalHeaders(additionalHeaders: Headers) = apply {
this.additionalHeaders.clear()
putAllAdditionalHeaders(additionalHeaders)
Expand Down Expand Up @@ -260,25 +318,6 @@ constructor(
additionalQueryParams.removeAll(keys)
}

fun additionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) = apply {
body.additionalProperties(additionalBodyProperties)
}

fun putAdditionalBodyProperty(key: String, value: JsonValue) = apply {
body.putAdditionalProperty(key, value)
}

fun putAllAdditionalBodyProperties(additionalBodyProperties: Map<String, JsonValue>) =
apply {
body.putAllAdditionalProperties(additionalBodyProperties)
}

fun removeAdditionalBodyProperty(key: String) = apply { body.removeAdditionalProperty(key) }

fun removeAllAdditionalBodyProperties(keys: Set<String>) = apply {
body.removeAllAdditionalProperties(keys)
}

fun build(): VerificationCheckParams =
VerificationCheckParams(
body.build(),
Expand All @@ -292,22 +331,40 @@ constructor(
class Target
@JsonCreator
private constructor(
@JsonProperty("type") private val type: Type,
@JsonProperty("value") private val value: String,
@JsonProperty("type") @ExcludeMissing private val type: JsonField<Type> = JsonMissing.of(),
@JsonProperty("value")
@ExcludeMissing
private val value: JsonField<String> = JsonMissing.of(),
@JsonAnySetter
private val additionalProperties: Map<String, JsonValue> = immutableEmptyMap(),
) {

/** The type of the target. Currently this can only be "phone_number". */
@JsonProperty("type") fun type(): Type = type
fun type(): Type = type.getRequired("type")

/** An E.164 formatted phone number to verify. */
@JsonProperty("value") fun value(): String = value
fun value(): String = value.getRequired("value")

/** The type of the target. Currently this can only be "phone_number". */
@JsonProperty("type") @ExcludeMissing fun _type(): JsonField<Type> = type

/** An E.164 formatted phone number to verify. */
@JsonProperty("value") @ExcludeMissing fun _value(): JsonField<String> = value

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

private var validated: Boolean = false

fun validate(): Target = apply {
if (!validated) {
type()
value()
validated = true
}
}

fun toBuilder() = Builder().from(this)

companion object {
Expand All @@ -317,8 +374,8 @@ constructor(

class Builder {

private var type: Type? = null
private var value: String? = null
private var type: JsonField<Type>? = null
private var value: JsonField<String>? = null
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

@JvmSynthetic
Expand All @@ -329,10 +386,16 @@ constructor(
}

/** The type of the target. Currently this can only be "phone_number". */
fun type(type: Type) = apply { this.type = type }
fun type(type: Type) = type(JsonField.of(type))

/** The type of the target. Currently this can only be "phone_number". */
fun type(type: JsonField<Type>) = apply { this.type = type }

/** An E.164 formatted phone number to verify. */
fun value(value: String) = value(JsonField.of(value))

/** An E.164 formatted phone number to verify. */
fun value(value: String) = apply { this.value = value }
fun value(value: JsonField<String>) = apply { this.value = value }

fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
this.additionalProperties.clear()
Expand Down
Loading

0 comments on commit 43f12a3

Please sign in to comment.