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;
20use std::sync::Arc;
21
22/// Implements a client for the Cloud Natural Language API.
23///
24/// # Service Description
25///
26/// Provides text analysis operations such as sentiment analysis and entity
27/// recognition.
28///
29/// # Configuration
30///
31/// `LanguageService` has various configuration parameters, the defaults should
32/// work with most applications.
33///
34/// # Pooling and Cloning
35///
36/// `LanguageService` holds a connection pool internally, it is advised to
37/// create one and the reuse it.  You do not need to wrap `LanguageService` in
38/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
39/// internally.
40#[derive(Clone, Debug)]
41pub struct LanguageService {
42    inner: Arc<dyn crate::stubs::dynamic::LanguageService>,
43}
44
45impl LanguageService {
46    /// Creates a new client with the default configuration.
47    pub async fn new() -> Result<Self> {
48        Self::new_with_config(gax::options::ClientConfig::default()).await
49    }
50
51    /// Creates a new client with the specified configuration.
52    pub async fn new_with_config(conf: gax::options::ClientConfig) -> Result<Self> {
53        let inner = Self::build_inner(conf).await?;
54        Ok(Self { inner })
55    }
56
57    /// Creates a new client from the provided stub.
58    ///
59    /// The most common case for calling this function is when mocking the
60    /// client.
61    pub fn from_stub<T>(stub: T) -> Self
62    where
63        T: crate::stubs::LanguageService + 'static,
64    {
65        Self {
66            inner: Arc::new(stub),
67        }
68    }
69
70    async fn build_inner(
71        conf: gax::options::ClientConfig,
72    ) -> Result<Arc<dyn crate::stubs::dynamic::LanguageService>> {
73        if conf.tracing_enabled() {
74            return Ok(Arc::new(Self::build_with_tracing(conf).await?));
75        }
76        Ok(Arc::new(Self::build_transport(conf).await?))
77    }
78
79    async fn build_transport(
80        conf: gax::options::ClientConfig,
81    ) -> Result<impl crate::stubs::LanguageService> {
82        crate::transport::LanguageService::new(conf).await
83    }
84
85    async fn build_with_tracing(
86        conf: gax::options::ClientConfig,
87    ) -> Result<impl crate::stubs::LanguageService> {
88        Self::build_transport(conf)
89            .await
90            .map(crate::tracing::LanguageService::new)
91    }
92
93    /// Analyzes the sentiment of the provided text.
94    pub fn analyze_sentiment(&self) -> crate::builders::language_service::AnalyzeSentiment {
95        crate::builders::language_service::AnalyzeSentiment::new(self.inner.clone())
96    }
97
98    /// Finds named entities (currently proper names and common nouns) in the text
99    /// along with entity types, probability, mentions for each entity, and
100    /// other properties.
101    pub fn analyze_entities(&self) -> crate::builders::language_service::AnalyzeEntities {
102        crate::builders::language_service::AnalyzeEntities::new(self.inner.clone())
103    }
104
105    /// Classifies a document into categories.
106    pub fn classify_text(&self) -> crate::builders::language_service::ClassifyText {
107        crate::builders::language_service::ClassifyText::new(self.inner.clone())
108    }
109
110    /// Moderates a document for harmful and sensitive categories.
111    pub fn moderate_text(&self) -> crate::builders::language_service::ModerateText {
112        crate::builders::language_service::ModerateText::new(self.inner.clone())
113    }
114
115    /// A convenience method that provides all features in one call.
116    pub fn annotate_text(&self) -> crate::builders::language_service::AnnotateText {
117        crate::builders::language_service::AnnotateText::new(self.inner.clone())
118    }
119}