Skip to main content

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