2
2
3
3
package com.openai.models.batches
4
4
5
- import com.fasterxml.jackson.annotation.JsonAnyGetter
6
- import com.fasterxml.jackson.annotation.JsonAnySetter
7
- import com.fasterxml.jackson.annotation.JsonCreator
8
- import com.fasterxml.jackson.annotation.JsonProperty
9
- import com.openai.core.ExcludeMissing
10
- import com.openai.core.JsonField
11
- import com.openai.core.JsonMissing
12
- import com.openai.core.JsonValue
13
- import com.openai.errors.OpenAIInvalidDataException
5
+ import com.openai.core.checkRequired
14
6
import com.openai.services.blocking.BatchService
15
- import java.util.Collections
16
7
import java.util.Objects
17
8
import java.util.Optional
18
9
import java.util.stream.Stream
19
10
import java.util.stream.StreamSupport
20
11
import kotlin.jvm.optionals.getOrNull
21
12
22
- /* * List your organization's batches. */
13
+ /* * @see [BatchService.list] */
23
14
class BatchListPage
24
15
private constructor (
25
- private val batchesService : BatchService ,
16
+ private val service : BatchService ,
26
17
private val params: BatchListParams ,
27
- private val response: Response ,
18
+ private val response: BatchListPageResponse ,
28
19
) {
29
20
30
- fun response (): Response = response
21
+ /* *
22
+ * Delegates to [BatchListPageResponse], but gracefully handles missing data.
23
+ *
24
+ * @see [BatchListPageResponse.data]
25
+ */
26
+ fun data (): List <Batch > = response._data ().getOptional(" data" ).getOrNull() ? : emptyList()
31
27
32
- fun data (): List <Batch > = response().data()
28
+ /* *
29
+ * Delegates to [BatchListPageResponse], but gracefully handles missing data.
30
+ *
31
+ * @see [BatchListPageResponse.hasMore]
32
+ */
33
+ fun hasMore (): Optional <Boolean > = response._hasMore ().getOptional(" has_more" )
33
34
34
- fun hasMore (): Optional <Boolean > = response().hasMore()
35
-
36
- override fun equals (other : Any? ): Boolean {
37
- if (this == = other) {
38
- return true
39
- }
40
-
41
- return /* spotless:off */ other is BatchListPage && batchesService == other.batchesService && params == other.params && response == other.response /* spotless:on */
42
- }
43
-
44
- override fun hashCode (): Int = /* spotless:off */ Objects .hash(batchesService, params, response) /* spotless:on */
45
-
46
- override fun toString () =
47
- " BatchListPage{batchesService=$batchesService , params=$params , response=$response }"
48
-
49
- fun hasNextPage (): Boolean {
50
- return ! data().isEmpty()
51
- }
35
+ fun hasNextPage (): Boolean = data().isNotEmpty()
52
36
53
37
fun getNextPageParams (): Optional <BatchListParams > {
54
38
if (! hasNextPage()) {
55
39
return Optional .empty()
56
40
}
57
41
58
- return Optional .of(params.toBuilder().after(data().last().id( )).build())
42
+ return Optional .of(params.toBuilder().after(data().last()._id ().getOptional( " id " )).build())
59
43
}
60
44
61
- fun getNextPage (): Optional <BatchListPage > {
62
- return getNextPageParams().map { batchesService.list(it) }
63
- }
45
+ fun getNextPage (): Optional <BatchListPage > = getNextPageParams().map { service.list(it) }
64
46
65
47
fun autoPager (): AutoPager = AutoPager (this )
66
48
67
- companion object {
68
-
69
- @JvmStatic
70
- fun of (batchesService : BatchService , params : BatchListParams , response : Response ) =
71
- BatchListPage (batchesService, params, response)
72
- }
73
-
74
- class Response (
75
- private val data : JsonField <List <Batch >>,
76
- private val hasMore : JsonField <Boolean >,
77
- private val additionalProperties : MutableMap <String , JsonValue >,
78
- ) {
79
-
80
- @JsonCreator
81
- private constructor (
82
- @JsonProperty(" data" ) data: JsonField <List <Batch >> = JsonMissing .of(),
83
- @JsonProperty(" has_more" ) hasMore: JsonField <Boolean > = JsonMissing .of(),
84
- ) : this (data, hasMore, mutableMapOf ())
85
-
86
- fun data (): List <Batch > = data.getOptional(" data" ).getOrNull() ? : listOf ()
87
-
88
- fun hasMore (): Optional <Boolean > = hasMore.getOptional(" has_more" )
89
-
90
- @JsonProperty(" data" )
91
- fun _data (): Optional <JsonField <List <Batch >>> = Optional .ofNullable(data)
92
-
93
- @JsonProperty(" has_more" )
94
- fun _hasMore (): Optional <JsonField <Boolean >> = Optional .ofNullable(hasMore)
49
+ /* * The parameters that were used to request this page. */
50
+ fun params (): BatchListParams = params
95
51
96
- @JsonAnySetter
97
- private fun putAdditionalProperty (key : String , value : JsonValue ) {
98
- additionalProperties.put(key, value)
99
- }
100
-
101
- @JsonAnyGetter
102
- @ExcludeMissing
103
- fun _additionalProperties (): Map <String , JsonValue > =
104
- Collections .unmodifiableMap(additionalProperties)
52
+ /* * The response that this page was parsed from. */
53
+ fun response (): BatchListPageResponse = response
105
54
106
- private var validated: Boolean = false
107
-
108
- fun validate (): Response = apply {
109
- if (validated) {
110
- return @apply
111
- }
55
+ fun toBuilder () = Builder ().from(this )
112
56
113
- data().map { it.validate() }
114
- hasMore()
115
- validated = true
116
- }
117
-
118
- fun isValid (): Boolean =
119
- try {
120
- validate()
121
- true
122
- } catch (e: OpenAIInvalidDataException ) {
123
- false
124
- }
125
-
126
- fun toBuilder () = Builder ().from(this )
127
-
128
- override fun equals (other : Any? ): Boolean {
129
- if (this == = other) {
130
- return true
131
- }
132
-
133
- return /* spotless:off */ other is Response && data == other.data && hasMore == other.hasMore && additionalProperties == other.additionalProperties /* spotless:on */
134
- }
57
+ companion object {
135
58
136
- override fun hashCode (): Int = /* spotless:off */ Objects .hash(data, hasMore, additionalProperties) /* spotless:on */
59
+ /* *
60
+ * Returns a mutable builder for constructing an instance of [BatchListPage].
61
+ *
62
+ * The following fields are required:
63
+ * ```java
64
+ * .service()
65
+ * .params()
66
+ * .response()
67
+ * ```
68
+ */
69
+ @JvmStatic fun builder () = Builder ()
70
+ }
137
71
138
- override fun toString () =
139
- " Response{data= $data , hasMore= $hasMore , additionalProperties= $additionalProperties } "
72
+ /* * A builder for [BatchListPage]. */
73
+ class Builder internal constructor() {
140
74
141
- companion object {
75
+ private var service: BatchService ? = null
76
+ private var params: BatchListParams ? = null
77
+ private var response: BatchListPageResponse ? = null
142
78
143
- /* * Returns a mutable builder for constructing an instance of [BatchListPage]. */
144
- @JvmStatic fun builder () = Builder ()
79
+ @JvmSynthetic
80
+ internal fun from (batchListPage : BatchListPage ) = apply {
81
+ service = batchListPage.service
82
+ params = batchListPage.params
83
+ response = batchListPage.response
145
84
}
146
85
147
- class Builder {
148
-
149
- private var data: JsonField <List <Batch >> = JsonMissing .of()
150
- private var hasMore: JsonField <Boolean > = JsonMissing .of()
151
- private var additionalProperties: MutableMap <String , JsonValue > = mutableMapOf ()
152
-
153
- @JvmSynthetic
154
- internal fun from (page : Response ) = apply {
155
- this .data = page.data
156
- this .hasMore = page.hasMore
157
- this .additionalProperties.putAll(page.additionalProperties)
158
- }
159
-
160
- fun data (data : List <Batch >) = data(JsonField .of(data))
161
-
162
- fun data (data : JsonField <List <Batch >>) = apply { this .data = data }
163
-
164
- fun hasMore (hasMore : Boolean ) = hasMore(JsonField .of(hasMore))
165
-
166
- fun hasMore (hasMore : JsonField <Boolean >) = apply { this .hasMore = hasMore }
167
-
168
- fun putAdditionalProperty (key : String , value : JsonValue ) = apply {
169
- this .additionalProperties.put(key, value)
170
- }
171
-
172
- /* *
173
- * Returns an immutable instance of [Response].
174
- *
175
- * Further updates to this [Builder] will not mutate the returned instance.
176
- */
177
- fun build (): Response = Response (data, hasMore, additionalProperties.toMutableMap())
178
- }
86
+ fun service (service : BatchService ) = apply { this .service = service }
87
+
88
+ /* * The parameters that were used to request this page. */
89
+ fun params (params : BatchListParams ) = apply { this .params = params }
90
+
91
+ /* * The response that this page was parsed from. */
92
+ fun response (response : BatchListPageResponse ) = apply { this .response = response }
93
+
94
+ /* *
95
+ * Returns an immutable instance of [BatchListPage].
96
+ *
97
+ * Further updates to this [Builder] will not mutate the returned instance.
98
+ *
99
+ * The following fields are required:
100
+ * ```java
101
+ * .service()
102
+ * .params()
103
+ * .response()
104
+ * ```
105
+ *
106
+ * @throws IllegalStateException if any required field is unset.
107
+ */
108
+ fun build (): BatchListPage =
109
+ BatchListPage (
110
+ checkRequired(" service" , service),
111
+ checkRequired(" params" , params),
112
+ checkRequired(" response" , response),
113
+ )
179
114
}
180
115
181
116
class AutoPager (private val firstPage : BatchListPage ) : Iterable<Batch> {
@@ -196,4 +131,16 @@ private constructor(
196
131
return StreamSupport .stream(spliterator(), false )
197
132
}
198
133
}
134
+
135
+ override fun equals (other : Any? ): Boolean {
136
+ if (this == = other) {
137
+ return true
138
+ }
139
+
140
+ return /* spotless:off */ other is BatchListPage && service == other.service && params == other.params && response == other.response /* spotless:on */
141
+ }
142
+
143
+ override fun hashCode (): Int = /* spotless:off */ Objects .hash(service, params, response) /* spotless:on */
144
+
145
+ override fun toString () = " BatchListPage{service=$service , params=$params , response=$response }"
199
146
}
0 commit comments