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::bare_urls)]
17#![allow(rustdoc::broken_intra_doc_links)]
18#![allow(rustdoc::invalid_html_tags)]
19#![allow(rustdoc::redundant_explicit_links)]
20
21/// Implements a client for the Cloud Natural Language API.
22///
23/// # Example
24/// ```
25/// # use google_cloud_language_v2::client::LanguageService;
26/// async fn sample(
27/// ) -> anyhow::Result<()> {
28/// let client = LanguageService::builder().build().await?;
29/// let response = client.analyze_sentiment()
30/// /* set fields */
31/// .send().await?;
32/// println!("response {:?}", response);
33/// Ok(())
34/// }
35/// ```
36///
37/// # Service Description
38///
39/// Provides text analysis operations such as sentiment analysis and entity
40/// recognition.
41///
42/// # Configuration
43///
44/// To configure `LanguageService` use the `with_*` methods in the type returned
45/// by [builder()][LanguageService::builder]. The default configuration should
46/// work for most applications. Common configuration changes include
47///
48/// * [with_endpoint()]: by default this client uses the global default endpoint
49/// (`https://language.googleapis.com`). Applications using regional
50/// endpoints or running in restricted networks (e.g. a network configured
51/// with [Private Google Access with VPC Service Controls]) may want to
52/// override this default.
53/// * [with_credentials()]: by default this client uses
54/// [Application Default Credentials]. Applications using custom
55/// authentication may need to override this default.
56///
57/// [with_endpoint()]: super::builder::language_service::ClientBuilder::with_endpoint
58/// [with_credentials()]: super::builder::language_service::ClientBuilder::with_credentials
59/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
60/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
61///
62/// # Pooling and Cloning
63///
64/// `LanguageService` holds a connection pool internally, it is advised to
65/// create one and reuse it. You do not need to wrap `LanguageService` in
66/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
67/// already uses an `Arc` internally.
68#[derive(Clone, Debug)]
69pub struct LanguageService {
70 inner: std::sync::Arc<dyn super::stub::dynamic::LanguageService>,
71}
72
73impl LanguageService {
74 /// Returns a builder for [LanguageService].
75 ///
76 /// ```
77 /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
78 /// # use google_cloud_language_v2::client::LanguageService;
79 /// let client = LanguageService::builder().build().await?;
80 /// # Ok(()) }
81 /// ```
82 pub fn builder() -> super::builder::language_service::ClientBuilder {
83 crate::new_client_builder(super::builder::language_service::client::Factory)
84 }
85
86 /// Creates a new client from the provided stub.
87 ///
88 /// The most common case for calling this function is in tests mocking the
89 /// client's behavior.
90 pub fn from_stub<T>(stub: impl Into<std::sync::Arc<T>>) -> Self
91 where
92 T: super::stub::LanguageService + 'static,
93 {
94 Self { inner: stub.into() }
95 }
96
97 pub(crate) async fn new(
98 config: gaxi::options::ClientConfig,
99 ) -> crate::ClientBuilderResult<Self> {
100 let inner = Self::build_inner(config).await?;
101 Ok(Self { inner })
102 }
103
104 async fn build_inner(
105 conf: gaxi::options::ClientConfig,
106 ) -> crate::ClientBuilderResult<std::sync::Arc<dyn super::stub::dynamic::LanguageService>> {
107 if gaxi::options::tracing_enabled(&conf) {
108 return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
109 }
110 Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
111 }
112
113 async fn build_transport(
114 conf: gaxi::options::ClientConfig,
115 ) -> crate::ClientBuilderResult<impl super::stub::LanguageService> {
116 super::transport::LanguageService::new(conf).await
117 }
118
119 async fn build_with_tracing(
120 conf: gaxi::options::ClientConfig,
121 ) -> crate::ClientBuilderResult<impl super::stub::LanguageService> {
122 Self::build_transport(conf)
123 .await
124 .map(super::tracing::LanguageService::new)
125 }
126
127 /// Analyzes the sentiment of the provided text.
128 ///
129 /// # Example
130 /// ```
131 /// # use google_cloud_language_v2::client::LanguageService;
132 /// use google_cloud_language_v2::Result;
133 /// async fn sample(
134 /// client: &LanguageService
135 /// ) -> Result<()> {
136 /// let response = client.analyze_sentiment()
137 /// /* set fields */
138 /// .send().await?;
139 /// println!("response {:?}", response);
140 /// Ok(())
141 /// }
142 /// ```
143 pub fn analyze_sentiment(&self) -> super::builder::language_service::AnalyzeSentiment {
144 super::builder::language_service::AnalyzeSentiment::new(self.inner.clone())
145 }
146
147 /// Finds named entities (currently proper names and common nouns) in the text
148 /// along with entity types, probability, mentions for each entity, and
149 /// other properties.
150 ///
151 /// # Example
152 /// ```
153 /// # use google_cloud_language_v2::client::LanguageService;
154 /// use google_cloud_language_v2::Result;
155 /// async fn sample(
156 /// client: &LanguageService
157 /// ) -> Result<()> {
158 /// let response = client.analyze_entities()
159 /// /* set fields */
160 /// .send().await?;
161 /// println!("response {:?}", response);
162 /// Ok(())
163 /// }
164 /// ```
165 pub fn analyze_entities(&self) -> super::builder::language_service::AnalyzeEntities {
166 super::builder::language_service::AnalyzeEntities::new(self.inner.clone())
167 }
168
169 /// Classifies a document into categories.
170 ///
171 /// # Example
172 /// ```
173 /// # use google_cloud_language_v2::client::LanguageService;
174 /// use google_cloud_language_v2::Result;
175 /// async fn sample(
176 /// client: &LanguageService
177 /// ) -> Result<()> {
178 /// let response = client.classify_text()
179 /// /* set fields */
180 /// .send().await?;
181 /// println!("response {:?}", response);
182 /// Ok(())
183 /// }
184 /// ```
185 pub fn classify_text(&self) -> super::builder::language_service::ClassifyText {
186 super::builder::language_service::ClassifyText::new(self.inner.clone())
187 }
188
189 /// Moderates a document for harmful and sensitive categories.
190 ///
191 /// # Example
192 /// ```
193 /// # use google_cloud_language_v2::client::LanguageService;
194 /// use google_cloud_language_v2::Result;
195 /// async fn sample(
196 /// client: &LanguageService
197 /// ) -> Result<()> {
198 /// let response = client.moderate_text()
199 /// /* set fields */
200 /// .send().await?;
201 /// println!("response {:?}", response);
202 /// Ok(())
203 /// }
204 /// ```
205 pub fn moderate_text(&self) -> super::builder::language_service::ModerateText {
206 super::builder::language_service::ModerateText::new(self.inner.clone())
207 }
208
209 /// A convenience method that provides all features in one call.
210 ///
211 /// # Example
212 /// ```
213 /// # use google_cloud_language_v2::client::LanguageService;
214 /// use google_cloud_language_v2::Result;
215 /// async fn sample(
216 /// client: &LanguageService
217 /// ) -> Result<()> {
218 /// let response = client.annotate_text()
219 /// /* set fields */
220 /// .send().await?;
221 /// println!("response {:?}", response);
222 /// Ok(())
223 /// }
224 /// ```
225 pub fn annotate_text(&self) -> super::builder::language_service::AnnotateText {
226 super::builder::language_service::AnnotateText::new(self.inner.clone())
227 }
228}