{"id":13741375,"url":"https://github.com/google/grpc-kapt","last_synced_at":"2025-05-08T21:33:27.504Z","repository":{"id":65981287,"uuid":"201347816","full_name":"google/grpc-kapt","owner":"google","description":"Annotation driven gRPC clients \u0026 servers in Kotlin","archived":true,"fork":false,"pushed_at":"2019-08-09T05:06:40.000Z","size":151,"stargazers_count":26,"open_issues_count":0,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-19T22:27:23.665Z","etag":null,"topics":["grpc","kapt","kotlin","kotlin-coroutines"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/google.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-08T22:41:07.000Z","updated_at":"2024-10-25T12:24:43.000Z","dependencies_parsed_at":"2023-02-19T19:00:49.097Z","dependency_job_id":null,"html_url":"https://github.com/google/grpc-kapt","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fgrpc-kapt","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fgrpc-kapt/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fgrpc-kapt/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/google%2Fgrpc-kapt/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/google","download_url":"https://codeload.github.com/google/grpc-kapt/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253153163,"owners_count":21862318,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["grpc","kapt","kotlin","kotlin-coroutines"],"created_at":"2024-08-03T04:00:58.413Z","updated_at":"2025-05-08T21:33:27.051Z","avatar_url":"https://github.com/google.png","language":"Kotlin","funding_links":[],"categories":["Language-Specific"],"sub_categories":["Kotlin"],"readme":"# gRPC-kapt\n\n[![CircleCI](https://circleci.com/gh/google/grpc-kapt.svg?style=svg)](https://circleci.com/gh/google/grpc-kapt)\n\nA Kotlin [kapt](https://kotlinlang.org/docs/reference/kapt.html) annotation processor for using\n[gRPC](https://grpc.io/) with the transport of your choice (json, proto, etc.).\n\nSupports client \u0026 server creation with [coroutines](https://kotlinlang.org/docs/reference/coroutines-overview.html).\n\n*Note* This project is a preview. Please try it out and let us know what you think, but there \nare currently no guarantees of any form of stability or support.\n\n## Getting started\n\nTo create a gRPC client simply create an interface annotated with `@GrpcClient`.\n\nTo implement a server, create another class that inherits from your `@GrpcClient`,\nannotate it with `@GrpcServer`, and implement the server-side logic for the methods \nthat you have defined on the client.\n\n```kotlin\nfun main() = runBlocking {\n    // create the server (use .asGrpcService() to create long running servers)\n    SimpleServer().asGrpcServer(8080) {\n        // create a client and query the server\n        SimpleServiceClient.forAddress(\"localhost\", 8080, channelOptions = { usePlaintext() }).use { client -\u003e\n            val answer = client.ask(Question(\"what's this?\"))\n            println(answer)\n        }\n    }\n}\n\n// define the data types\ndata class Question(val query: String)\ndata class Answer(val result: String)\n\n// generate a gRPC client\n@GrpcClient\ninterface SimpleService {\n    suspend fun ask(question: Question): Answer\n}\n\n// generate a gRPC service\n@GrpcServer\nclass SimpleServer : SimpleService {\n    override suspend fun ask(question: Question) = Answer(result = \"you said: '${question.query}'\")\n}\n```\n\nIn most cases, you should also add at least one object in your application with the `@GrpcMarshaller`\nannotation to specify how the data will be marshaled. \n\nHere's another example using JSON and a bidirectional streaming API:\n\n```kotlin\nfun main() = runBlocking {\n    // create the server\n    ComplexServer().asGrpcServer(8080) {\n        // create a client and call the server\n        ComplexServiceClient.forAddress(\"localhost\", 8080, channelOptions = { usePlaintext() }).use { client -\u003e\n            // bidirectional streaming\n            val answers = client.debate(produce {\n                repeat(10) { i -\u003e send(Question(\"Question #${i + 1}\")) }\n            })\n            for (answer in answers) {\n                println(answer)\n            }\n        }\n    }\n}\n\n// generate a gRPC client\n@GrpcClient\ninterface ComplexService {\n    suspend fun debate(questions: ReceiveChannel\u003cQuestion\u003e): ReceiveChannel\u003cAnswer\u003e\n}\n\n// generate \u0026 implement a gRPC service\n@GrpcServer\nclass ComplexServer : ComplexService {\n    override suspend fun debate(questions: ReceiveChannel\u003cQuestion\u003e) = CoroutineScope(coroutineContext).produce {\n        for (question in questions) {\n            send(Answer(\"${question.query}? Sorry, I don't know...\"))\n        }\n    }\n}\n\n// Use JSON serialization for the client \u0026 server\n@GrpcMarshaller\nobject MyMarshallerProvider {\n    private val mapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule())\n\n    fun \u003cT\u003e of(type: Class\u003cT\u003e): MethodDescriptor.Marshaller\u003cT\u003e {\n        return object : MethodDescriptor.Marshaller\u003cT\u003e {\n            override fun stream(value: T): InputStream = ByteArrayInputStream(mapper.writeValueAsBytes(value))\n            override fun parse(stream: InputStream): T = stream.bufferedReader().use { mapper.readValue(it, type) }\n        }\n    }\n}\n```\n\n### More Examples\n\nSee the [complete example here](example-with-json), a [proto example here](example-with-proto), and a\n[Google Cloud API example here](example-with-google-api).\n\nFor the adventurous, see the [gRPC reflection](https://github.com/grpc/grpc-java/blob/master/documentation/server-reflection-tutorial.md) \nexample [here](example-with-proto/src/main/kotlin/com/google/api/example/ExampleReflection.kt).\n\nYou can run the examples by running the following in the root of the project:\n\n```bash\n $ ./gradlew :runExample\n $ ./gradlew :runExampleWithJson\n $ ./gradlew :runExampleWithProto\n $ ./gradlew :runExampleWithGoogle\n $ ./gradlew :runExampleWithProtoReflection\n```\n\n*Note*: `:runExampleWithGoogle` requires a [GCP](https://cloud.google.com) project with the \n[Langauge API](https://cloud.google.com/natural-language/) enabled. Set the `GOOGLE_APPLICATION_CREDENTIALS`\nenvironment variable before running, i.e.:\n\n```bash\nGOOGLE_APPLICATION_CREDENTIALS=\u003cpath_to_service_account.json\u003e ./gradlew :runExampleWithGoogle\n```\n\n## Related Projects\n\nIt's sort of a simpler version of [kgen](https://github.com/googleapis/gapic-generator-kotlin).\n\n## Contributing\n\nContributions to this library are always welcome and highly encouraged.\n\nSee the [CONTRIBUTING](CONTRIBUTING.md) documentation for more information on how to get started.\n\n## Versioning\n\nThis library is currently a *preview* with no guarantees of stability or support. Please get involved and let us know\nif you find it useful and we'll work towards a stable version.\n\n## Disclaimer\n\nThis is not an official Google product.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fgrpc-kapt","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgoogle%2Fgrpc-kapt","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgoogle%2Fgrpc-kapt/lists"}