google_cloud_longrunning/client.rs
1// Copyright 2024 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::bare_urls)]
17#![allow(rustdoc::broken_intra_doc_links)]
18#![allow(rustdoc::invalid_html_tags)]
19#![allow(rustdoc::redundant_explicit_links)]
20
21/// Implements a client for the Long Running Operations API.
22///
23/// # Example
24/// ```
25/// # use google_cloud_longrunning::client::Operations;
26/// use google_cloud_gax::paginator::ItemPaginator as _;
27/// async fn sample(
28/// ) -> anyhow::Result<()> {
29/// let client = Operations::builder().build().await?;
30/// let mut list = client.list_operations()
31/// /* set fields */
32/// .by_item();
33/// while let Some(item) = list.next().await.transpose()? {
34/// println!("{:?}", item);
35/// }
36/// Ok(())
37/// }
38/// ```
39///
40/// # Service Description
41///
42/// Manages long-running operations with an API service.
43///
44/// When an API method normally takes long time to complete, it can be designed
45/// to return [Operation][google.longrunning.Operation] to the client, and the
46/// client can use this interface to receive the real response asynchronously by
47/// polling the operation resource, or pass the operation resource to another API
48/// (such as Pub/Sub API) to receive the response. Any API service that returns
49/// long-running operations should implement the `Operations` interface so
50/// developers can have a consistent client experience.
51///
52/// [google.longrunning.Operation]: crate::model::Operation
53///
54/// # Configuration
55///
56/// To configure `Operations` use the `with_*` methods in the type returned
57/// by [builder()][Operations::builder]. The default configuration should
58/// work for most applications. Common configuration changes include
59///
60/// * [with_endpoint()]: by default this client uses the global default endpoint
61/// (`https://longrunning.googleapis.com`). Applications using regional
62/// endpoints or running in restricted networks (e.g. a network configured
63/// with [Private Google Access with VPC Service Controls]) may want to
64/// override this default.
65/// * [with_credentials()]: by default this client uses
66/// [Application Default Credentials]. Applications using custom
67/// authentication may need to override this default.
68///
69/// [with_endpoint()]: super::builder::operations::ClientBuilder::with_endpoint
70/// [with_credentials()]: super::builder::operations::ClientBuilder::with_credentials
71/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
72/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
73///
74/// # Pooling and Cloning
75///
76/// `Operations` holds a connection pool internally, it is advised to
77/// create one and reuse it. You do not need to wrap `Operations` in
78/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
79/// already uses an `Arc` internally.
80#[derive(Clone, Debug)]
81pub struct Operations {
82 inner: std::sync::Arc<dyn super::stub::dynamic::Operations>,
83}
84
85impl Operations {
86 /// Returns a builder for [Operations].
87 ///
88 /// ```
89 /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
90 /// # use google_cloud_longrunning::client::Operations;
91 /// let client = Operations::builder().build().await?;
92 /// # Ok(()) }
93 /// ```
94 pub fn builder() -> super::builder::operations::ClientBuilder {
95 crate::new_client_builder(super::builder::operations::client::Factory)
96 }
97
98 /// Creates a new client from the provided stub.
99 ///
100 /// The most common case for calling this function is in tests mocking the
101 /// client's behavior.
102 pub fn from_stub<T>(stub: impl Into<std::sync::Arc<T>>) -> Self
103 where
104 T: super::stub::Operations + 'static,
105 {
106 Self { inner: stub.into() }
107 }
108
109 pub(crate) async fn new(
110 config: gaxi::options::ClientConfig,
111 ) -> crate::ClientBuilderResult<Self> {
112 let inner = Self::build_inner(config).await?;
113 Ok(Self { inner })
114 }
115
116 async fn build_inner(
117 conf: gaxi::options::ClientConfig,
118 ) -> crate::ClientBuilderResult<std::sync::Arc<dyn super::stub::dynamic::Operations>> {
119 if gaxi::options::tracing_enabled(&conf) {
120 return Ok(std::sync::Arc::new(Self::build_with_tracing(conf).await?));
121 }
122 Ok(std::sync::Arc::new(Self::build_transport(conf).await?))
123 }
124
125 async fn build_transport(
126 conf: gaxi::options::ClientConfig,
127 ) -> crate::ClientBuilderResult<impl super::stub::Operations> {
128 super::transport::Operations::new(conf).await
129 }
130
131 async fn build_with_tracing(
132 conf: gaxi::options::ClientConfig,
133 ) -> crate::ClientBuilderResult<impl super::stub::Operations> {
134 Self::build_transport(conf)
135 .await
136 .map(super::tracing::Operations::new)
137 }
138
139 /// Lists operations that match the specified filter in the request. If the
140 /// server doesn't support this method, it returns `UNIMPLEMENTED`.
141 ///
142 /// # Example
143 /// ```
144 /// # use google_cloud_longrunning::client::Operations;
145 /// use google_cloud_gax::paginator::ItemPaginator as _;
146 /// use google_cloud_longrunning::Result;
147 /// async fn sample(
148 /// client: &Operations
149 /// ) -> Result<()> {
150 /// let mut list = client.list_operations()
151 /// /* set fields */
152 /// .by_item();
153 /// while let Some(item) = list.next().await.transpose()? {
154 /// println!("{:?}", item);
155 /// }
156 /// Ok(())
157 /// }
158 /// ```
159 pub fn list_operations(&self) -> super::builder::operations::ListOperations {
160 super::builder::operations::ListOperations::new(self.inner.clone())
161 }
162
163 /// Gets the latest state of a long-running operation. Clients can use this
164 /// method to poll the operation result at intervals as recommended by the API
165 /// service.
166 ///
167 /// # Example
168 /// ```
169 /// # use google_cloud_longrunning::client::Operations;
170 /// use google_cloud_longrunning::Result;
171 /// async fn sample(
172 /// client: &Operations
173 /// ) -> Result<()> {
174 /// let response = client.get_operation()
175 /// /* set fields */
176 /// .send().await?;
177 /// println!("response {:?}", response);
178 /// Ok(())
179 /// }
180 /// ```
181 pub fn get_operation(&self) -> super::builder::operations::GetOperation {
182 super::builder::operations::GetOperation::new(self.inner.clone())
183 }
184
185 /// Deletes a long-running operation. This method indicates that the client is
186 /// no longer interested in the operation result. It does not cancel the
187 /// operation. If the server doesn't support this method, it returns
188 /// `google.rpc.Code.UNIMPLEMENTED`.
189 ///
190 /// # Example
191 /// ```
192 /// # use google_cloud_longrunning::client::Operations;
193 /// use google_cloud_longrunning::Result;
194 /// async fn sample(
195 /// client: &Operations
196 /// ) -> Result<()> {
197 /// client.delete_operation()
198 /// /* set fields */
199 /// .send().await?;
200 /// Ok(())
201 /// }
202 /// ```
203 pub fn delete_operation(&self) -> super::builder::operations::DeleteOperation {
204 super::builder::operations::DeleteOperation::new(self.inner.clone())
205 }
206
207 /// Starts asynchronous cancellation on a long-running operation. The server
208 /// makes a best effort to cancel the operation, but success is not
209 /// guaranteed. If the server doesn't support this method, it returns
210 /// `google.rpc.Code.UNIMPLEMENTED`. Clients can use
211 /// [Operations.GetOperation][google.longrunning.Operations.GetOperation] or
212 /// other methods to check whether the cancellation succeeded or whether the
213 /// operation completed despite cancellation. On successful cancellation,
214 /// the operation is not deleted; instead, it becomes an operation with
215 /// an [Operation.error][google.longrunning.Operation.error] value with a
216 /// [google.rpc.Status.code][google.rpc.Status.code] of `1`, corresponding to
217 /// `Code.CANCELLED`.
218 ///
219 /// [google.longrunning.Operation.error]: crate::model::Operation::result
220 /// [google.longrunning.Operations.GetOperation]: google-cloud-longrunning::client::Operations::get_operation
221 /// [google.rpc.Status.code]: google_cloud_rpc::model::Status::code
222 ///
223 /// # Example
224 /// ```
225 /// # use google_cloud_longrunning::client::Operations;
226 /// use google_cloud_longrunning::Result;
227 /// async fn sample(
228 /// client: &Operations
229 /// ) -> Result<()> {
230 /// client.cancel_operation()
231 /// /* set fields */
232 /// .send().await?;
233 /// Ok(())
234 /// }
235 /// ```
236 pub fn cancel_operation(&self) -> super::builder::operations::CancelOperation {
237 super::builder::operations::CancelOperation::new(self.inner.clone())
238 }
239}