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