Skip to content

Commit 95e287f

Browse files
author
Heiko Seeberger
committed
docs/examples: add simple example and update README
1 parent 5395c1e commit 95e287f

File tree

3 files changed

+105
-4
lines changed

3 files changed

+105
-4
lines changed

README.md

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,58 @@
11
# Pub-Sub-Client #
22

3-
Rust library to access Google Cloud Pub/Sub.
3+
Rust library to access Google Cloud Pub/Sub. Currently only pulling from a subscription as well as acknowledging pulled messages is supported.
4+
5+
Messages can either be pulled as raw or, if the payload is JSON data, deserialized into domain messages (structs or enums) via [Serde](https://serde.rs/) and [Serde JSON](https://docs.serde.rs/serde_json). Both raw `ReceivedMessages` and `MessageEnvelopes` holding deserialized messages, expose metadata like message ID, acknowledge ID, attributes, etc.
6+
7+
Aside from straight forward deserialization it is also possible to first transform the pulled JSON values before deserizlizing into domain messages which allows for generally adjusting the JSON structure as well as schema evolution.
8+
9+
## Usage
10+
11+
Typically we want to deserialize into a domain message:
12+
13+
``` rust
14+
#[derive(Debug, Deserialize)]
15+
struct Message {
16+
text: String,
17+
}
18+
```
19+
20+
First create a `PubSubClient`, giving the path to a service account key file and the duration to refresh access tokens before they expire:
21+
22+
``` rust
23+
let pub_sub_client = PubSubClient::new(
24+
"secrets/cryptic-hawk-336616-e228f9680cbc.json",
25+
Duration::from_secs(30),
26+
)?;
27+
```
28+
29+
Things could go wrong, e.g. if the service account key file does not exist or is malformed, hence a `Result` is returned.
30+
31+
Next we call `pull` to get at most the given `42` messages from the given `SUBSCRIPTION`; we do not use a request timeout here for simplicity:
32+
33+
``` rust
34+
let envelopes = pub_sub_client
35+
.pull::<Message>(SUBSCRIPTION, 42, None)
36+
.await?;
37+
```
38+
39+
Of course pulling which happens via HTTP could fail, hence we get back another `Result`.
40+
41+
Finally we handle the pulled messages; for simplicity we only deal with the happy path here, i.e. when the deserialization was successful:
42+
43+
``` rust
44+
for envelope in envelopes {
45+
let envelope = envelope?;
46+
println!("Message text: {}", envelope.message.text);
47+
48+
pub_sub_client
49+
.acknowledge(SUBSCRIPTION, vec![&envelope.ack_id], None)
50+
.await?;
51+
println!("Successfully acknowledged");
52+
}
53+
```
54+
55+
For successfully deserialized messages we call `acknowledge` with the acknowledge ID taken from the envelope.
456

557
## Contribution policy ##
658

examples/simple.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
use pub_sub_client::{Error, PubSubClient};
2+
use serde::Deserialize;
3+
use std::error::Error as _;
4+
use std::time::Duration;
5+
6+
const SUBSCRIPTION: &str = "test";
7+
8+
#[derive(Debug, Deserialize)]
9+
struct Message {
10+
text: String,
11+
}
12+
13+
#[tokio::main]
14+
async fn main() {
15+
if let Err(e) = run().await {
16+
eprintln!("ERRORx: {}", e);
17+
if let Some(e) = e.source() {
18+
eprintln!("SOURCE: {}", e);
19+
}
20+
}
21+
}
22+
23+
async fn run() -> Result<(), Error> {
24+
let pub_sub_client = PubSubClient::new(
25+
"secrets/cryptic-hawk-336616-e228f9680cbc.json",
26+
Duration::from_secs(30),
27+
)?;
28+
29+
let envelopes = pub_sub_client
30+
.pull::<Message>(SUBSCRIPTION, 42, None)
31+
.await?;
32+
33+
for envelope in envelopes {
34+
let envelope = envelope?;
35+
println!("Message text: {}", envelope.message.text);
36+
37+
pub_sub_client
38+
.acknowledge(SUBSCRIPTION, vec![&envelope.ack_id], None)
39+
.await?;
40+
println!("Successfully acknowledged");
41+
}
42+
43+
Ok(())
44+
}

examples/transform_versioned.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::anyhow;
2-
use pub_sub_client::{Error, MessageEnvelope, PubSubClient, ReceivedMessage};
2+
use pub_sub_client::{Error, PubSubClient, ReceivedMessage};
33
use serde::Deserialize;
44
use serde_json::{json, Value};
55
use std::error::Error as _;
@@ -30,8 +30,13 @@ async fn run() -> Result<(), Error> {
3030
Duration::from_secs(30),
3131
)?;
3232

33-
let msg_envelopes: Vec<Result<MessageEnvelope<Message>, Error>> = pub_sub_client
34-
.pull_with_transform(SUBSCRIPTION, 42, Some(Duration::from_secs(45)), transform)
33+
let msg_envelopes = pub_sub_client
34+
.pull_with_transform::<Message, _>(
35+
SUBSCRIPTION,
36+
42,
37+
Some(Duration::from_secs(45)),
38+
transform,
39+
)
3540
.await?;
3641

3742
for msg_envelope in msg_envelopes {

0 commit comments

Comments
 (0)