commonMain.com.algolia.client.extensions.internal.Iterable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of algoliasearch-client-kotlin-jvm Show documentation
Show all versions of algoliasearch-client-kotlin-jvm Show documentation
"Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for Kotlin lets you easily use the Algolia Search REST API from your JVM project, such as Android or backend implementations."
package com.algolia.client.extensions.internal
import com.algolia.client.exception.AlgoliaIterableException
import kotlinx.coroutines.delay
import kotlin.time.Duration
public data class IterableError(
public val validate: (T) -> Boolean,
public val message: ((T) -> String)? = null
)
public suspend fun createIterable(
execute: suspend (T?) -> T,
validate: (T) -> Boolean,
aggregator: ((T) -> Unit)? = null,
timeout: () -> Duration = { Duration.ZERO },
error: IterableError? = null
): T {
suspend fun executor(previousResponse: T? = null): T {
val response = execute(previousResponse)
if (aggregator != null) {
aggregator(response)
}
if (validate(response)) {
return response
}
if (error != null && error.validate(response)) {
val message = error.message?.invoke(response) ?: "An error occurred"
throw AlgoliaIterableException(message)
}
delay(timeout().inWholeMilliseconds)
return executor(response)
}
return executor()
}