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/// # tokio_test::block_on(async {
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/// # gax::client_builder::Result::<()>::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    /// # tokio_test::block_on(async {
71    /// # use google_cloud_language_v2::client::LanguageService;
72    /// let client = LanguageService::builder().build().await?;
73    /// # gax::client_builder::Result::<()>::Ok(()) });
74    /// ```
75    pub fn builder() -> super::builder::language_service::ClientBuilder {
76        gax::client_builder::internal::new_builder(
77            super::builder::language_service::client::Factory,
78        )
79    }
80
81    /// Creates a new client from the provided stub.
82    ///
83    /// The most common case for calling this function is in tests mocking the
84    /// client's behavior.
85    pub fn from_stub<T>(stub: T) -> Self
86    where
87        T: super::stub::LanguageService + 'static,
88    {
89        Self {
90            inner: std::sync::Arc::new(stub),
91        }
92    }
93
94    pub(crate) async fn new(
95        config: gaxi::options::ClientConfig,
96    ) -> gax::client_builder::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    ) -> gax::client_builder::Result<std::sync::Arc<dyn super::stub::dynamic::LanguageService>>
104    {
105        if gaxi::options::tracing_enabled(&conf) {
106            return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
107        }
108        Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
109    }
110
111    async fn build_transport(
112        conf: gaxi::options::ClientConfig,
113    ) -> gax::client_builder::Result<impl super::stub::LanguageService> {
114        super::transport::LanguageService::new(conf).await
115    }
116
117    async fn build_with_tracing(
118        conf: gaxi::options::ClientConfig,
119    ) -> gax::client_builder::Result<impl super::stub::LanguageService> {
120        Self::build_transport(conf)
121            .await
122            .map(super::tracing::LanguageService::new)
123    }
124
125    /// Analyzes the sentiment of the provided text.
126    pub fn analyze_sentiment(&self) -> super::builder::language_service::AnalyzeSentiment {
127        super::builder::language_service::AnalyzeSentiment::new(self.inner.clone())
128    }
129
130    /// Finds named entities (currently proper names and common nouns) in the text
131    /// along with entity types, probability, mentions for each entity, and
132    /// other properties.
133    pub fn analyze_entities(&self) -> super::builder::language_service::AnalyzeEntities {
134        super::builder::language_service::AnalyzeEntities::new(self.inner.clone())
135    }
136
137    /// Classifies a document into categories.
138    pub fn classify_text(&self) -> super::builder::language_service::ClassifyText {
139        super::builder::language_service::ClassifyText::new(self.inner.clone())
140    }
141
142    /// Moderates a document for harmful and sensitive categories.
143    pub fn moderate_text(&self) -> super::builder::language_service::ModerateText {
144        super::builder::language_service::ModerateText::new(self.inner.clone())
145    }
146
147    /// A convenience method that provides all features in one call.
148    pub fn annotate_text(&self) -> super::builder::language_service::AnnotateText {
149        super::builder::language_service::AnnotateText::new(self.inner.clone())
150    }
151}