Skip to content

Commit 52aab03

Browse files
refactor(client)!: switch query params objects to use QueryParams (#64)
feat(client): add methods for removing additional properties chore(internal): remove unnecessary validation methods chore(internal): swap params body getters to use optionals chore(internal): remove unnecessary nullable annotations style(internal): minor changes to code ordering and syntax # Migration If you were accessing `_additionalProperties()` on a query params object, then previously it returned `Map<String, List<String>>`. Now it returns a dedicated `QueryParams` object. The `QueryParams` class is better suited for mapping a key to multiple values and has similar methods to `Map`.
1 parent d1e3e00 commit 52aab03

File tree

74 files changed

+1914
-1375
lines changed

Some content is hidden

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

74 files changed

+1914
-1375
lines changed

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ class OpenAIOkHttpClient private constructor() {
138138
clientOptions.azureServiceVersion(azureServiceVersion)
139139
}
140140

141-
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
141+
fun organization(organization: String) = apply { clientOptions.organization(organization) }
142142

143-
fun project(project: String?) = apply { clientOptions.project(project) }
143+
fun project(project: String) = apply { clientOptions.project(project) }
144144

145145
fun fromEnv() = apply { clientOptions.fromEnv() }
146146

openai-java-client-okhttp/src/main/kotlin/com/openai/client/okhttp/OpenAIOkHttpClientAsync.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ class OpenAIOkHttpClientAsync private constructor() {
138138
clientOptions.azureServiceVersion(azureServiceVersion)
139139
}
140140

141-
fun organization(organization: String?) = apply { clientOptions.organization(organization) }
141+
fun organization(organization: String) = apply { clientOptions.organization(organization) }
142142

143-
fun project(project: String?) = apply { clientOptions.project(project) }
143+
fun project(project: String) = apply { clientOptions.project(project) }
144144

145145
fun fromEnv() = apply { clientOptions.fromEnv() }
146146

openai-java-core/src/main/kotlin/com/openai/core/ClientOptions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ private constructor(
187187
this.azureServiceVersion = azureServiceVersion
188188
}
189189

190-
fun organization(organization: String?) = apply { this.organization = organization }
190+
fun organization(organization: String) = apply { this.organization = organization }
191191

192-
fun project(project: String?) = apply { this.project = project }
192+
fun project(project: String) = apply { this.project = project }
193193

194194
fun fromEnv() = apply {
195195
val openAIKey = System.getenv("OPENAI_API_KEY")

openai-java-core/src/main/kotlin/com/openai/errors/OpenAIError.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.openai.errors
55
import com.fasterxml.jackson.annotation.JsonAnyGetter
66
import com.fasterxml.jackson.annotation.JsonAnySetter
77
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
8+
import com.openai.core.ExcludeMissing
89
import com.openai.core.JsonValue
910
import com.openai.core.NoAutoDetect
1011
import com.openai.core.toImmutable
@@ -15,6 +16,7 @@ import java.util.Objects
1516
class OpenAIError
1617
private constructor(
1718
@JsonAnyGetter
19+
@ExcludeMissing
1820
@get:JvmName("additionalProperties")
1921
val additionalProperties: Map<String, JsonValue>,
2022
) {

openai-java-core/src/main/kotlin/com/openai/models/Batch.kt

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ private constructor(
4545
private val additionalProperties: Map<String, JsonValue>,
4646
) {
4747

48-
private var validated: Boolean = false
49-
5048
fun id(): String = id.getRequired("id")
5149

5250
/** The object type, which is always `batch`. */
@@ -174,6 +172,8 @@ private constructor(
174172
@ExcludeMissing
175173
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
176174

175+
private var validated: Boolean = false
176+
177177
fun validate(): Batch = apply {
178178
if (!validated) {
179179
id()
@@ -232,27 +232,27 @@ private constructor(
232232

233233
@JvmSynthetic
234234
internal fun from(batch: Batch) = apply {
235-
this.id = batch.id
236-
this.object_ = batch.object_
237-
this.endpoint = batch.endpoint
238-
this.errors = batch.errors
239-
this.inputFileId = batch.inputFileId
240-
this.completionWindow = batch.completionWindow
241-
this.status = batch.status
242-
this.outputFileId = batch.outputFileId
243-
this.errorFileId = batch.errorFileId
244-
this.createdAt = batch.createdAt
245-
this.inProgressAt = batch.inProgressAt
246-
this.expiresAt = batch.expiresAt
247-
this.finalizingAt = batch.finalizingAt
248-
this.completedAt = batch.completedAt
249-
this.failedAt = batch.failedAt
250-
this.expiredAt = batch.expiredAt
251-
this.cancellingAt = batch.cancellingAt
252-
this.cancelledAt = batch.cancelledAt
253-
this.requestCounts = batch.requestCounts
254-
this.metadata = batch.metadata
255-
additionalProperties(batch.additionalProperties)
235+
id = batch.id
236+
object_ = batch.object_
237+
endpoint = batch.endpoint
238+
errors = batch.errors
239+
inputFileId = batch.inputFileId
240+
completionWindow = batch.completionWindow
241+
status = batch.status
242+
outputFileId = batch.outputFileId
243+
errorFileId = batch.errorFileId
244+
createdAt = batch.createdAt
245+
inProgressAt = batch.inProgressAt
246+
expiresAt = batch.expiresAt
247+
finalizingAt = batch.finalizingAt
248+
completedAt = batch.completedAt
249+
failedAt = batch.failedAt
250+
expiredAt = batch.expiredAt
251+
cancellingAt = batch.cancellingAt
252+
cancelledAt = batch.cancelledAt
253+
requestCounts = batch.requestCounts
254+
metadata = batch.metadata
255+
additionalProperties = batch.additionalProperties.toMutableMap()
256256
}
257257

258258
fun id(id: String) = id(JsonField.of(id))
@@ -420,18 +420,24 @@ private constructor(
420420

421421
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
422422
this.additionalProperties.clear()
423-
this.additionalProperties.putAll(additionalProperties)
423+
putAllAdditionalProperties(additionalProperties)
424424
}
425425

426426
@JsonAnySetter
427427
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
428-
this.additionalProperties.put(key, value)
428+
additionalProperties.put(key, value)
429429
}
430430

431431
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
432432
this.additionalProperties.putAll(additionalProperties)
433433
}
434434

435+
fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }
436+
437+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
438+
keys.forEach(::removeAdditionalProperty)
439+
}
440+
435441
fun build(): Batch =
436442
Batch(
437443
id,
@@ -611,8 +617,6 @@ private constructor(
611617
private val additionalProperties: Map<String, JsonValue>,
612618
) {
613619

614-
private var validated: Boolean = false
615-
616620
/** The object type, which is always `list`. */
617621
fun object_(): Optional<String> = Optional.ofNullable(object_.getNullable("object"))
618622

@@ -627,6 +631,8 @@ private constructor(
627631
@ExcludeMissing
628632
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
629633

634+
private var validated: Boolean = false
635+
630636
fun validate(): Errors = apply {
631637
if (!validated) {
632638
object_()
@@ -650,9 +656,9 @@ private constructor(
650656

651657
@JvmSynthetic
652658
internal fun from(errors: Errors) = apply {
653-
this.object_ = errors.object_
654-
this.data = errors.data
655-
additionalProperties(errors.additionalProperties)
659+
object_ = errors.object_
660+
data = errors.data
661+
additionalProperties = errors.additionalProperties.toMutableMap()
656662
}
657663

658664
/** The object type, which is always `list`. */
@@ -671,18 +677,24 @@ private constructor(
671677

672678
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
673679
this.additionalProperties.clear()
674-
this.additionalProperties.putAll(additionalProperties)
680+
putAllAdditionalProperties(additionalProperties)
675681
}
676682

677683
@JsonAnySetter
678684
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
679-
this.additionalProperties.put(key, value)
685+
additionalProperties.put(key, value)
680686
}
681687

682688
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
683689
this.additionalProperties.putAll(additionalProperties)
684690
}
685691

692+
fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }
693+
694+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
695+
keys.forEach(::removeAdditionalProperty)
696+
}
697+
686698
fun build(): Errors =
687699
Errors(
688700
object_,

openai-java-core/src/main/kotlin/com/openai/models/BatchCreateParams.kt

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ constructor(
6363
@NoAutoDetect
6464
class BatchCreateBody
6565
internal constructor(
66-
private val completionWindow: CompletionWindow?,
67-
private val endpoint: Endpoint?,
68-
private val inputFileId: String?,
66+
private val completionWindow: CompletionWindow,
67+
private val endpoint: Endpoint,
68+
private val inputFileId: String,
6969
private val metadata: Metadata?,
7070
private val additionalProperties: Map<String, JsonValue>,
7171
) {
@@ -75,15 +75,15 @@ constructor(
7575
* supported.
7676
*/
7777
@JsonProperty("completion_window")
78-
fun completionWindow(): CompletionWindow? = completionWindow
78+
fun completionWindow(): CompletionWindow = completionWindow
7979

8080
/**
8181
* The endpoint to be used for all requests in the batch. Currently `/v1/chat/completions`,
8282
* `/v1/embeddings`, and `/v1/completions` are supported. Note that `/v1/embeddings` batches
8383
* are also restricted to a maximum of 50,000 embedding inputs across all requests in the
8484
* batch.
8585
*/
86-
@JsonProperty("endpoint") fun endpoint(): Endpoint? = endpoint
86+
@JsonProperty("endpoint") fun endpoint(): Endpoint = endpoint
8787

8888
/**
8989
* The ID of an uploaded file that contains requests for the new batch.
@@ -96,10 +96,10 @@ constructor(
9696
* must be uploaded with the purpose `batch`. The file can contain up to 50,000 requests,
9797
* and can be up to 200 MB in size.
9898
*/
99-
@JsonProperty("input_file_id") fun inputFileId(): String? = inputFileId
99+
@JsonProperty("input_file_id") fun inputFileId(): String = inputFileId
100100

101101
/** Optional custom metadata for the batch. */
102-
@JsonProperty("metadata") fun metadata(): Metadata? = metadata
102+
@JsonProperty("metadata") fun metadata(): Optional<Metadata> = Optional.ofNullable(metadata)
103103

104104
@JsonAnyGetter
105105
@ExcludeMissing
@@ -122,11 +122,11 @@ constructor(
122122

123123
@JvmSynthetic
124124
internal fun from(batchCreateBody: BatchCreateBody) = apply {
125-
this.completionWindow = batchCreateBody.completionWindow
126-
this.endpoint = batchCreateBody.endpoint
127-
this.inputFileId = batchCreateBody.inputFileId
128-
this.metadata = batchCreateBody.metadata
129-
additionalProperties(batchCreateBody.additionalProperties)
125+
completionWindow = batchCreateBody.completionWindow
126+
endpoint = batchCreateBody.endpoint
127+
inputFileId = batchCreateBody.inputFileId
128+
metadata = batchCreateBody.metadata
129+
additionalProperties = batchCreateBody.additionalProperties.toMutableMap()
130130
}
131131

132132
/**
@@ -167,18 +167,24 @@ constructor(
167167

168168
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
169169
this.additionalProperties.clear()
170-
this.additionalProperties.putAll(additionalProperties)
170+
putAllAdditionalProperties(additionalProperties)
171171
}
172172

173173
@JsonAnySetter
174174
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
175-
this.additionalProperties.put(key, value)
175+
additionalProperties.put(key, value)
176176
}
177177

178178
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
179179
this.additionalProperties.putAll(additionalProperties)
180180
}
181181

182+
fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }
183+
184+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
185+
keys.forEach(::removeAdditionalProperty)
186+
}
187+
182188
fun build(): BatchCreateBody =
183189
BatchCreateBody(
184190
checkNotNull(completionWindow) {
@@ -541,23 +547,29 @@ constructor(
541547

542548
@JvmSynthetic
543549
internal fun from(metadata: Metadata) = apply {
544-
additionalProperties(metadata.additionalProperties)
550+
additionalProperties = metadata.additionalProperties.toMutableMap()
545551
}
546552

547553
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
548554
this.additionalProperties.clear()
549-
this.additionalProperties.putAll(additionalProperties)
555+
putAllAdditionalProperties(additionalProperties)
550556
}
551557

552558
@JsonAnySetter
553559
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
554-
this.additionalProperties.put(key, value)
560+
additionalProperties.put(key, value)
555561
}
556562

557563
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
558564
this.additionalProperties.putAll(additionalProperties)
559565
}
560566

567+
fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }
568+
569+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
570+
keys.forEach(::removeAdditionalProperty)
571+
}
572+
561573
fun build(): Metadata = Metadata(additionalProperties.toImmutable())
562574
}
563575

openai-java-core/src/main/kotlin/com/openai/models/BatchError.kt

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ private constructor(
2626
private val additionalProperties: Map<String, JsonValue>,
2727
) {
2828

29-
private var validated: Boolean = false
30-
3129
/** An error code identifying the error type. */
3230
fun code(): Optional<String> = Optional.ofNullable(code.getNullable("code"))
3331

@@ -56,6 +54,8 @@ private constructor(
5654
@ExcludeMissing
5755
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
5856

57+
private var validated: Boolean = false
58+
5959
fun validate(): BatchError = apply {
6060
if (!validated) {
6161
code()
@@ -83,11 +83,11 @@ private constructor(
8383

8484
@JvmSynthetic
8585
internal fun from(batchError: BatchError) = apply {
86-
this.code = batchError.code
87-
this.message = batchError.message
88-
this.param = batchError.param
89-
this.line = batchError.line
90-
additionalProperties(batchError.additionalProperties)
86+
code = batchError.code
87+
message = batchError.message
88+
param = batchError.param
89+
line = batchError.line
90+
additionalProperties = batchError.additionalProperties.toMutableMap()
9191
}
9292

9393
/** An error code identifying the error type. */
@@ -124,18 +124,24 @@ private constructor(
124124

125125
fun additionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
126126
this.additionalProperties.clear()
127-
this.additionalProperties.putAll(additionalProperties)
127+
putAllAdditionalProperties(additionalProperties)
128128
}
129129

130130
@JsonAnySetter
131131
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
132-
this.additionalProperties.put(key, value)
132+
additionalProperties.put(key, value)
133133
}
134134

135135
fun putAllAdditionalProperties(additionalProperties: Map<String, JsonValue>) = apply {
136136
this.additionalProperties.putAll(additionalProperties)
137137
}
138138

139+
fun removeAdditionalProperty(key: String) = apply { additionalProperties.remove(key) }
140+
141+
fun removeAllAdditionalProperties(keys: Set<String>) = apply {
142+
keys.forEach(::removeAdditionalProperty)
143+
}
144+
139145
fun build(): BatchError =
140146
BatchError(
141147
code,

0 commit comments

Comments
 (0)