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

jvmMain.ru.astrainteractive.klibs.sample.feature.service.RickMortyService.kt Maven / Gradle / Ivy

There is a newer version: 2.3.0
Show newest version
package ru.astrainteractive.klibs.sample.feature.service

import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.parameter
import io.ktor.client.statement.bodyAsText
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import ru.astrainteractive.klibs.sample.feature.service.model.CharacterModel
import ru.astrainteractive.klibs.sample.feature.service.model.Filter

internal interface RickMortyService {
    suspend fun fetchCharacters(page: Int, pageSize: Int, filter: Filter): List
}

internal class RickMortyServiceImpl(
    private val httpClient: HttpClient,
    private val json: Json
) : RickMortyService {
    override suspend fun fetchCharacters(page: Int, pageSize: Int, filter: Filter): List {
        val stringBody = httpClient.get("https://rickandmortyapi.com/api/character/") {
            parameter("name", filter.name)
            parameter("status", filter.status?.string)
            parameter("page", page)
            parameter("count", pageSize)
        }.bodyAsText()
        val resultsString = json.parseToJsonElement(stringBody).jsonObject["results"]?.toString()
        return if (resultsString.isNullOrBlank()) {
            emptyList()
        } else {
            json.decodeFromString(resultsString)
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy