Skip to content

Commit

Permalink
pubsub: add publish_messages methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestas-poskus committed Dec 27, 2021
1 parent e356904 commit 94453d7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 3 additions & 1 deletion google-cloud/src/pubsub/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ mod client;
mod message;
mod subscription;
mod topic;
mod api {

/// API proto structures
pub mod api {
include!("api/google.pubsub.v1.rs");
}

Expand Down
27 changes: 19 additions & 8 deletions google-cloud/src/pubsub/topic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,34 @@ impl Topic {
}

/// Publish a message onto this topic.
pub async fn publish(&mut self, data: impl Into<Vec<u8>>) -> Result<(), Error> {
pub async fn publish_message(&mut self, msg: api::PubsubMessage) -> Result<(), Error> {
self.publish_messages(vec![msg]).await
}

/// Publish a message onto this topic.
pub async fn publish_messages(
&mut self,
messages: Vec<api::PubsubMessage>,
) -> Result<(), Error> {
let request = api::PublishRequest {
topic: self.name.clone(),
messages: vec![api::PubsubMessage {
data: data.into(),
attributes: HashMap::new(),
message_id: String::new(),
ordering_key: String::new(),
publish_time: None,
}],
messages,
};
let request = self.client.construct_request(request).await?;
self.client.publisher.publish(request).await?;

Ok(())
}

/// Publish a message onto this topic.
pub async fn publish(&mut self, data: impl Into<Vec<u8>>) -> Result<(), Error> {
self.publish_message(api::PubsubMessage {
data: data.into(),
..Default::default()
})
.await
}

/// Delete the topic.
pub async fn delete(mut self) -> Result<(), Error> {
let request = api::DeleteTopicRequest {
Expand Down

0 comments on commit 94453d7

Please sign in to comment.