#
google_cloud_pubsub/subscriber/
client_builder.rs

    1// Copyright 2026 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 super::client::Subscriber;
    16use gax::client_builder::Result as BuilderResult;
    17use gaxi::options::ClientConfig;
    18
    19/// A builder for [Subscriber].
    20///
    21/// # Example
    22/// ```
    23/// # use google_cloud_pubsub::client::Subscriber;
    24/// # async fn sample() -> anyhow::Result<()> {
    25/// let builder = Subscriber::builder();
    26/// let client = builder
    27///     .with_endpoint("https://pubsub.googleapis.com")
    28///     .build()
    29///     .await?;
    30/// # Ok(()) }
    31/// ```
    32pub struct ClientBuilder {
    33    pub(super) config: ClientConfig,
    34}
    35
    36impl ClientBuilder {
    37    pub(super) fn new() -> Self {
    38        Self {
    39            config: ClientConfig::default(),
    40        }
    41    }
    42
    43    /// Creates a new client.
    44    ///
    45    /// # Example
    46    /// ```
    47    /// # use google_cloud_pubsub::client::Subscriber;
    48    /// # async fn sample() -> anyhow::Result<()> {
    49    /// let client = Subscriber::builder().build().await?;
    50    /// # Ok(()) }
    51    /// ```
    52    pub async fn build(self) -> BuilderResult<Subscriber> {
    53        Subscriber::new(self).await
    54    }
    55
    56    /// Sets the endpoint.
    57    ///
    58    /// # Example
    59    /// ```
    60    /// # use google_cloud_pubsub::client::Subscriber;
    61    /// # async fn sample() -> anyhow::Result<()> {
    62    /// let client = Subscriber::builder()
    63    ///     .with_endpoint("https://private.googleapis.com")
    64    ///     .build()
    65    ///     .await?;
    66    /// # Ok(()) }
    67    /// ```
    68    pub fn with_endpoint<V: Into<String>>(mut self, v: V) -> Self {
    69        self.config.endpoint = Some(v.into());
    70        self
    71    }
    72
    73    /// Configures the authentication credentials.
    74    ///
    75    /// More information about valid credentials types can be found in the
    76    /// [google-cloud-auth] crate documentation.
    77    ///
    78    /// # Example
    79    /// ```
    80    /// # use google_cloud_pubsub::client::Subscriber;
    81    /// # async fn sample() -> anyhow::Result<()> {
    82    /// use auth::credentials::mds;
    83    /// let client = Subscriber::builder()
    84    ///     .with_credentials(
    85    ///         mds::Builder::default()
    86    ///             .with_scopes(["https://www.googleapis.com/auth/cloud-platform.read-only"])
    87    ///             .build()?)
    88    ///     .build()
    89    ///     .await?;
    90    /// # Ok(()) }
    91    /// ```
    92    ///
    93    /// [google-cloud-auth]: https://docs.rs/google-cloud-auth
    94    pub fn with_credentials<V: Into<auth::credentials::Credentials>>(mut self, v: V) -> Self {
    95        self.config.cred = Some(v.into());
    96        self
    97    }
    98}
    99
    100#[cfg(test)]
    101mod tests {
    102    use super::*;
    103    use auth::credentials::anonymous::Builder as Anonymous;
    104
    105    #[test]
    106    fn defaults() {
    107        let builder = ClientBuilder::new();
    108        assert!(builder.config.endpoint.is_none());
    109        assert!(builder.config.cred.is_none());
    110    }
    111
    112    #[test]
    113    fn setters() {
    114        let builder = ClientBuilder::new()
    115            .with_endpoint("test-endpoint.com")
    116            .with_credentials(Anonymous::new().build());
    117        assert_eq!(
    118            builder.config.endpoint,
    119            Some("test-endpoint.com".to_string())
    120        );
    121        assert!(builder.config.cred.is_some());
    122    }
    123}