Hi, Playing with arrow and trying to understand Pa...
# arrow
r
Hi, Playing with arrow and trying to understand Paco’s blog post about coroutines… can’t figure out what’s wrong with this simple usecase based on ktor’s HttpClient(CIO)
Copy code
import arrow.core.Either
import <http://arrow.fx.IO|arrow.fx.IO>
import arrow.fx.extensions.fx
import io.ktor.client.HttpClient
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import io.ktor.client.request.get
import kotlinx.serialization.Serializable

fun main() {
    IO.fx {
        val todo = effect { getToDo(1) }
            .bind()
        !effect { println("My todo: $todo") }
    }.unsafeRunSync()
}

suspend fun getToDo(id: Int): Either<ServiceError, ToDo> {
    val client = HttpClient {
        install(JsonFeature) {
            serializer = KotlinxSerializer()
        }
    }
    val baseUrl = "<https://jsonplaceholder.typicode.com>"
    val url = "$baseUrl/todos/$id"
    val resp = Either.catch {
        client.get<ToDo>(url)
    }.mapLeft {
        println(it.printStackTrace())
        ServiceError(it.message)
    }
    return resp
}

class ServiceError(msg: String?) {
    init {
        println("Error: $msg")
    }
}

@Serializable
data class ToDo(val userId: Int, val id: Int, val title: String, val completed: Boolean)