All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.http4k.connect.Paged.kt Maven / Gradle / Ivy

There is a newer version: 5.45.2.0
Show newest version
package org.http4k.connect

import dev.forkhandles.result4k.Result
import dev.forkhandles.result4k.map

/**
 * Represents a Paged response
 */
interface Paged: Iterable {
    fun token(): Token?
    val items: List
    override fun iterator() = items.iterator()
}

/**
 * Superclass for all Paged actions
 */
interface PagedAction, Self : PagedAction> :
    Action> {
    fun next(token: Token): Self
}

/**
 * Paginate the response of the passed action
 */
fun , Rsp : Paged> paginated(
    fn: (Action) -> Result, action: Action
): Sequence, RemoteFailure>> {

    var nextRequest: Action? = action

    return generateSequence {
        nextRequest
            ?.let {
                fn(it).map { rsp ->
                    nextRequest = rsp.token()?.let(it::next)
                    rsp.items
                }
            }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy