Skip to main content

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
19/// Implements a client for the Service Control API.
20///
21/// # Example
22/// ```
23/// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
24/// # async fn sample() -> Result<(), Box<dyn std::error::Error>> {
25///     let client = QuotaController::builder().build().await?;
26///     let response = client.allocate_quota()
27///         /* set fields */
28///         .send().await?;
29///     println!("response {:?}", response);
30/// # 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::with_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 reuse it. You do not need to wrap `QuotaController` in
64/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
65/// already uses an `Arc` internally.
66#[derive(Clone, Debug)]
67pub struct QuotaController {
68    inner: std::sync::Arc<dyn super::stub::dynamic::QuotaController>,
69}
70
71impl QuotaController {
72    /// Returns a builder for [QuotaController].
73    ///
74    /// ```
75    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
76    /// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
77    /// let client = QuotaController::builder().build().await?;
78    /// # Ok(()) }
79    /// ```
80    pub fn builder() -> super::builder::quota_controller::ClientBuilder {
81        crate::new_client_builder(super::builder::quota_controller::client::Factory)
82    }
83
84    /// Creates a new client from the provided stub.
85    ///
86    /// The most common case for calling this function is in tests mocking the
87    /// client's behavior.
88    pub fn from_stub<T>(stub: T) -> Self
89    where
90        T: super::stub::QuotaController + 'static,
91    {
92        Self {
93            inner: std::sync::Arc::new(stub),
94        }
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::QuotaController>> {
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::QuotaController> {
116        super::transport::QuotaController::new(conf).await
117    }
118
119    async fn build_with_tracing(
120        conf: gaxi::options::ClientConfig,
121    ) -> crate::ClientBuilderResult<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    ///
139    /// # Example
140    /// ```
141    /// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
142    /// use google_cloud_api_servicecontrol_v1::Result;
143    /// async fn sample(
144    ///    client: &QuotaController
145    /// ) -> Result<()> {
146    ///     let response = client.allocate_quota()
147    ///         /* set fields */
148    ///         .send().await?;
149    ///     println!("response {:?}", response);
150    ///     Ok(())
151    /// }
152    /// ```
153    pub fn allocate_quota(&self) -> super::builder::quota_controller::AllocateQuota {
154        super::builder::quota_controller::AllocateQuota::new(self.inner.clone())
155    }
156}
157
158/// Implements a client for the Service Control API.
159///
160/// # Example
161/// ```
162/// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
163/// # async fn sample() -> Result<(), Box<dyn std::error::Error>> {
164///     let client = ServiceController::builder().build().await?;
165///     let response = client.check()
166///         /* set fields */
167///         .send().await?;
168///     println!("response {:?}", response);
169/// # Ok(()) }
170/// ```
171///
172/// # Service Description
173///
174/// [Google Service Control API](/service-control/overview)
175///
176/// Lets clients check and report operations against a [managed
177/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
178///
179/// # Configuration
180///
181/// To configure `ServiceController` use the `with_*` methods in the type returned
182/// by [builder()][ServiceController::builder]. The default configuration should
183/// work for most applications. Common configuration changes include
184///
185/// * [with_endpoint()]: by default this client uses the global default endpoint
186///   (`https://servicecontrol.googleapis.com`). Applications using regional
187///   endpoints or running in restricted networks (e.g. a network configured
188//    with [Private Google Access with VPC Service Controls]) may want to
189///   override this default.
190/// * [with_credentials()]: by default this client uses
191///   [Application Default Credentials]. Applications using custom
192///   authentication may need to override this default.
193///
194/// [with_endpoint()]: super::builder::service_controller::ClientBuilder::with_endpoint
195/// [with_credentials()]: super::builder::service_controller::ClientBuilder::with_credentials
196/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
197/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
198///
199/// # Pooling and Cloning
200///
201/// `ServiceController` holds a connection pool internally, it is advised to
202/// create one and reuse it. You do not need to wrap `ServiceController` in
203/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
204/// already uses an `Arc` internally.
205#[derive(Clone, Debug)]
206pub struct ServiceController {
207    inner: std::sync::Arc<dyn super::stub::dynamic::ServiceController>,
208}
209
210impl ServiceController {
211    /// Returns a builder for [ServiceController].
212    ///
213    /// ```
214    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
215    /// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
216    /// let client = ServiceController::builder().build().await?;
217    /// # Ok(()) }
218    /// ```
219    pub fn builder() -> super::builder::service_controller::ClientBuilder {
220        crate::new_client_builder(super::builder::service_controller::client::Factory)
221    }
222
223    /// Creates a new client from the provided stub.
224    ///
225    /// The most common case for calling this function is in tests mocking the
226    /// client's behavior.
227    pub fn from_stub<T>(stub: T) -> Self
228    where
229        T: super::stub::ServiceController + 'static,
230    {
231        Self {
232            inner: std::sync::Arc::new(stub),
233        }
234    }
235
236    pub(crate) async fn new(
237        config: gaxi::options::ClientConfig,
238    ) -> crate::ClientBuilderResult<Self> {
239        let inner = Self::build_inner(config).await?;
240        Ok(Self { inner })
241    }
242
243    async fn build_inner(
244        conf: gaxi::options::ClientConfig,
245    ) -> crate::ClientBuilderResult<std::sync::Arc<dyn super::stub::dynamic::ServiceController>>
246    {
247        if gaxi::options::tracing_enabled(&conf) {
248            return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
249        }
250        Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
251    }
252
253    async fn build_transport(
254        conf: gaxi::options::ClientConfig,
255    ) -> crate::ClientBuilderResult<impl super::stub::ServiceController> {
256        super::transport::ServiceController::new(conf).await
257    }
258
259    async fn build_with_tracing(
260        conf: gaxi::options::ClientConfig,
261    ) -> crate::ClientBuilderResult<impl super::stub::ServiceController> {
262        Self::build_transport(conf)
263            .await
264            .map(super::tracing::ServiceController::new)
265    }
266
267    /// Checks whether an operation on a service should be allowed to proceed
268    /// based on the configuration of the service and related policies. It must be
269    /// called before the operation is executed.
270    ///
271    /// If feasible, the client should cache the check results and reuse them for
272    /// 60 seconds. In case of any server errors, the client should rely on the
273    /// cached results for much longer time to avoid outage.
274    /// WARNING: There is general 60s delay for the configuration and policy
275    /// propagation, therefore callers MUST NOT depend on the `Check` method having
276    /// the latest policy information.
277    ///
278    /// NOTE: the [CheckRequest][google.api.servicecontrol.v1.CheckRequest] has
279    /// the size limit (wire-format byte size) of 1MB.
280    ///
281    /// This method requires the `servicemanagement.services.check` permission
282    /// on the specified service. For more information, see
283    /// [Cloud IAM](https://cloud.google.com/iam).
284    ///
285    /// [google.api.servicecontrol.v1.CheckRequest]: crate::model::CheckRequest
286    ///
287    /// # Example
288    /// ```
289    /// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
290    /// use google_cloud_api_servicecontrol_v1::Result;
291    /// async fn sample(
292    ///    client: &ServiceController
293    /// ) -> Result<()> {
294    ///     let response = client.check()
295    ///         /* set fields */
296    ///         .send().await?;
297    ///     println!("response {:?}", response);
298    ///     Ok(())
299    /// }
300    /// ```
301    pub fn check(&self) -> super::builder::service_controller::Check {
302        super::builder::service_controller::Check::new(self.inner.clone())
303    }
304
305    /// Reports operation results to Google Service Control, such as logs and
306    /// metrics. It should be called after an operation is completed.
307    ///
308    /// If feasible, the client should aggregate reporting data for up to 5
309    /// seconds to reduce API traffic. Limiting aggregation to 5 seconds is to
310    /// reduce data loss during client crashes. Clients should carefully choose
311    /// the aggregation time window to avoid data loss risk more than 0.01%
312    /// for business and compliance reasons.
313    ///
314    /// NOTE: the [ReportRequest][google.api.servicecontrol.v1.ReportRequest] has
315    /// the size limit (wire-format byte size) of 1MB.
316    ///
317    /// This method requires the `servicemanagement.services.report` permission
318    /// on the specified service. For more information, see
319    /// [Google Cloud IAM](https://cloud.google.com/iam).
320    ///
321    /// [google.api.servicecontrol.v1.ReportRequest]: crate::model::ReportRequest
322    ///
323    /// # Example
324    /// ```
325    /// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
326    /// use google_cloud_api_servicecontrol_v1::Result;
327    /// async fn sample(
328    ///    client: &ServiceController
329    /// ) -> Result<()> {
330    ///     let response = client.report()
331    ///         /* set fields */
332    ///         .send().await?;
333    ///     println!("response {:?}", response);
334    ///     Ok(())
335    /// }
336    /// ```
337    pub fn report(&self) -> super::builder::service_controller::Report {
338        super::builder::service_controller::Report::new(self.inner.clone())
339    }
340}