Skip to content

Commit

Permalink
feat(api): add event message attempts (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-bot committed Jul 18, 2023
1 parent 5a741c3 commit 3eab17c
Show file tree
Hide file tree
Showing 21 changed files with 2,202 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
configured_endpoints: 72
configured_endpoints: 74
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package com.lithic.api.models

import com.fasterxml.jackson.annotation.JsonAnyGetter
import com.fasterxml.jackson.annotation.JsonAnySetter
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.lithic.api.core.ExcludeMissing
import com.lithic.api.core.JsonField
import com.lithic.api.core.JsonMissing
import com.lithic.api.core.JsonValue
import com.lithic.api.core.NoAutoDetect
import com.lithic.api.core.toUnmodifiable
import com.lithic.api.services.blocking.EventService
import java.util.Objects
import java.util.Optional
import java.util.stream.Stream
import java.util.stream.StreamSupport

class EventListAttemptsPage
private constructor(
private val eventsService: EventService,
private val params: EventListAttemptsParams,
private val response: Response,
) {

fun response(): Response = response

fun data(): List<MessageAttempt> = response().data()

fun hasMore(): Boolean = response().hasMore()

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is EventListAttemptsPage &&
this.eventsService == other.eventsService &&
this.params == other.params &&
this.response == other.response
}

override fun hashCode(): Int {
return Objects.hash(
eventsService,
params,
response,
)
}

override fun toString() =
"EventListAttemptsPage{eventsService=$eventsService, params=$params, response=$response}"

fun hasNextPage(): Boolean {
return data().isEmpty()
}

fun getNextPageParams(): Optional<EventListAttemptsParams> {
if (!hasNextPage()) {
return Optional.empty()
}

return if (params.endingBefore().isPresent) {
Optional.of(
EventListAttemptsParams.builder()
.from(params)
.endingBefore(data().first().token())
.build()
)
} else {
Optional.of(
EventListAttemptsParams.builder()
.from(params)
.startingAfter(data().last().token())
.build()
)
}
}

fun getNextPage(): Optional<EventListAttemptsPage> {
return getNextPageParams().map { eventsService.listAttempts(it) }
}

fun autoPager(): AutoPager = AutoPager(this)

companion object {

@JvmStatic
fun of(eventsService: EventService, params: EventListAttemptsParams, response: Response) =
EventListAttemptsPage(
eventsService,
params,
response,
)
}

@JsonDeserialize(builder = Response.Builder::class)
@NoAutoDetect
class Response
constructor(
private val data: JsonField<List<MessageAttempt>>,
private val hasMore: JsonField<Boolean>,
private val additionalProperties: Map<String, JsonValue>,
) {

private var validated: Boolean = false

fun data(): List<MessageAttempt> = data.getNullable("data") ?: listOf()

fun hasMore(): Boolean = hasMore.getRequired("has_more")

@JsonProperty("data")
fun _data(): Optional<JsonField<List<MessageAttempt>>> = Optional.ofNullable(data)

@JsonProperty("has_more")
fun _hasMore(): Optional<JsonField<Boolean>> = Optional.ofNullable(hasMore)

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

fun validate(): Response = apply {
if (!validated) {
data().map { it.validate() }
hasMore()
validated = true
}
}

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

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
}

return other is Response &&
this.data == other.data &&
this.hasMore == other.hasMore &&
this.additionalProperties == other.additionalProperties
}

override fun hashCode(): Int {
return Objects.hash(
data,
hasMore,
additionalProperties,
)
}

override fun toString() =
"EventListAttemptsPage.Response{data=$data, hasMore=$hasMore, additionalProperties=$additionalProperties}"

companion object {

@JvmStatic fun builder() = Builder()
}

class Builder {

private var data: JsonField<List<MessageAttempt>> = JsonMissing.of()
private var hasMore: JsonField<Boolean> = JsonMissing.of()
private var additionalProperties: MutableMap<String, JsonValue> = mutableMapOf()

@JvmSynthetic
internal fun from(page: Response) = apply {
this.data = page.data
this.hasMore = page.hasMore
this.additionalProperties.putAll(page.additionalProperties)
}

fun data(data: List<MessageAttempt>) = data(JsonField.of(data))

@JsonProperty("data")
fun data(data: JsonField<List<MessageAttempt>>) = apply { this.data = data }

fun hasMore(hasMore: Boolean) = hasMore(JsonField.of(hasMore))

@JsonProperty("has_more")
fun hasMore(hasMore: JsonField<Boolean>) = apply { this.hasMore = hasMore }

@JsonAnySetter
fun putAdditionalProperty(key: String, value: JsonValue) = apply {
this.additionalProperties.put(key, value)
}

fun build() =
Response(
data,
hasMore,
additionalProperties.toUnmodifiable(),
)
}
}

class AutoPager
constructor(
private val firstPage: EventListAttemptsPage,
) : Iterable<MessageAttempt> {

override fun iterator(): Iterator<MessageAttempt> = iterator {
var page = firstPage
var index = 0
while (true) {
while (index < page.data().size) {
yield(page.data()[index++])
}
page = page.getNextPage().orElse(null) ?: break
index = 0
}
}

fun stream(): Stream<MessageAttempt> {
return StreamSupport.stream(spliterator(), false)
}
}
}
Loading

0 comments on commit 3eab17c

Please sign in to comment.