dfns_sdk_rs/api/policies/
client.rs

1// @dfns-sdk-rs/src/api/policies/client.rs
2
3use super::types::*;
4use crate::{
5    error::DfnsError,
6    models::generic::DfnsApiClientOptions,
7    utils::{fetch::simple_fetch, url::build_path_and_query, user_action_fetch::user_action_fetch},
8};
9use std::collections::HashMap;
10
11pub struct PoliciesClient {
12    api_options: DfnsApiClientOptions,
13}
14
15impl PoliciesClient {
16    pub fn new(api_options: DfnsApiClientOptions) -> Self {
17        Self { api_options }
18    }
19
20    pub async fn archive_policy(
21        &self,
22        request: ArchivePolicyRequest,
23    ) -> Result<ArchivePolicyResponse, DfnsError> {
24        let path = build_path_and_query(
25            "/v2/policies/:policy_id",
26            &crate::utils::url::PathAndQueryParams {
27                path: {
28                    let mut map = HashMap::new();
29                    map.insert("policy_id".to_string(), request.policy_id);
30                    map
31                },
32                query: HashMap::new(),
33            },
34        );
35
36        user_action_fetch(
37            &path,
38            crate::utils::fetch::FetchOptions {
39                method: crate::utils::fetch::HttpMethod::DELETE,
40                headers: None,
41                body: Some(serde_json::json!({})),
42                api_options: self.api_options.clone(),
43            },
44        )
45        .await
46    }
47
48    pub async fn create_approval_decision(
49        &self,
50        request: CreateApprovalDecisionRequest,
51    ) -> Result<CreateApprovalDecisionResponse, DfnsError> {
52        let path = build_path_and_query(
53            "/v2/policy-approvals/:approval_id/decisions",
54            &crate::utils::url::PathAndQueryParams {
55                path: {
56                    let mut map = HashMap::new();
57                    map.insert("approval_id".to_string(), request.approval_id);
58                    map
59                },
60                query: HashMap::new(),
61            },
62        );
63
64        user_action_fetch(
65            &path,
66            crate::utils::fetch::FetchOptions {
67                method: crate::utils::fetch::HttpMethod::POST,
68                headers: None,
69                body: Some(serde_json::to_value(&request.body)?),
70                api_options: self.api_options.clone(),
71            },
72        )
73        .await
74    }
75
76    pub async fn create_policy(
77        &self,
78        request: CreatePolicyRequest,
79    ) -> Result<CreatePolicyResponse, DfnsError> {
80        let path = build_path_and_query(
81            "/v2/policies",
82            &crate::utils::url::PathAndQueryParams {
83                path: HashMap::new(),
84                query: HashMap::new(),
85            },
86        );
87
88        user_action_fetch(
89            &path,
90            crate::utils::fetch::FetchOptions {
91                method: crate::utils::fetch::HttpMethod::POST,
92                headers: None,
93                body: Some(serde_json::to_value(&request.body)?),
94                api_options: self.api_options.clone(),
95            },
96        )
97        .await
98    }
99
100    pub async fn get_approval(
101        &self,
102        request: GetApprovalRequest,
103    ) -> Result<GetApprovalResponse, DfnsError> {
104        let path = build_path_and_query(
105            "/v2/policy-approvals/:approval_id",
106            &crate::utils::url::PathAndQueryParams {
107                path: {
108                    let mut map = HashMap::new();
109                    map.insert("approval_id".to_string(), request.approval_id);
110                    map
111                },
112                query: HashMap::new(),
113            },
114        );
115
116        simple_fetch(
117            &path,
118            crate::utils::fetch::FetchOptions {
119                method: crate::utils::fetch::HttpMethod::GET,
120                headers: None,
121                body: None,
122                api_options: self.api_options.base.clone(),
123            },
124        )
125        .await
126    }
127
128    pub async fn get_policy(
129        &self,
130        request: GetPolicyRequest,
131    ) -> Result<GetPolicyResponse, DfnsError> {
132        let path = build_path_and_query(
133            "/v2/policies/:policy_id",
134            &crate::utils::url::PathAndQueryParams {
135                path: {
136                    let mut map = HashMap::new();
137                    map.insert("policy_id".to_string(), request.policy_id);
138                    map
139                },
140                query: HashMap::new(),
141            },
142        );
143
144        simple_fetch(
145            &path,
146            crate::utils::fetch::FetchOptions {
147                method: crate::utils::fetch::HttpMethod::GET,
148                headers: None,
149                body: None,
150                api_options: self.api_options.base.clone(),
151            },
152        )
153        .await
154    }
155
156    pub async fn list_approvals(
157        &self,
158        request: Option<ListApprovalsRequest>,
159    ) -> Result<ListApprovalsResponse, DfnsError> {
160        let path = build_path_and_query(
161            "/v2/policy-approvals",
162            &crate::utils::url::PathAndQueryParams {
163                path: HashMap::new(),
164                query: request
165                    .and_then(|r| r.query)
166                    .map(|q| {
167                        let mut map = HashMap::new();
168                        if let Some(pagination_token) = q.pagination_token {
169                            map.insert("pageToken".to_string(), pagination_token);
170                        }
171                        if let Some(approver_id) = q.approver_id {
172                            map.insert("approverId".to_string(), approver_id);
173                        }
174                        if let Some(initiator_id) = q.initiator_id {
175                            map.insert("initiatorId".to_string(), initiator_id);
176                        }
177                        if let Some(limit) = q.limit {
178                            map.insert("limit".to_string(), limit.to_string());
179                        }
180                        if let Some(status) = q.status {
181                            map.insert("status".to_string(), format!("{:?}", status));
182                        }
183                        map
184                    })
185                    .unwrap_or_default(),
186            },
187        );
188
189        simple_fetch(
190            &path,
191            crate::utils::fetch::FetchOptions {
192                method: crate::utils::fetch::HttpMethod::GET,
193                headers: None,
194                body: None,
195                api_options: self.api_options.base.clone(),
196            },
197        )
198        .await
199    }
200
201    pub async fn list_policies(
202        &self,
203        request: Option<ListPoliciesRequest>,
204    ) -> Result<ListPoliciesResponse, DfnsError> {
205        let path = build_path_and_query(
206            "/v2/policies",
207            &crate::utils::url::PathAndQueryParams {
208                path: HashMap::new(),
209                query: request
210                    .and_then(|r| r.query)
211                    .map(|q| {
212                        let mut map = HashMap::new();
213                        if let Some(pagination_token) = q.pagination_token {
214                            map.insert("pageToken".to_string(), pagination_token);
215                        }
216                        if let Some(limit) = q.limit {
217                            map.insert("limit".to_string(), limit.to_string());
218                        }
219                        if let Some(status) = q.status {
220                            map.insert("status".to_string(), format!("{:?}", status));
221                        }
222                        map
223                    })
224                    .unwrap_or_default(),
225            },
226        );
227
228        simple_fetch(
229            &path,
230            crate::utils::fetch::FetchOptions {
231                method: crate::utils::fetch::HttpMethod::GET,
232                headers: None,
233                body: None,
234                api_options: self.api_options.base.clone(),
235            },
236        )
237        .await
238    }
239
240    pub async fn update_policy(
241        &self,
242        request: UpdatePolicyRequest,
243    ) -> Result<UpdatePolicyResponse, DfnsError> {
244        let path = build_path_and_query(
245            "/v2/policies/:policy_id",
246            &crate::utils::url::PathAndQueryParams {
247                path: {
248                    let mut map = HashMap::new();
249                    map.insert("policy_id".to_string(), request.policy_id);
250                    map
251                },
252                query: HashMap::new(),
253            },
254        );
255
256        user_action_fetch(
257            &path,
258            crate::utils::fetch::FetchOptions {
259                method: crate::utils::fetch::HttpMethod::PUT,
260                headers: None,
261                body: Some(serde_json::to_value(&request.body)?),
262                api_options: self.api_options.clone(),
263            },
264        )
265        .await
266    }
267}