All Projects → google → grpc-kapt

google / grpc-kapt

Licence: Apache-2.0 license
Annotation driven gRPC clients & servers in Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to grpc-kapt

McTester
An integration testing framework for Minecraft
Stars: ✭ 39 (+56%)
Mutual labels:  kotlin-coroutines
CoroutineLite
Simple implementation of kotlinx.coroutines to clarify the design of Kotlin Coroutines.
Stars: ✭ 142 (+468%)
Mutual labels:  kotlin-coroutines
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (+40%)
Mutual labels:  kotlin-coroutines
mobius.kt
Kotlin Multiplatform framework for managing state evolution and side-effects
Stars: ✭ 39 (+56%)
Mutual labels:  kotlin-coroutines
copydynamic
Prototype of generating `copyDynamic` extension functions for kotlin data classes
Stars: ✭ 57 (+128%)
Mutual labels:  kapt
StarWarsSearch-MVI
Star wars sample android project showcasing the use of View components for rendering UI in Fragments and Activities. Uses Android Jetpack, clean architecture with MVI (Uni-directional data flow), dagger hilt, and kotlin coroutines with StateFlow
Stars: ✭ 189 (+656%)
Mutual labels:  kotlin-coroutines
nytclient-android
This sample app is created to demonstrate the usage of Android Architecture Components with MVVM architecture
Stars: ✭ 24 (-4%)
Mutual labels:  kotlin-coroutines
jvmbuilder
A source code generator for Kotlin data classes to automatically create a Builder class.
Stars: ✭ 16 (-36%)
Mutual labels:  kapt
Simple-MVVM
A simple Android MVVM pattern example
Stars: ✭ 34 (+36%)
Mutual labels:  kotlin-coroutines
android-kotlin-coroutines
A simple Android project using asynchronous programming with Kotlin Coroutines.
Stars: ✭ 45 (+80%)
Mutual labels:  kotlin-coroutines
android-compose-mvvm-foodies
Android sample app following best practices: Kotlin, Compose, Coroutines and Flow, Hilt, JetPack Navigation, ViewModel, MVVM, Retrofit, Coil
Stars: ✭ 374 (+1396%)
Mutual labels:  kotlin-coroutines
Age-Gender Estimation TF-Android
Age + Gender Estimation on Android with TensorFlow Lite
Stars: ✭ 34 (+36%)
Mutual labels:  kotlin-coroutines
Android-MVVM-News-App
MVVM News Application with clean code architecture & android jetpack components.
Stars: ✭ 38 (+52%)
Mutual labels:  kotlin-coroutines
Kotlin-Annotation-Processor
Annotation Processor Sample in Kotlin
Stars: ✭ 19 (-24%)
Mutual labels:  kapt
kotlin-coroutines-android
Useful extensions for coroutines. AutoDispose + MainScope
Stars: ✭ 84 (+236%)
Mutual labels:  kotlin-coroutines
Delish
Delish, a Food Recipes App in Jetpack Compose and Hilt based on modern Android tech-stacks and MVI clean architecture.
Stars: ✭ 356 (+1324%)
Mutual labels:  kotlin-coroutines
KMP-NativeCoroutines
Library to use Kotlin Coroutines from Swift code in KMP apps
Stars: ✭ 502 (+1908%)
Mutual labels:  kotlin-coroutines
tv-maniac
Tv-Maniac is a Multiplatform app (Android & iOS) for viewing TV Shows from TMDB.
Stars: ✭ 55 (+120%)
Mutual labels:  kotlin-coroutines
permissions-flow
A simple library to make it easy requesting permissions in Android using Kotlin Coroutines.
Stars: ✭ 81 (+224%)
Mutual labels:  kotlin-coroutines
Lifecycle-Coroutines-Extension
AAC 的 Lifecycle 结合 Kotlin Coroutines 进行使用
Stars: ✭ 47 (+88%)
Mutual labels:  kotlin-coroutines

gRPC-kapt

