/connect-kotlin

Idiomatic gRPC & Connect RPCs for Kotlin.

Primary LanguageKotlinApache License 2.0Apache-2.0

Build License

Connect-Kotlin

Connect-Kotlin is a slim library for building browser and gRPC-compatible HTTP APIs. You write a short Protocol Buffer schema and implement your application logic, and Connect generates code to handle marshaling, routing, compression, and content type negotiation. It also generates an idiomatic, type-safe client. Handlers and clients support three protocols: gRPC, gRPC-Web, and Connect's own protocol.

Given a simple Protobuf schema, Connect-Kotlin generates idiomatic Kotlin protocol interfaces and client implementations:

Click to expand ElizaServiceClient.kt
public class ElizaServiceClient(
    private val client: ProtocolClientInterface
) : ElizaServiceClientInterface {
    public override suspend fun say(request: SayRequest, headers: Headers):
        ResponseMessage<SayResponse> = client.unary(
        request,
        headers,
        MethodSpec(
            "buf.connect.demo.eliza.v1.ElizaService/Say",
            buf.connect.demo.eliza.v1.SayRequest::class,
            buf.connect.demo.eliza.v1.SayResponse::class
        )
    )

    public override suspend fun converse(headers: Headers):
        BidirectionalStreamInterface<ConverseRequest, ConverseResponse> = client.stream(
        headers,
        MethodSpec(
            "buf.connect.demo.eliza.v1.ElizaService/Converse",
            buf.connect.demo.eliza.v1.ConverseRequest::class,
            buf.connect.demo.eliza.v1.ConverseResponse::class
        )
    )

    public override suspend fun introduce(headers: Headers):
        ServerOnlyStreamInterface<IntroduceRequest, IntroduceResponse> = client.serverStream(
        headers,
        MethodSpec(
            "buf.connect.demo.eliza.v1.ElizaService/Introduce",
            buf.connect.demo.eliza.v1.IntroduceRequest::class,
            buf.connect.demo.eliza.v1.IntroduceResponse::class
        )
    )
}
Click to expand ElizaServiceClientInterface.kt
public interface ElizaServiceClientInterface {
    public suspend fun say(request: SayRequest, headers: Headers = emptyMap()):
        ResponseMessage<SayResponse>

    public suspend fun converse(headers: Headers = emptyMap()):
        BidirectionalStreamInterface<ConverseRequest, ConverseResponse>

    public suspend fun introduce(headers: Headers = emptyMap()):
        ServerOnlyStreamInterface<IntroduceRequest, IntroduceResponse>
}

This code can then be integrated with just a few lines:

class MainActivity : AppCompatActivity() {
    private lateinit var elizaServiceClient: ElizaServiceClientInterface

    private suspend fun say(sentence: String) {
        // Make a unary request to Eliza.
        val response = elizaServiceClient.say(SayRequest.newBuilder().setSentence(sentence).build())
        val elizaSentence = response.success { success ->
            // Get Eliza's reply from the response.
            success.message.sentence
        }
        // Use the elizaSentence in your views.
    }
}

That’s it! You no longer need to manually define request/response models, specify the exact path of your request, nor worry about the underlying networking transport for your applications!

Quick Start

Head over to our quick start tutorial to get started. It only takes ~10 minutes to complete a working chat app that uses Connect-Kotlin!

Documentation

Comprehensive documentation for everything, including interceptors, streaming, and error handling is available on the connect.build website.

Generation Options

Option Type Default Details
generateCallbackMethods Boolean false Generate callback signatures for unary methods.
generateCoroutineMethods Boolean true Generate suspend signatures for unary methods.
generateBlockingUnaryMethods Boolean false Generate blocking signatures for unary methods.

Example Apps

Example apps are available in /examples. First, run make generate to generate code for the Protobuf plugins.

For the Android example, you can run make installandroid to build and install a fully functional Android application using Connect-Kotlin.

Additionally, there are pure Kotlin examples that demonstrate a simple main executable using Connect-Kotlin:

The examples demonstrates:

Contributing

We'd love your help making Connect better!

Extensive instructions for building the library and generator plugins locally, running tests, and contributing to the repository are available in our CONTRIBUTING.md guide. Please check it out for details.

Ecosystem

Status

This project is in beta, and we may make a few changes as we gather feedback from early adopters. Join us on Slack!

Legal

Offered under the Apache 2 license.