gcloud_longrunning/autogen/
operations_client.rs

1use std::time::Duration;
2
3use tonic::Response;
4
5use google_cloud_gax::conn::{Channel, Error};
6use google_cloud_gax::create_request;
7use google_cloud_gax::grpc::{Code, Status};
8use google_cloud_gax::retry::{invoke, MapErr, RetrySetting};
9use google_cloud_googleapis::longrunning::operations_client::OperationsClient as InternalOperationsClient;
10use google_cloud_googleapis::longrunning::{
11    CancelOperationRequest, DeleteOperationRequest, GetOperationRequest, Operation, WaitOperationRequest,
12};
13
14pub fn default_retry_setting() -> RetrySetting {
15    RetrySetting {
16        from_millis: 50,
17        max_delay: Some(Duration::from_secs(10)),
18        factor: 1u64,
19        take: 20,
20        codes: vec![Code::Unavailable, Code::Unknown],
21    }
22}
23
24#[derive(Clone)]
25pub struct OperationsClient {
26    inner: InternalOperationsClient<Channel>,
27}
28
29impl OperationsClient {
30    pub async fn new(channel: Channel) -> Result<Self, Error> {
31        Ok(OperationsClient {
32            inner: InternalOperationsClient::new(channel).max_decoding_message_size(i32::MAX as usize),
33        })
34    }
35
36    /// GetOperation gets the latest state of a long-running operation.  Clients can use this
37    /// method to poll the operation result at intervals as recommended by the API service.
38    pub async fn get_operation(
39        &self,
40        req: GetOperationRequest,
41        retry: Option<RetrySetting>,
42    ) -> Result<Response<Operation>, Status> {
43        let setting = retry.unwrap_or_else(default_retry_setting);
44        let name = &req.name;
45        let action = || async {
46            let request = create_request(format!("name={name}"), req.clone());
47            self.inner.clone().get_operation(request).await.map_transient_err()
48        };
49        invoke(Some(setting), action).await
50    }
51
52    /// DeleteOperation deletes a long-running operation. This method indicates that the client is
53    /// no longer interested in the operation result. It does not cancel the
54    /// operation. If the server doesn’t support this method, it returns
55    /// google.rpc.Code.UNIMPLEMENTED.
56    pub async fn delete_operation(
57        &self,
58        req: DeleteOperationRequest,
59        retry: Option<RetrySetting>,
60    ) -> Result<Response<()>, Status> {
61        let setting = retry.unwrap_or_else(default_retry_setting);
62        let name = &req.name;
63        let action = || async {
64            let request = create_request(format!("name={name}"), req.clone());
65            self.inner.clone().delete_operation(request).await.map_transient_err()
66        };
67        invoke(Some(setting), action).await
68    }
69
70    /// CancelOperation starts asynchronous cancellation on a long-running operation.  The server
71    /// makes a best effort to cancel the operation, but success is not
72    /// guaranteed.  If the server doesn’t support this method, it returns
73    /// google.rpc.Code.UNIMPLEMENTED.  Clients can use
74    /// Operations.GetOperation or
75    /// other methods to check whether the cancellation succeeded or whether the
76    /// operation completed despite cancellation. On successful cancellation,
77    /// the operation is not deleted; instead, it becomes an operation with
78    /// an Operation.error value with a google.rpc.Status.code of 1,
79    /// corresponding to Code.CANCELLED.
80    pub async fn cancel_operation(
81        &self,
82        req: CancelOperationRequest,
83        retry: Option<RetrySetting>,
84    ) -> Result<Response<()>, Status> {
85        let setting = retry.unwrap_or_else(default_retry_setting);
86        let name = &req.name;
87        let action = || async {
88            let request = create_request(format!("name={name}"), req.clone());
89            self.inner.clone().cancel_operation(request).await.map_transient_err()
90        };
91        invoke(Some(setting), action).await
92    }
93
94    /// WaitOperation waits until the specified long-running operation is done or reaches at most
95    /// a specified timeout, returning the latest state.  If the operation is
96    /// already done, the latest state is immediately returned.  If the timeout
97    /// specified is greater than the default HTTP/RPC timeout, the HTTP/RPC
98    /// timeout is used.  If the server does not support this method, it returns
99    /// google.rpc.Code.UNIMPLEMENTED.
100    /// Note that this method is on a best-effort basis.  It may return the latest
101    /// state before the specified timeout (including immediately), meaning even an
102    /// immediate response is no guarantee that the operation is done.
103    pub async fn wait_operation(
104        &self,
105        req: WaitOperationRequest,
106        retry: Option<RetrySetting>,
107    ) -> Result<Response<Operation>, Status> {
108        let setting = retry.unwrap_or_else(default_retry_setting);
109        let action = || async {
110            let request = create_request("".to_string(), req.clone());
111            self.inner.clone().wait_operation(request).await.map_transient_err()
112        };
113        invoke(Some(setting), action).await
114    }
115}