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(
25/// ) -> anyhow::Result<()> {
26///     let client = QuotaController::builder().build().await?;
27///     let response = client.allocate_quota()
28///         /* set fields */
29///         .send().await?;
30///     println!("response {:?}", response);
31///     Ok(())
32/// }
33/// ```
34///
35/// # Service Description
36///
37/// [Google Quota Control API](/service-control/overview)
38///
39/// Allows clients to allocate and release quota against a [managed
40/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
41///
42/// # Configuration
43///
44/// To configure `QuotaController` use the `with_*` methods in the type returned
45/// by [builder()][QuotaController::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://servicecontrol.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::quota_controller::ClientBuilder::with_endpoint
58/// [with_credentials()]: super::builder::quota_controller::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/// `QuotaController` holds a connection pool internally, it is advised to
65/// create one and reuse it. You do not need to wrap `QuotaController` 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 QuotaController {
70    inner: std::sync::Arc<dyn super::stub::dynamic::QuotaController>,
71}
72
73impl QuotaController {
74    /// Returns a builder for [QuotaController].
75    ///
76    /// ```
77    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
78    /// # use google_cloud_api_servicecontrol_v1::client::QuotaController;
79    /// let client = QuotaController::builder().build().await?;
80    /// # Ok(()) }
81    /// ```
82    pub fn builder() -> super::builder::quota_controller::ClientBuilder {
83        crate::new_client_builder(super::builder::quota_controller::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::QuotaController + '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::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(
164/// ) -> anyhow::Result<()> {
165///     let client = ServiceController::builder().build().await?;
166///     let response = client.check()
167///         /* set fields */
168///         .send().await?;
169///     println!("response {:?}", response);
170///     Ok(())
171/// }
172/// ```
173///
174/// # Service Description
175///
176/// [Google Service Control API](/service-control/overview)
177///
178/// Lets clients check and report operations against a [managed
179/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
180///
181/// # Configuration
182///
183/// To configure `ServiceController` use the `with_*` methods in the type returned
184/// by [builder()][ServiceController::builder]. The default configuration should
185/// work for most applications. Common configuration changes include
186///
187/// * [with_endpoint()]: by default this client uses the global default endpoint
188///   (`https://servicecontrol.googleapis.com`). Applications using regional
189///   endpoints or running in restricted networks (e.g. a network configured
190//    with [Private Google Access with VPC Service Controls]) may want to
191///   override this default.
192/// * [with_credentials()]: by default this client uses
193///   [Application Default Credentials]. Applications using custom
194///   authentication may need to override this default.
195///
196/// [with_endpoint()]: super::builder::service_controller::ClientBuilder::with_endpoint
197/// [with_credentials()]: super::builder::service_controller::ClientBuilder::with_credentials
198/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
199/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
200///
201/// # Pooling and Cloning
202///
203/// `ServiceController` holds a connection pool internally, it is advised to
204/// create one and reuse it. You do not need to wrap `ServiceController` in
205/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
206/// already uses an `Arc` internally.
207#[derive(Clone, Debug)]
208pub struct ServiceController {
209    inner: std::sync::Arc<dyn super::stub::dynamic::ServiceController>,
210}
211
212impl ServiceController {
213    /// Returns a builder for [ServiceController].
214    ///
215    /// ```
216    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
217    /// # use google_cloud_api_servicecontrol_v1::client::ServiceController;
218    /// let client = ServiceController::builder().build().await?;
219    /// # Ok(()) }
220    /// ```
221    pub fn builder() -> super::builder::service_controller::ClientBuilder {
222        crate::new_client_builder(super::builder::service_controller::client::Factory)
223    }
224
225    /// Creates a new client from the provided stub.
226    ///
227    /// The most common case for calling this function is in tests mocking the
228    /// client's behavior.
229    pub fn from_stub<T>(stub: impl Into<std::sync::Arc<T>>) -> Self
230    where
231        T: super::stub::ServiceController + 'static,
232    {
233        Self { inner: stub.into() }
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}