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

com.linkedplanet.kotlinhttpclient.error.Utils.kt Maven / Gradle / Ivy

There is a newer version: 0.5.2
Show newest version
/**
 * Copyright 2022 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.
 */
package com.linkedplanet.kotlinhttpclient.error

import arrow.core.*
import com.google.gson.Gson
import com.google.gson.GsonBuilder

val GSON: Gson = GsonBuilder().create()

suspend fun  recursiveRestCall(
    start: Int = 0,
    max: Int? = null,
    call: suspend (Int, Int) -> Either>
): Either> {
    var index = start
    val maxResults = 1
    val elements = mutableListOf()
    var nextPage = false
    do {
        val tmpElements: List = call(index, maxResults).getOrHandle {
            return@recursiveRestCall it.left()
        }
        elements.addAll(tmpElements)
        nextPage = tmpElements.size >= maxResults
        index += tmpElements.size
    } while (nextPage && (max == null || index <= max))
    return elements.right()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy