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