google_cloud_language_v2/
client.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//
15// Code generated by sidekick. DO NOT EDIT.
16#![allow(rustdoc::redundant_explicit_links)]
17#![allow(rustdoc::broken_intra_doc_links)]
18
19use crate::Result;
20
21/// Implements a client for the Cloud Natural Language API.
22///
23/// # Example
24/// ```
25/// # tokio_test::block_on(async {
26/// # use google_cloud_language_v2::client::LanguageService;
27/// let client = LanguageService::builder().build().await?;
28/// // use `client` to make requests to the Cloud Natural Language API.
29/// # gax::Result::<()>::Ok(()) });
30/// ```
31///
32/// # Service Description
33///
34/// Provides text analysis operations such as sentiment analysis and entity
35/// recognition.
36///
37/// # Configuration
38///
39/// To configure `LanguageService` use the `with_*` methods in the type returned
40/// by [builder()][LanguageService::builder]. The default configuration should
41/// work for most applications. Common configuration changes include
42///
43/// * [with_endpoint()]: by default this client uses the global default endpoint
44///   (`https://language.googleapis.com`). Applications using regional
45///   endpoints or running in restricted networks (e.g. a network configured
46//    with [Private Google Access with VPC Service Controls]) may want to
47///   override this default.
48/// * [with_credentials()]: by default this client uses
49///   [Application Default Credentials]. Applications using custom
50///   authentication may need to override this default.
51///
52/// [with_endpoint()]: super::builder::language_service::ClientBuilder::with_endpoint
53/// [with_credentials()]: super::builder::language_service::ClientBuilder::credentials
54/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
55/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
56///
57/// # Pooling and Cloning
58///
59/// `LanguageService` holds a connection pool internally, it is advised to
60/// create one and the reuse it.  You do not need to wrap `LanguageService` in
61/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
62/// already uses an `Arc` internally.
63#[derive(Clone, Debug)]
64pub struct LanguageService {
65    inner: std::sync::Arc<dyn super::stub::dynamic::LanguageService>,
66}
67
68impl LanguageService {
69    /// Returns a builder for [LanguageService].
70    ///
71    /// ```
72    /// # tokio_test::block_on(async {
73    /// # use google_cloud_language_v2::client::LanguageService;
74    /// let client = LanguageService::builder().build().await?;
75    /// # gax::Result::<()>::Ok(()) });
76    /// ```
77    pub fn builder() -> super::builder::language_service::ClientBuilder {
78        gax::client_builder::internal::new_builder(
79            super::builder::language_service::client::Factory,
80        )
81    }
82
83    /// Creates a new client from the provided stub.
84    ///
85    /// The most common case for calling this function is in tests mocking the
86    /// client's behavior.
87    pub fn from_stub<T>(stub: T) -> Self
88    where
89        T: super::stub::LanguageService + 'static,
90    {
91        Self {
92            inner: std::sync::Arc::new(stub),
93        }
94    }
95
96    pub(crate) async fn new(config: gaxi::options::ClientConfig) -> Result<Self> {
97        let inner = Self::build_inner(config).await?;
98        Ok(Self { inner })
99    }
100
101    async fn build_inner(
102        conf: gaxi::options::ClientConfig,
103    ) -> Result<std::sync::Arc<dyn super::stub::dynamic::LanguageService>> {
104        if gaxi::options::tracing_enabled(&conf) {
105            return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
106        }
107        Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
108    }
109
110    async fn build_transport(
111        conf: gaxi::options::ClientConfig,
112    ) -> Result<impl super::stub::LanguageService> {
113        super::transport::LanguageService::new(conf).await
114    }
115
116    async fn build_with_tracing(
117        conf: gaxi::options::ClientConfig,
118    ) -> Result<impl super::stub::LanguageService> {
119        Self::build_transport(conf)
120            .await
121            .map(super::tracing::LanguageService::new)
122    }
123
124    /// Analyzes the sentiment of the provided text.
125    pub fn analyze_sentiment(&self) -> super::builder::language_service::AnalyzeSentiment {
126        super::builder::language_service::AnalyzeSentiment::new(self.inner.clone())
127    }
128
129    /// Finds named entities (currently proper names and common nouns) in the text
130    /// along with entity types, probability, mentions for each entity, and
131    /// other properties.
132    pub fn analyze_entities(&self) -> super::builder::language_service::AnalyzeEntities {
133        super::builder::language_service::AnalyzeEntities::new(self.inner.clone())
134    }
135
136    /// Classifies a document into categories.
137    pub fn classify_text(&self) -> super::builder::language_service::ClassifyText {
138        super::builder::language_service::ClassifyText::new(self.inner.clone())
139    }
140
141    /// Moderates a document for harmful and sensitive categories.
142    pub fn moderate_text(&self) -> super::builder::language_service::ModerateText {
143        super::builder::language_service::ModerateText::new(self.inner.clone())
144    }
145
146    /// A convenience method that provides all features in one call.
147    pub fn annotate_text(&self) -> super::builder::language_service::AnnotateText {
148        super::builder::language_service::AnnotateText::new(self.inner.clone())
149    }
150}