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

com.linkedplanet.kotlinhttpclient.api.http.Utils.kt Maven / Gradle / Ivy

The newest version!
/*-
 * #%L
 * kotlin-http-client-api
 * %%
 * Copyright (C) 2022 - 2023 linked-planet GmbH
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.linkedplanet.kotlinhttpclient.api.http

import arrow.core.*
import com.linkedplanet.kotlinhttpclient.error.HttpDomainError

@Suppress("unused")
suspend fun  recursiveRestCall(
    startIndex: Int = 0,
    pageSize: Int = 100,
    call: suspend (Int, Int) -> Either>
): Either> {
    var index = startIndex
    val elements = mutableListOf()
    do {
        val tmpElements: List = call(index, pageSize).getOrElse {
            return@recursiveRestCall it.left()
        }
        elements.addAll(tmpElements)
        index += tmpElements.size
    } while (tmpElements.size >= pageSize)
    return elements.right()
}

@Suppress("unused")
suspend fun  recursiveRestCallPaginated(
    startIndex: Int = 0,
    pageSize: Int = 100,
    call: suspend (Int, Int) -> Either>
): Either> {
    var index = startIndex
    val elements = mutableListOf()
    do {
        val tmpElements: HttpPage = call(index, pageSize).getOrElse {
            return@recursiveRestCallPaginated it.left()
        }
        elements.addAll(tmpElements.getValues())
        index += tmpElements.getValues().size
    } while (tmpElements.getValues().size >= pageSize)
    return elements.right()
}

@Suppress("unused")
suspend fun  recursiveRestCallPaginatedRaw(
    startIndex: Int = 0,
    pageSize: Int = 100,
    call: suspend (Int, Int) -> Either>
): Either>> {
    var index = startIndex
    val elements = mutableListOf>()
    do {
        val tmpElements: HttpPage = call(index, pageSize).getOrElse {
            return@recursiveRestCallPaginatedRaw it.left()
        }
        elements.add(tmpElements)
        index += tmpElements.getValues().size
    } while (tmpElements.getValues().size >= pageSize)
    return elements.right()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy