generated from arrow-kt/Arrow-JVM-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json modules, and finish documentation
- Loading branch information
Showing
36 changed files
with
1,687 additions
and
663 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,69 @@ | ||
# Kotlin GCP PubSub | ||
|
||
A KotlinX (Flow) binding for Google Cloud PubSub. | ||
Google Cloud PubSub made easy! Kotlin GCP PubSub offers idiomatic KotlinX & Ktor integration for GCP. | ||
|
||
```kotlin | ||
@Serializable | ||
data class Event(val key: String, val message: String) | ||
|
||
fun Application.pubSubApp() { | ||
pubSub(ProjectId("my-project")) { | ||
subscribe<Event>(SubscriptionId("my-subscription")) { (event, record) -> | ||
println("event.key: ${event.key}, event.message: ${event.message}") | ||
record.ack() | ||
} | ||
} | ||
|
||
routing { | ||
post("/publish/{key}/{message}") { | ||
val event = Event(call.parameters["key"]!!, call.parameters["message"]!!) | ||
|
||
pubSub() | ||
.publisher(ProjectId("my-project")) | ||
.publish(TopicId("my-topic"), event) | ||
|
||
call.respond(HttpStatusCode.Accepted) | ||
} | ||
} | ||
} | ||
``` | ||
|
||
## Modules | ||
|
||
- [PubSub Ktor plugin](pubsub-ktor/README.MD) to conveniently consume messages from GCP PubSub, and publish messages to | ||
GCP PubSub | ||
- [PubSub Ktor KotlinX Serialization Json](pubsub-ktor-kotlinx-serialization-json/README.MD) to conveniently consume | ||
messages from GCP PubSub, and publish messages to GCP PubSub using KotlinX Serialization Json | ||
- [PubSub test](pubsub-test/README.MD) one-line testing support powered by testcontainers | ||
- [GCP PubSub](pubsub/README.MD): KotlinX integration for `TopicAdminClient`, `SubscriptionAdminClient`, `Susbcriber` | ||
and `Publisher`. | ||
- [PubSub Ktor KotlinX Serialization Json](pubsub-kotlinx-serialization-json/README.MD) to conveniently consume messages | ||
from GCP PubSub, and publish messages to GCP PubSub | ||
- [Google Common API](api-core/README.MD): KotlinX integration for `ApiFuture` | ||
|
||
## Using in your projects | ||
|
||
### Gradle | ||
|
||
Add dependencies (you can also add other modules that you need): | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("io.github.nomisrev:gcp-pubsub-ktor:1.0.0") | ||
implementation("io.github.nomisrev:gcp-pubsub-ktor-kotlinx-serialization-json:1.0.0") | ||
testImplementation("io.github.nomisrev:gcp-pubsub-test:1.0.0") | ||
} | ||
``` | ||
|
||
### Maven | ||
|
||
Add dependencies (you can also add other modules that you need): | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>io.github.nomisrev</groupId> | ||
<artifactId>gcp-pubsub-ktor</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,7 @@ plugins { | |
alias(libs.plugins.knit) | ||
alias(libs.plugins.spotless) | ||
} | ||
|
||
repositories { | ||
mavenCentral() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Module common-api | ||
|
||
KotlinX integration module for Google's api-core module. If you need to work with | ||
Google's [`ApiFuture`](https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFutures). | ||
|
||
Extension functions: | ||
|
||
| **Name** | **Description** | ||
|--------------------------|--------------------------------------------------- | ||
| [ApiFuture.await]() | Awaits for completion of the future (cancellable) | ||
| [ApiFuture.asDeferred]() | Converts a deferred value to the future | ||
|
||
## Example | ||
|
||
Given the following functions defined in some Java API based on Guava: | ||
|
||
```java | ||
public ApiFuture<AckResponse> ack(); // starts async acknowledging of message | ||
``` | ||
|
||
We can consume this API from Kotlin coroutine to load two images and combine then asynchronously. | ||
The resulting function returns `ListenableFuture<Image>` for ease of use back from Guava-based Java code. | ||
|
||
```kotlin | ||
suspend fun processMessage(record: PubsubRecord): Unit { | ||
println(record.message.data.toStringUtf8()) | ||
when (val response = record.ack().await()) { | ||
SUCCESSFUL -> println("Message was successfully acknowledged. Will not be redelivered.") | ||
else -> println("Acknowledgment failed, message might be redelivered.") | ||
} | ||
} | ||
``` | ||
|
||
Note that this module should be used only for integration with existing Java APIs based on `ApiFuture`. | ||
Writing pure-Kotlin code that uses `ApiFuture` is highly not recommended, since the resulting APIs based | ||
on the futures are quite error-prone. See the discussion on | ||
[Asynchronous Programming Styles](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#asynchronous-programming-styles) | ||
for details on general problems pertaining to any future-based API and keep in mind that `ApiFuture` exposes | ||
a _blocking_ method [get](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#get--) that makes | ||
it especially bad choice for coroutine-based Kotlin code. | ||
|
||
## Using in your projects | ||
|
||
### Gradle | ||
|
||
Add dependencies (you can also add other modules that you need): | ||
|
||
```kotlin | ||
dependencies { | ||
implementation("io.github.nomisrev:google-common-api:1.0.0") | ||
} | ||
``` | ||
|
||
### Maven | ||
|
||
Add dependencies (you can also add other modules that you need): | ||
|
||
```xml | ||
|
||
<dependency> | ||
<groupId>io.github.nomisrev</groupId> | ||
<artifactId>google-common-api</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 0 additions & 23 deletions
23
gcp-pubsub-ktor/src/main/kotlin/io/github/nomisrev/gcp/pubsub/ktor/GcpPubSubSyntax.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.