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

commonMain.com.funkatronics.networking.Rpc20Driver.kt Maven / Gradle / Ivy

There is a newer version: 0.2.8
Show newest version
package com.funkatronics.networking

import com.funkatronics.rpccore.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json

class Rpc20Driver(private val url: String,
                  private val httpDriver: HttpNetworkDriver) : JsonRpcDriver {

    private val json = Json {
        ignoreUnknownKeys = true
    }

    override suspend fun  makeRequest(request: RpcRequest, resultSerializer: KSerializer): Rpc20Response =
        httpDriver.makeHttpRequest(
            HttpPostRequest(
                url = url,
                properties = mapOf("Content-Type" to "application/json; charset=utf-8"),
                body = json.encodeToString(RpcRequestPolymorphicSerializer, request)
            )
        ).run {
            try {
                json.decodeFromString(Rpc20Response.serializer(resultSerializer), this)
            } catch (e: Exception) {
                Rpc20Response(error = RpcError(-1, e.message ?: this))
            }
        }

    internal class HttpPostRequest(
        override val url: String,
        override val properties: Map,
        override val body: String? = null
    ) : HttpRequest {
        override val method = "POST"
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy