Skip to main content

ironflow_sdk/
delegation_methods.rs

1//! Approval-delegation methods for [`IronflowClient`].
2
3use uuid::Uuid;
4
5use ironflow_types::ApiResponse;
6
7use crate::client::{IronflowClient, ListApprovalDelegationsFilter};
8use crate::error::Error;
9use crate::types;
10
11impl IronflowClient {
12    /// List the first page of the active approval delegations visible to the
13    /// caller.
14    ///
15    /// # Errors
16    ///
17    /// Returns an error if the HTTP request fails or the response cannot be deserialized.
18    ///
19    /// # Examples
20    ///
21    /// ```no_run
22    /// use ironflow_sdk::IronflowClient;
23    ///
24    /// # async fn example() -> Result<(), ironflow_sdk::Error> {
25    /// let client = IronflowClient::new("https://ironflow.example.com", "key");
26    /// let delegations = client.list_approval_delegations().await?;
27    /// # Ok(())
28    /// # }
29    /// ```
30    pub async fn list_approval_delegations(
31        &self,
32    ) -> Result<ApiResponse<Vec<types::ApprovalDelegationResponse>>, Error> {
33        self.list_approval_delegations_filtered(&ListApprovalDelegationsFilter::default())
34            .await
35    }
36
37    /// List the active approval delegations visible to the caller, with
38    /// filters and pagination.
39    ///
40    /// # Errors
41    ///
42    /// Returns an error if the HTTP request fails or the response cannot be deserialized.
43    ///
44    /// # Examples
45    ///
46    /// ```no_run
47    /// use ironflow_sdk::IronflowClient;
48    /// use ironflow_sdk::client::ListApprovalDelegationsFilter;
49    ///
50    /// # async fn example() -> Result<(), ironflow_sdk::Error> {
51    /// let client = IronflowClient::new("https://ironflow.example.com", "key");
52    /// let filter = ListApprovalDelegationsFilter {
53    ///     page: Some(2),
54    ///     ..Default::default()
55    /// };
56    /// let delegations = client.list_approval_delegations_filtered(&filter).await?;
57    /// # Ok(())
58    /// # }
59    /// ```
60    pub async fn list_approval_delegations_filtered(
61        &self,
62        filter: &ListApprovalDelegationsFilter,
63    ) -> Result<ApiResponse<Vec<types::ApprovalDelegationResponse>>, Error> {
64        self.send_envelope(self.get("/api/v1/approval-delegations").query(filter))
65            .await
66    }
67
68    /// Delegate the caller's approval power to another user.
69    ///
70    /// # Errors
71    ///
72    /// Returns an error if the HTTP request fails or the response cannot be deserialized.
73    ///
74    /// # Examples
75    ///
76    /// ```no_run
77    /// use chrono::{TimeDelta, Utc};
78    /// use ironflow_sdk::IronflowClient;
79    /// use ironflow_sdk::types::CreateApprovalDelegationRequest;
80    /// use uuid::Uuid;
81    ///
82    /// # async fn example(bob: Uuid) -> Result<(), ironflow_sdk::Error> {
83    /// let client = IronflowClient::new("https://ironflow.example.com", "key");
84    /// let delegation = client.create_approval_delegation(&CreateApprovalDelegationRequest {
85    ///     to_user_id: bob,
86    ///     valid_from: None,
87    ///     valid_until: Utc::now() + TimeDelta::days(7),
88    ///     workflow_filter: Some("deploy-*".to_string()),
89    /// }).await?;
90    /// # Ok(())
91    /// # }
92    /// ```
93    pub async fn create_approval_delegation(
94        &self,
95        request: &types::CreateApprovalDelegationRequest,
96    ) -> Result<ApiResponse<types::ApprovalDelegationResponse>, Error> {
97        self.send_envelope(self.post("/api/v1/approval-delegations").json(request))
98            .await
99    }
100
101    /// Revoke an approval delegation by ID.
102    ///
103    /// # Errors
104    ///
105    /// Returns an error if the HTTP request fails.
106    ///
107    /// # Examples
108    ///
109    /// ```no_run
110    /// use ironflow_sdk::IronflowClient;
111    /// use uuid::Uuid;
112    ///
113    /// # async fn example() -> Result<(), ironflow_sdk::Error> {
114    /// let client = IronflowClient::new("https://ironflow.example.com", "key");
115    /// client.delete_approval_delegation(Uuid::nil()).await?;
116    /// # Ok(())
117    /// # }
118    /// ```
119    pub async fn delete_approval_delegation(&self, id: Uuid) -> Result<(), Error> {
120        self.send_no_content(self.delete(&format!("/api/v1/approval-delegations/{id}")))
121            .await
122    }
123}