Skip to main content

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() -> Result<(), Box<dyn std::error::Error>> {
26///     let client = Operations::builder().build().await?;
27///     let mut list = client.list_operations()
28///         /* set fields */
29///         .by_item();
30///     while let Some(item) = list.next().await.transpose()? {
31///         println!("{:?}", item);
32///     }
33/// # Ok(()) }
34/// ```
35///
36/// # Service Description
37///
38/// Manages long-running operations with an API service.
39///
40/// When an API method normally takes long time to complete, it can be designed
41/// to return [Operation][google.longrunning.Operation] to the client, and the
42/// client can use this interface to receive the real response asynchronously by
43/// polling the operation resource, or pass the operation resource to another API
44/// (such as Pub/Sub API) to receive the response.  Any API service that returns
45/// long-running operations should implement the `Operations` interface so
46/// developers can have a consistent client experience.
47///
48/// [google.longrunning.Operation]: crate::model::Operation
49///
50/// # Configuration
51///
52/// To configure `Operations` use the `with_*` methods in the type returned
53/// by [builder()][Operations::builder]. The default configuration should
54/// work for most applications. Common configuration changes include
55///
56/// * [with_endpoint()]: by default this client uses the global default endpoint
57///   (`https://longrunning.googleapis.com`). Applications using regional
58///   endpoints or running in restricted networks (e.g. a network configured
59//    with [Private Google Access with VPC Service Controls]) may want to
60///   override this default.
61/// * [with_credentials()]: by default this client uses
62///   [Application Default Credentials]. Applications using custom
63///   authentication may need to override this default.
64///
65/// [with_endpoint()]: super::builder::operations::ClientBuilder::with_endpoint
66/// [with_credentials()]: super::builder::operations::ClientBuilder::with_credentials
67/// [Private Google Access with VPC Service Controls]: https://cloud.google.com/vpc-service-controls/docs/private-connectivity
68/// [Application Default Credentials]: https://cloud.google.com/docs/authentication#adc
69///
70/// # Pooling and Cloning
71///
72/// `Operations` holds a connection pool internally, it is advised to
73/// create one and reuse it. You do not need to wrap `Operations` in
74/// an [Rc](std::rc::Rc) or [Arc](std::sync::Arc) to reuse it, because it
75/// already uses an `Arc` internally.
76#[derive(Clone, Debug)]
77pub struct Operations {
78    inner: std::sync::Arc<dyn super::stub::dynamic::Operations>,
79}
80
81impl Operations {
82    /// Returns a builder for [Operations].
83    ///
84    /// ```
85    /// # async fn sample() -> google_cloud_gax::client_builder::Result<()> {
86    /// # use google_cloud_longrunning::client::Operations;
87    /// let client = Operations::builder().build().await?;
88    /// # Ok(()) }
89    /// ```
90    pub fn builder() -> super::builder::operations::ClientBuilder {
91        crate::new_client_builder(super::builder::operations::client::Factory)
92    }
93
94    /// Creates a new client from the provided stub.
95    ///
96    /// The most common case for calling this function is in tests mocking the
97    /// client's behavior.
98    pub fn from_stub<T>(stub: T) -> Self
99    where
100        T: super::stub::Operations + 'static,
101    {
102        Self {
103            inner: std::sync::Arc::new(stub),
104        }
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}