Skip to main content

google_cloud_pubsub/publisher/
base_publisher.rs

1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::publisher::builder::PublisherPartialBuilder;
16
17/// Creates [`Publisher`](crate::client::Publisher) instances.
18///
19/// A single `BasePublisher` can be used to create multiple `Publisher` clients
20/// for different topics. It manages the underlying gRPC connection and
21/// authentication.
22///
23/// # Example
24///
25/// ```
26/// # async fn sample() -> anyhow::Result<()> {
27/// # use google_cloud_pubsub::client::BasePublisher;
28/// # use google_cloud_pubsub::model::Message;
29///
30/// // Create a client.
31/// let client: BasePublisher = BasePublisher::builder().build().await?;
32///
33/// // Create a publisher for a specific topic.
34/// let publisher = client.publisher("projects/my-project/topics/my-topic").build();
35///
36/// // Publish a message.
37/// let handle = publisher.publish(Message::new().set_data("hello world"));
38/// let message_id = handle.await?;
39/// println!("Message sent with ID: {}", message_id);
40/// # Ok(())
41/// # }
42/// ```
43#[derive(Clone, Debug)]
44pub struct BasePublisher {
45    pub(crate) inner: crate::generated::gapic_dataplane::client::Publisher,
46}
47
48pub use super::client_builder::BasePublisherBuilder;
49
50impl BasePublisher {
51    /// Returns a builder for [BasePublisher].
52    ///
53    /// ```
54    /// # async fn sample() -> anyhow::Result<()> {
55    /// # use google_cloud_pubsub::client::BasePublisher;
56    /// let client: BasePublisher = BasePublisher::builder().build().await?;
57    /// # Ok(()) }
58    /// ```
59    pub fn builder() -> BasePublisherBuilder {
60        BasePublisherBuilder::new()
61    }
62
63    /// Creates a new Pub/Sub publisher client with the given configuration.
64    pub(crate) async fn new(builder: BasePublisherBuilder) -> crate::ClientBuilderResult<Self> {
65        let inner =
66            crate::generated::gapic_dataplane::client::Publisher::new(builder.config).await?;
67        std::result::Result::Ok(Self { inner })
68    }
69
70    /// Creates a new `Publisher` for a given topic.
71    ///
72    /// ```
73    /// # async fn sample() -> anyhow::Result<()> {
74    /// # use google_cloud_pubsub::*;
75    /// # use builder::publisher::BasePublisherBuilder;
76    /// # use client::BasePublisher;
77    /// # use model::Message;
78    /// let client = BasePublisher::builder().build().await?;
79    /// let publisher = client.publisher("projects/my-project/topics/my-topic").build();
80    /// let message_id = publisher.publish(Message::new().set_data("Hello, World")).await?;
81    /// # Ok(()) }
82    /// ```
83    pub fn publisher<T>(&self, topic: T) -> PublisherPartialBuilder
84    where
85        T: Into<String>,
86    {
87        PublisherPartialBuilder::new(self.inner.clone(), topic.into())
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    use super::BasePublisher;
94    use google_cloud_auth::credentials::anonymous::Builder as Anonymous;
95
96    #[tokio::test]
97    async fn builder() -> anyhow::Result<()> {
98        let client = BasePublisher::builder()
99            .with_credentials(Anonymous::new().build())
100            .build()
101            .await?;
102        let _ = client.publisher("projects/my-project/topics/my-topic".to_string());
103        Ok(())
104    }
105}