CircleCI

A Kotlin kapt annotation processor for using gRPC with the transport of your choice (json, proto, etc.).

Supports client & server creation with coroutines.

Note This project is a preview. Please try it out and let us know what you think, but there are currently no guarantees of any form of stability or support.

Getting started

To create a gRPC client simply create an interface annotated with @GrpcClient.

To implement a server, create another class that inherits from your @GrpcClient, annotate it with @GrpcServer, and implement the server-side logic for the methods that you have defined on the client.

fun main() = runBlocking {
    // create the server (use .asGrpcService() to create long running servers)
    SimpleServer().asGrpcServer(8080) {
        // create a client and query the server
        SimpleServiceClient.forAddress("localhost", 8080, channelOptions = { usePlaintext() }).use { client ->
            val answer = client.ask(Question("what's this?"))
            println(answer)
        }
    }
}

// define the data types
data class Question(val query: String)
data class Answer(val result: String)

// generate a gRPC client
@GrpcClient
interface SimpleService {
    suspend fun ask(question: Question): Answer
}

// generate a gRPC service
@GrpcServer
class SimpleServer : SimpleService {
    override suspend fun ask(question: Question) = Answer(result = "you said: '${question.query}'")
}

In most cases, you should also add at least one object in your application with the @GrpcMarshaller annotation to specify how the data will be marshaled.

Here's another example using JSON and a bidirectional streaming API:

fun main() = runBlocking {
    // create the server
    ComplexServer().asGrpcServer(8080) {
        // create a client and call the server
        ComplexServiceClient.forAddress("localhost", 8080, channelOptions = { usePlaintext() }).use { client ->
            // bidirectional streaming
            val answers = client.debate(produce {
                repeat(10) { i -> send(Question("Question #${i + 1}")) }
            })
            for (answer in answers) {
                println(answer)
            }
        }
    }
}

// generate a gRPC client
@GrpcClient
interface ComplexService {
    suspend fun debate(questions: ReceiveChannel<Question>): ReceiveChannel<Answer>
}

// generate & implement a gRPC service
@GrpcServer
class ComplexServer : ComplexService {
    override suspend fun debate(questions: ReceiveChannel<Question>) = CoroutineScope(coroutineContext).produce {
        for (question in questions) {
            send(Answer("${question.query}? Sorry, I don't know..."))
        }
    }
}

// Use JSON serialization for the client & server
@GrpcMarshaller
object MyMarshallerProvider {
    private val mapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule())

    fun <T> of(type: Class<T>): MethodDescriptor.Marshaller<T> {
        return object : MethodDescriptor.Marshaller<T> {
            override fun stream(value: T): InputStream = ByteArrayInputStream(mapper.writeValueAsBytes(value))
            override fun parse(stream: InputStream): T = stream.bufferedReader().use { mapper.readValue(it, type) }
        }
    }
}

More Examples

See the complete example here, a proto example here, and a Google Cloud API example here.

For the adventurous, see the gRPC reflection example here.

You can run the examples by running the following in the root of the project:

 $ ./gradlew :runExample
 $ ./gradlew :runExampleWithJson
 $ ./gradlew :runExampleWithProto
 $ ./gradlew :runExampleWithGoogle
 $ ./gradlew :runExampleWithProtoReflection

Note: :runExampleWithGoogle requires a GCP project with the Langauge API enabled. Set the GOOGLE_APPLICATION_CREDENTIALS environment variable before running, i.e.:

GOOGLE_APPLICATION_CREDENTIALS=<path_to_service_account.json> ./gradlew :runExampleWithGoogle

Related Projects

It's sort of a simpler version of kgen.

Contributing

Contributions to this library are always welcome and highly encouraged.

See the CONTRIBUTING documentation for more information on how to get started.

Versioning

This library is currently a preview with no guarantees of stability or support. Please get involved and let us know if you find it useful and we'll work towards a stable version.

Disclaimer

This is not an official Google product.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].