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/// # Service Description
25///
26/// [Google Quota Control API](/service-control/overview)
27///
28/// Allows clients to allocate and release quota against a [managed
29/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
30///
31/// # Configuration
32///
33/// `QuotaController` has various configuration parameters, the defaults should
34/// work with most applications.
35///
36/// # Pooling and Cloning
37///
38/// `QuotaController` holds a connection pool internally, it is advised to
39/// create one and the reuse it. You do not need to wrap `QuotaController` in
40/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
41/// internally.
42#[derive(Clone, Debug)]
43pub struct QuotaController {
44 inner: Arc<dyn crate::stubs::dynamic::QuotaController>,
45}
46
47impl QuotaController {
48 /// Creates a new client with the default configuration.
49 pub async fn new() -> Result<Self> {
50 Self::new_with_config(gax::options::ClientConfig::default()).await
51 }
52
53 /// Creates a new client with the specified configuration.
54 pub async fn new_with_config(conf: gax::options::ClientConfig) -> Result<Self> {
55 let inner = Self::build_inner(conf).await?;
56 Ok(Self { inner })
57 }
58
59 /// Creates a new client from the provided stub.
60 ///
61 /// The most common case for calling this function is when mocking the
62 /// client.
63 pub fn from_stub<T>(stub: T) -> Self
64 where
65 T: crate::stubs::QuotaController + 'static,
66 {
67 Self {
68 inner: Arc::new(stub),
69 }
70 }
71
72 async fn build_inner(
73 conf: gax::options::ClientConfig,
74 ) -> Result<Arc<dyn crate::stubs::dynamic::QuotaController>> {
75 if conf.tracing_enabled() {
76 return Ok(Arc::new(Self::build_with_tracing(conf).await?));
77 }
78 Ok(Arc::new(Self::build_transport(conf).await?))
79 }
80
81 async fn build_transport(
82 conf: gax::options::ClientConfig,
83 ) -> Result<impl crate::stubs::QuotaController> {
84 crate::transport::QuotaController::new(conf).await
85 }
86
87 async fn build_with_tracing(
88 conf: gax::options::ClientConfig,
89 ) -> Result<impl crate::stubs::QuotaController> {
90 Self::build_transport(conf)
91 .await
92 .map(crate::tracing::QuotaController::new)
93 }
94
95 /// Attempts to allocate quota for the specified consumer. It should be called
96 /// before the operation is executed.
97 ///
98 /// This method requires the `servicemanagement.services.quota`
99 /// permission on the specified service. For more information, see
100 /// [Cloud IAM](https://cloud.google.com/iam).
101 ///
102 /// **NOTE:** The client **must** fail-open on server errors `INTERNAL`,
103 /// `UNKNOWN`, `DEADLINE_EXCEEDED`, and `UNAVAILABLE`. To ensure system
104 /// reliability, the server may inject these errors to prohibit any hard
105 /// dependency on the quota functionality.
106 pub fn allocate_quota(
107 &self,
108 service_name: impl Into<std::string::String>,
109 ) -> crate::builders::quota_controller::AllocateQuota {
110 crate::builders::quota_controller::AllocateQuota::new(self.inner.clone())
111 .set_service_name(service_name.into())
112 }
113}
114
115/// Implements a client for the Service Control API.
116///
117/// # Service Description
118///
119/// [Google Service Control API](/service-control/overview)
120///
121/// Lets clients check and report operations against a [managed
122/// service](https://cloud.google.com/service-management/reference/rpc/google.api/servicemanagement.v1#google.api.servicemanagement.v1.ManagedService).
123///
124/// # Configuration
125///
126/// `ServiceController` has various configuration parameters, the defaults should
127/// work with most applications.
128///
129/// # Pooling and Cloning
130///
131/// `ServiceController` holds a connection pool internally, it is advised to
132/// create one and the reuse it. You do not need to wrap `ServiceController` in
133/// an [Rc](std::rc::Rc) or [Arc] to reuse it, because it already uses an `Arc`
134/// internally.
135#[derive(Clone, Debug)]
136pub struct ServiceController {
137 inner: Arc<dyn crate::stubs::dynamic::ServiceController>,
138}
139
140impl ServiceController {
141 /// Creates a new client with the default configuration.
142 pub async fn new() -> Result<Self> {
143 Self::new_with_config(gax::options::ClientConfig::default()).await
144 }
145
146 /// Creates a new client with the specified configuration.
147 pub async fn new_with_config(conf: gax::options::ClientConfig) -> Result<Self> {
148 let inner = Self::build_inner(conf).await?;
149 Ok(Self { inner })
150 }
151
152 /// Creates a new client from the provided stub.
153 ///
154 /// The most common case for calling this function is when mocking the
155 /// client.
156 pub fn from_stub<T>(stub: T) -> Self
157 where
158 T: crate::stubs::ServiceController + 'static,
159 {
160 Self {
161 inner: Arc::new(stub),
162 }
163 }
164
165 async fn build_inner(
166 conf: gax::options::ClientConfig,
167 ) -> Result<Arc<dyn crate::stubs::dynamic::ServiceController>> {
168 if conf.tracing_enabled() {
169 return Ok(Arc::new(Self::build_with_tracing(conf).await?));
170 }
171 Ok(Arc::new(Self::build_transport(conf).await?))
172 }
173
174 async fn build_transport(
175 conf: gax::options::ClientConfig,
176 ) -> Result<impl crate::stubs::ServiceController> {
177 crate::transport::ServiceController::new(conf).await
178 }
179
180 async fn build_with_tracing(
181 conf: gax::options::ClientConfig,
182 ) -> Result<impl crate::stubs::ServiceController> {
183 Self::build_transport(conf)
184 .await
185 .map(crate::tracing::ServiceController::new)
186 }
187
188 /// Checks whether an operation on a service should be allowed to proceed
189 /// based on the configuration of the service and related policies. It must be
190 /// called before the operation is executed.
191 ///
192 /// If feasible, the client should cache the check results and reuse them for
193 /// 60 seconds. In case of any server errors, the client should rely on the
194 /// cached results for much longer time to avoid outage.
195 /// WARNING: There is general 60s delay for the configuration and policy
196 /// propagation, therefore callers MUST NOT depend on the `Check` method having
197 /// the latest policy information.
198 ///
199 /// NOTE: the [CheckRequest][google.api.servicecontrol.v1.CheckRequest] has
200 /// the size limit (wire-format byte size) of 1MB.
201 ///
202 /// This method requires the `servicemanagement.services.check` permission
203 /// on the specified service. For more information, see
204 /// [Cloud IAM](https://cloud.google.com/iam).
205 ///
206 /// [google.api.servicecontrol.v1.CheckRequest]: crate::model::CheckRequest
207 pub fn check(
208 &self,
209 service_name: impl Into<std::string::String>,
210 ) -> crate::builders::service_controller::Check {
211 crate::builders::service_controller::Check::new(self.inner.clone())
212 .set_service_name(service_name.into())
213 }
214
215 /// Reports operation results to Google Service Control, such as logs and
216 /// metrics. It should be called after an operation is completed.
217 ///
218 /// If feasible, the client should aggregate reporting data for up to 5
219 /// seconds to reduce API traffic. Limiting aggregation to 5 seconds is to
220 /// reduce data loss during client crashes. Clients should carefully choose
221 /// the aggregation time window to avoid data loss risk more than 0.01%
222 /// for business and compliance reasons.
223 ///
224 /// NOTE: the [ReportRequest][google.api.servicecontrol.v1.ReportRequest] has
225 /// the size limit (wire-format byte size) of 1MB.
226 ///
227 /// This method requires the `servicemanagement.services.report` permission
228 /// on the specified service. For more information, see
229 /// [Google Cloud IAM](https://cloud.google.com/iam).
230 ///
231 /// [google.api.servicecontrol.v1.ReportRequest]: crate::model::ReportRequest
232 pub fn report(
233 &self,
234 service_name: impl Into<std::string::String>,
235 ) -> crate::builders::service_controller::Report {
236 crate::builders::service_controller::Report::new(self.inner.clone())
237 .set_service_name(service_name.into())
238 }
239}