google_cloud_api_servicecontrol_v1/
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 Service Control API.
23///
24/// # Example
25/// ```
26/// # tokio_test::block_on(async {
27/// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
28/// let client = QuotaController::builder().build().await?;
29/// // use `client` to make requests to the {Codec.APITitle}}.
30/// # gax::Result::<()>::Ok(()) });
31/// ```
32///
33/// # Service Description
34///
35/// [Google Quota Control API](/service-control/overview)
36///
37/// Allows clients to allocate and release quota against a [managed
38/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
39///
40/// # Configuration
41///
42/// To configure `QuotaController` use the `with_*` methods in the type returned
43/// by [builder()][QuotaController::builder]. The default configuration should
44/// work for most applications. Common configuration changes include
45///
46/// * [with_endpoint()]: by default this client uses the global default endpoint
47///   (`https://servicecontrol.googleapis.com`). Applications using regional
48///   endpoints or running in restricted networks (e.g. a network configured
49//    with [Private Google Access with VPC Service Controls]) may want to
50///   override this default.
51/// * [with_credentials()]: by default this client uses
52///   [Application Default Credentials]. Applications using custom
53///   authentication may need to override this default.
54///
55/// [with_endpoint()]: super::builder::quota_controller::ClientBuilder::with_endpoint
56/// [with_credentials()]: super::builder::quota_controller::ClientBuilder::credentials
57/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
58/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
59///
60/// # Pooling and Cloning
61///
62/// `QuotaController` holds a connection pool internally, it is advised to
63/// create one and the reuse it.  You do not need to wrap `QuotaController` in
64/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
65/// internally.
66#[derive(Clone, Debug)]
67pub struct QuotaController {
68    inner: Arc<dyn super::stub::dynamic::QuotaController>,
69}
70
71impl QuotaController {
72    /// Returns a builder for [QuotaController].
73    ///
74    /// ```
75    /// # tokio_test::block_on(async {
76    /// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
77    /// let client = QuotaController::builder().build().await?;
78    /// # gax::Result::<()>::Ok(()) });
79    /// ```
80    pub fn builder() -> super::builder::quota_controller::ClientBuilder {
81        gax::client_builder::internal::new_builder(
82            super::builder::quota_controller::client::Factory,
83        )
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: T) -> Self
91    where
92        T: super::stub::QuotaController + 'static,
93    {
94        Self {
95            inner: Arc::new(stub),
96        }
97    }
98
99    pub(crate) async fn new(config: gaxi::options::ClientConfig) -> Result<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    ) -> Result<Arc<dyn super::stub::dynamic::QuotaController>> {
107        if gaxi::options::tracing_enabled(&conf) {
108            return Ok(Arc::new(Self::build_with_tracing(conf).await?));
109        }
110        Ok(Arc::new(Self::build_transport(conf).await?))
111    }
112
113    async fn build_transport(
114        conf: gaxi::options::ClientConfig,
115    ) -> Result<impl super::stub::QuotaController> {
116        super::transport::QuotaController::new(conf).await
117    }
118
119    async fn build_with_tracing(
120        conf: gaxi::options::ClientConfig,
121    ) -> Result<impl super::stub::QuotaController> {
122        Self::build_transport(conf)
123            .await
124            .map(super::tracing::QuotaController::new)
125    }
126
127    /// Attempts to allocate quota for the specified consumer. It should be called
128    /// before the operation is executed.
129    ///
130    /// This method requires the `servicemanagement.services.quota`
131    /// permission on the specified service. For more information, see
132    /// [Cloud IAM](https://cloud.google.com/iam).
133    ///
134    /// **NOTE:** The client **must** fail-open on server errors `INTERNAL`,
135    /// `UNKNOWN`, `DEADLINE_EXCEEDED`, and `UNAVAILABLE`. To ensure system
136    /// reliability, the server may inject these errors to prohibit any hard
137    /// dependency on the quota functionality.
138    pub fn allocate_quota(
139        &self,
140        service_name: impl Into<std::string::String>,
141    ) -> super::builder::quota_controller::AllocateQuota {
142        super::builder::quota_controller::AllocateQuota::new(self.inner.clone())
143            .set_service_name(service_name.into())
144    }
145}
146
147/// Implements a client for the Service Control API.
148///
149/// # Example
150/// ```
151/// # tokio_test::block_on(async {
152/// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
153/// let client = ServiceController::builder().build().await?;
154/// // use `client` to make requests to the {Codec.APITitle}}.
155/// # gax::Result::<()>::Ok(()) });
156/// ```
157///
158/// # Service Description
159///
160/// [Google Service Control API](/service-control/overview)
161///
162/// Lets clients check and report operations against a [managed
163/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
164///
165/// # Configuration
166///
167/// To configure `ServiceController` use the `with_*` methods in the type returned
168/// by [builder()][ServiceController::builder]. The default configuration should
169/// work for most applications. Common configuration changes include
170///
171/// * [with_endpoint()]: by default this client uses the global default endpoint
172///   (`https://servicecontrol.googleapis.com`). Applications using regional
173///   endpoints or running in restricted networks (e.g. a network configured
174//    with [Private Google Access with VPC Service Controls]) may want to
175///   override this default.
176/// * [with_credentials()]: by default this client uses
177///   [Application Default Credentials]. Applications using custom
178///   authentication may need to override this default.
179///
180/// [with_endpoint()]: super::builder::service_controller::ClientBuilder::with_endpoint
181/// [with_credentials()]: super::builder::service_controller::ClientBuilder::credentials
182/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
183/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
184///
185/// # Pooling and Cloning
186///
187/// `ServiceController` holds a connection pool internally, it is advised to
188/// create one and the reuse it.  You do not need to wrap `ServiceController` in
189/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
190/// internally.
191#[derive(Clone, Debug)]
192pub struct ServiceController {
193    inner: Arc<dyn super::stub::dynamic::ServiceController>,
194}
195
196impl ServiceController {
197    /// Returns a builder for [ServiceController].
198    ///
199    /// ```
200    /// # tokio_test::block_on(async {
201    /// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
202    /// let client = ServiceController::builder().build().await?;
203    /// # gax::Result::<()>::Ok(()) });
204    /// ```
205    pub fn builder() -> super::builder::service_controller::ClientBuilder {
206        gax::client_builder::internal::new_builder(
207            super::builder::service_controller::client::Factory,
208        )
209    }
210
211    /// Creates a new client from the provided stub.
212    ///
213    /// The most common case for calling this function is in tests mocking the
214    /// client's behavior.
215    pub fn from_stub<T>(stub: T) -> Self
216    where
217        T: super::stub::ServiceController + 'static,
218    {
219        Self {
220            inner: Arc::new(stub),
221        }
222    }
223
224    pub(crate) async fn new(config: gaxi::options::ClientConfig) -> Result<Self> {
225        let inner = Self::build_inner(config).await?;
226        Ok(Self { inner })
227    }
228
229    async fn build_inner(
230        conf: gaxi::options::ClientConfig,
231    ) -> Result<Arc<dyn super::stub::dynamic::ServiceController>> {
232        if gaxi::options::tracing_enabled(&conf) {
233            return Ok(Arc::new(Self::build_with_tracing(conf).await?));
234        }
235        Ok(Arc::new(Self::build_transport(conf).await?))
236    }
237
238    async fn build_transport(
239        conf: gaxi::options::ClientConfig,
240    ) -> Result<impl super::stub::ServiceController> {
241        super::transport::ServiceController::new(conf).await
242    }
243
244    async fn build_with_tracing(
245        conf: gaxi::options::ClientConfig,
246    ) -> Result<impl super::stub::ServiceController> {
247        Self::build_transport(conf)
248            .await
249            .map(super::tracing::ServiceController::new)
250    }
251
252    /// Checks whether an operation on a service should be allowed to proceed
253    /// based on the configuration of the service and related policies. It must be
254    /// called before the operation is executed.
255    ///
256    /// If feasible, the client should cache the check results and reuse them for
257    /// 60 seconds. In case of any server errors, the client should rely on the
258    /// cached results for much longer time to avoid outage.
259    /// WARNING: There is general 60s delay for the configuration and policy
260    /// propagation, therefore callers MUST NOT depend on the `Check` method having
261    /// the latest policy information.
262    ///
263    /// NOTE: the [CheckRequest][google.api.servicecontrol.v1.CheckRequest] has
264    /// the size limit (wire-format byte size) of 1MB.
265    ///
266    /// This method requires the `servicemanagement.services.check` permission
267    /// on the specified service. For more information, see
268    /// [Cloud IAM](https://cloud.google.com/iam).
269    ///
270    /// [google.api.servicecontrol.v1.CheckRequest]: crate::model::CheckRequest
271    pub fn check(
272        &self,
273        service_name: impl Into<std::string::String>,
274    ) -> super::builder::service_controller::Check {
275        super::builder::service_controller::Check::new(self.inner.clone())
276            .set_service_name(service_name.into())
277    }
278
279    /// Reports operation results to Google Service Control, such as logs and
280    /// metrics. It should be called after an operation is completed.
281    ///
282    /// If feasible, the client should aggregate reporting data for up to 5
283    /// seconds to reduce API traffic. Limiting aggregation to 5 seconds is to
284    /// reduce data loss during client crashes. Clients should carefully choose
285    /// the aggregation time window to avoid data loss risk more than 0.01%
286    /// for business and compliance reasons.
287    ///
288    /// NOTE: the [ReportRequest][google.api.servicecontrol.v1.ReportRequest] has
289    /// the size limit (wire-format byte size) of 1MB.
290    ///
291    /// This method requires the `servicemanagement.services.report` permission
292    /// on the specified service. For more information, see
293    /// [Google Cloud IAM](https://cloud.google.com/iam).
294    ///
295    /// [google.api.servicecontrol.v1.ReportRequest]: crate::model::ReportRequest
296    pub fn report(
297        &self,
298        service_name: impl Into<std::string::String>,
299    ) -> super::builder::service_controller::Report {
300        super::builder::service_controller::Report::new(self.inner.clone())
301            .set_service_name(service_name.into())
302    }
303}