Skip to content

Commit 3eab17c

Browse files
committed
feat(api): add event message attempts (#28)
1 parent 5a741c3 commit 3eab17c

21 files changed

Lines changed: 2202 additions & 1 deletion

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
configured_endpoints: 72
1+
configured_endpoints: 74
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
package com.lithic.api.models
2+
3+
import com.fasterxml.jackson.annotation.JsonAnyGetter
4+
import com.fasterxml.jackson.annotation.JsonAnySetter
5+
import com.fasterxml.jackson.annotation.JsonProperty
6+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
7+
import com.lithic.api.core.ExcludeMissing
8+
import com.lithic.api.core.JsonField
9+
import com.lithic.api.core.JsonMissing
10+
import com.lithic.api.core.JsonValue
11+
import com.lithic.api.core.NoAutoDetect
12+
import com.lithic.api.core.toUnmodifiable
13+
import com.lithic.api.services.blocking.EventService
14+
import java.util.Objects
15+
import java.util.Optional
16+
import java.util.stream.Stream
17+
import java.util.stream.StreamSupport
18+
19+
class EventListAttemptsPage
20+
private constructor(
21+
private val eventsService: EventService,
22+
private val params: EventListAttemptsParams,
23+
private val response: Response,
24+
) {
25+
26+
fun response(): Response = response
27+
28+
fun data(): List<MessageAttempt> = response().data()
29+
30+
fun hasMore(): Boolean = response().hasMore()
31+
32+
override fun equals(other: Any?): Boolean {
33+
if (this === other) {
34+
return true
35+
}
36+
37+
return other is EventListAttemptsPage &&
38+
this.eventsService == other.eventsService &&
39+
this.params == other.params &&
40+
this.response == other.response
41+
}
42+
43+
override fun hashCode(): Int {
44+
return Objects.hash(
45+
eventsService,
46+
params,
47+
response,
48+
)
49+
}
50+
51+
override fun toString() =
52+
"EventListAttemptsPage{eventsService=$eventsService, params=$params, response=$response}"
53+
54+
fun hasNextPage(): Boolean {
55+
return data().isEmpty()
56+
}
57+
58+
fun getNextPageParams(): Optional<EventListAttemptsParams> {
59+
if (!hasNextPage()) {
60+
return Optional.empty()
61+
}
62+
63+
return if (params.endingBefore().isPresent) {
64+
Optional.of(
65+
EventListAttemptsParams.builder()
66+
.from(params)
67+
.endingBefore(data().first().token())
68+
.build()
69+
)
70+
} else {
71+
Optional.of(
72+
EventListAttemptsParams.builder()
73+
.from(params)
74+
.startingAfter(data().last().token())
75+
.build()
76+
)
77+
}
78+
}
79+
80+
fun getNextPage(): Optional<EventListAttemptsPage> {
81+
return getNextPageParams().map { eventsService.listAttempts(it) }
82+
}
83+
84+
fun autoPager(): AutoPager = AutoPager(this)
85+
86+
companion object {
87+
88+
@JvmStatic
89+
fun of(eventsService: EventService, params: EventListAttemptsParams, response: Response) =
90+
EventListAttemptsPage(
91+
eventsService,
92+
params,
93+
response,
94+
)
95+
}
96+
97+
@JsonDeserialize(builder = Response.Builder::class)
98+
@NoAutoDetect
99+
class Response
100+
constructor(
101+
private val data: JsonField<List<MessageAttempt>>,
102+
private val hasMore: JsonField<Boolean>,
103+
private val additionalProperties: Map<String, JsonValue>,
104+
) {
105+
106+
private var validated: Boolean = false
107+
108+
fun data(): List<MessageAttempt> = data.getNullable("data") ?: listOf()
109+
110+
fun hasMore(): Boolean = hasMore.getRequired("has_more")
111+
112+
@JsonProperty("data")
113+
fun _data(): Optional<JsonField<List<MessageAttempt>>> = Optional.ofNullable(data)
114+
115+
@JsonProperty("has_more")
116+
fun _hasMore(): Optional<JsonField<Boolean>> = Optional.ofNullable(hasMore)
117+
118+
@JsonAnyGetter
119+
@ExcludeMissing
120+
fun _additionalProperties(): Map<String, JsonValue> = additionalProperties
121+
122+
fun validate(): Response = apply {
123+
if (!validated) {
124+
data().map { it.validate() }
125+
hasMore()
126+
validated = true
127+
}
128+
}
129+
130+
fun toBuilder() = Builder().from(this)
131+
132+
override fun equals(other: Any?): Boolean {
133+
if (this === other) {
134+
return true
135+
}
136+
137+
return other is Response &&
138+
this.data == other.data &&
139+
this.hasMore == other.hasMore &&
140+
this.additionalProperties == other.additionalProperties
141+
}
142+
143+
override fun hashCode(): Int {
144+
return Objects.hash(
145+
data,
146+
hasMore,
147+
additionalProperties,
148+
)
149+
}
150+
151+
override fun toString() =
152+
"EventListAttemptsPage.Response{data=$data, hasMore=$hasMore, additionalProperties=$additionalProperties}"
153+
154+
companion object {
155+
156+
@JvmStatic fun builder() = Builder()
157+
}
158+
159+
class Builder {
160+
161+
private var data: JsonField<List<MessageAttempt>> = JsonMissing.of()
162+
private var hasMore: JsonField<Boolean> = JsonMissing.of()
163+
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()
164+
165+
@JvmSynthetic
166+
internal fun from(page: Response) = apply {
167+
this.data = page.data
168+
this.hasMore = page.hasMore
169+
this.additionalProperties.putAll(page.additionalProperties)
170+
}
171+
172+
fun data(data: List<MessageAttempt>) = data(JsonField.of(data))
173+
174+
@JsonProperty("data")
175+
fun data(data: JsonField<List<MessageAttempt>>) = apply { this.data = data }
176+
177+
fun hasMore(hasMore: Boolean) = hasMore(JsonField.of(hasMore))
178+
179+
@JsonProperty("has_more")
180+
fun hasMore(hasMore: JsonField<Boolean>) = apply { this.hasMore = hasMore }
181+
182+
@JsonAnySetter
183+
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
184+
this.additionalProperties.put(key, value)
185+
}
186+
187+
fun build() =
188+
Response(
189+
data,
190+
hasMore,
191+
additionalProperties.toUnmodifiable(),
192+
)
193+
}
194+
}
195+
196+
class AutoPager
197+
constructor(
198+
private val firstPage: EventListAttemptsPage,
199+
) : Iterable<MessageAttempt> {
200+
201+
override fun iterator(): Iterator<MessageAttempt> = iterator {
202+
var page = firstPage
203+
var index = 0
204+
while (true) {
205+
while (index < page.data().size) {
206+
yield(page.data()[index++])
207+
}
208+
page = page.getNextPage().orElse(null) ?: break
209+
index = 0
210+
}
211+
}
212+
213+
fun stream(): Stream<MessageAttempt> {
214+
return StreamSupport.stream(spliterator(), false)
215+
}
216+
}
217+
}

0 commit comments

Comments
 (0)