commonMain.com.funkatronics.networking.Rpc20Driver.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rpc-core-jvm Show documentation
Show all versions of rpc-core-jvm Show documentation
Multiplatform JSON RPC Library using Kotlin Serialization
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