asHttpChannelClient

fun HttpClient.asHttpChannelClient(baseUrl: String, env: KsrpcEnvironment<String>, errorCodeToHttpStatus: Map<Int, Int> = DEFAULT_KSRPC_ERROR_CODE_TO_HTTP_STATUS): ChannelClient<String>

Turn an HttpClient into a ChannelClient for a specified baseUrl.

HTTP is a request/response transport only: the returned value is a ChannelClient (client-only), not a bidirectional com.monkopedia.ksrpc.channels.Connection. For a bidirectional connection, use the WebSocket or JSON-RPC transports instead.

This is functionally equivalent to baseUrl.toKsrpcUri().connect(env).

Samples

import com.monkopedia.ksrpc.ksrpcEnvironment
import com.monkopedia.ksrpc.ktor.asHttpChannelClient
import com.monkopedia.ksrpc.ktor.serveHttp
import com.monkopedia.ksrpc.toStub
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
import io.ktor.server.routing.routing

fun main() { 
   //sampleStart 
   val env = ksrpcEnvironment { }

val httpClient = HttpClient(OkHttp)

// Turn the HttpClient into a ksrpc ChannelClient for the given URL.
val channelClient = httpClient.asHttpChannelClient(
    "http://localhost:8080/api",
    env
)

// Get the default channel and create a typed stub.
val stub = channelClient.defaultChannel().toStub<GreetingService, String>()
val greeting = stub.greet("world") 
   //sampleEnd
}