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

commonMain.com.funkatronics.rpccore.JsonRpcDriver.kt Maven / Gradle / Ivy

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

import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.JsonElement

interface JsonRpcDriver {
    suspend fun  makeRequest(request: RpcRequest, resultSerializer: KSerializer): RpcResponse
}

suspend inline fun  JsonRpcDriver.get(
    request: RpcRequest,
    serializer: KSerializer
): Result =
    this.makeRequest(request, serializer).let { response ->
        (response.result)?.let { result ->
            return Result.success(result)
        }

        response.error?.let {
            return Result.failure(Error(it.message))
        }

        // an empty error and empty result means we did not find anything, return null
        return Result.success(null)
    }

suspend fun JsonRpcDriver.makeRequest(request: RpcRequest): DefaultRpcResponse =
    makeRequest(request, JsonElement.serializer())




© 2015 - 2025 Weber Informatics LLC | Privacy Policy