dfns_sdk_rs/api/permissions/
client.rs

1// @dfns-sdk-rs/src/api/permissions/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 PermissionsClient {
12    api_options: DfnsApiClientOptions,
13}
14
15impl PermissionsClient {
16    pub fn new(api_options: DfnsApiClientOptions) -> Self {
17        Self { api_options }
18    }
19
20    pub async fn archive_permission(
21        &self,
22        request: ArchivePermissionRequest,
23    ) -> Result<ArchivePermissionResponse, DfnsError> {
24        let path = build_path_and_query(
25            "/permissions/:permission_id/archive",
26            &crate::utils::url::PathAndQueryParams {
27                path: {
28                    let mut map = HashMap::new();
29                    map.insert("permission_id".to_string(), request.permission_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::PUT,
40                headers: None,
41                body: Some(serde_json::to_value(&request.body)?),
42                api_options: self.api_options.clone(),
43            },
44        )
45        .await
46    }
47
48    pub async fn create_assignment(
49        &self,
50        request: CreateAssignmentRequest,
51    ) -> Result<CreateAssignmentResponse, DfnsError> {
52        let path = build_path_and_query(
53            "/permissions/:permission_id/assignments",
54            &crate::utils::url::PathAndQueryParams {
55                path: {
56                    let mut map = HashMap::new();
57                    map.insert("permission_id".to_string(), request.permission_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_permission(
77        &self,
78        request: CreatePermissionRequest,
79    ) -> Result<CreatePermissionResponse, DfnsError> {
80        let path = build_path_and_query(
81            "/permissions",
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 delete_assignment(
101        &self,
102        request: DeleteAssignmentRequest,
103    ) -> Result<DeleteAssignmentResponse, DfnsError> {
104        let path = build_path_and_query(
105            "/permissions/:permission_id/assignments/:assignment_id",
106            &crate::utils::url::PathAndQueryParams {
107                path: {
108                    let mut map = HashMap::new();
109                    map.insert("permission_id".to_string(), request.permission_id);
110                    map.insert("assignment_id".to_string(), request.assignment_id);
111                    map
112                },
113                query: HashMap::new(),
114            },
115        );
116
117        user_action_fetch(
118            &path,
119            crate::utils::fetch::FetchOptions {
120                method: crate::utils::fetch::HttpMethod::DELETE,
121                headers: None,
122                body: Some(serde_json::json!({})),
123                api_options: self.api_options.clone(),
124            },
125        )
126        .await
127    }
128
129    pub async fn get_permission(
130        &self,
131        request: GetPermissionRequest,
132    ) -> Result<GetPermissionResponse, DfnsError> {
133        let path = build_path_and_query(
134            "/permissions/:permission_id",
135            &crate::utils::url::PathAndQueryParams {
136                path: {
137                    let mut map = HashMap::new();
138                    map.insert("permission_id".to_string(), request.permission_id);
139                    map
140                },
141                query: HashMap::new(),
142            },
143        );
144
145        simple_fetch(
146            &path,
147            crate::utils::fetch::FetchOptions {
148                method: crate::utils::fetch::HttpMethod::GET,
149                headers: None,
150                body: None,
151                api_options: self.api_options.base.clone(),
152            },
153        )
154        .await
155    }
156
157    pub async fn list_assignments(
158        &self,
159        request: ListAssignmentsRequest,
160    ) -> Result<ListAssignmentsResponse, DfnsError> {
161        let path = build_path_and_query(
162            "/permissions/:permission_id/assignments",
163            &crate::utils::url::PathAndQueryParams {
164                path: {
165                    let mut map = HashMap::new();
166                    map.insert("permission_id".to_string(), request.permission_id);
167                    map
168                },
169                query: HashMap::new(),
170            },
171        );
172
173        simple_fetch(
174            &path,
175            crate::utils::fetch::FetchOptions {
176                method: crate::utils::fetch::HttpMethod::GET,
177                headers: None,
178                body: None,
179                api_options: self.api_options.base.clone(),
180            },
181        )
182        .await
183    }
184
185    pub async fn list_permissions(
186        &self,
187        request: Option<ListPermissionsRequest>,
188    ) -> Result<ListPermissionsResponse, DfnsError> {
189        let path = build_path_and_query(
190            "/permissions",
191            &crate::utils::url::PathAndQueryParams {
192                path: HashMap::new(),
193                query: request
194                    .and_then(|r| r.query)
195                    .map(|q| {
196                        let mut map = HashMap::new();
197                        if let Some(pagination_token) = q.pagination_token {
198                            map.insert("pageToken".to_string(), pagination_token);
199                        }
200                        map
201                    })
202                    .unwrap_or_default(),
203            },
204        );
205
206        simple_fetch(
207            &path,
208            crate::utils::fetch::FetchOptions {
209                method: crate::utils::fetch::HttpMethod::GET,
210                headers: None,
211                body: None,
212                api_options: self.api_options.base.clone(),
213            },
214        )
215        .await
216    }
217
218    pub async fn update_permission(
219        &self,
220        request: UpdatePermissionRequest,
221    ) -> Result<UpdatePermissionResponse, DfnsError> {
222        let path = build_path_and_query(
223            "/permissions/:permission_id",
224            &crate::utils::url::PathAndQueryParams {
225                path: {
226                    let mut map = HashMap::new();
227                    map.insert("permission_id".to_string(), request.permission_id);
228                    map
229                },
230                query: HashMap::new(),
231            },
232        );
233
234        user_action_fetch(
235            &path,
236            crate::utils::fetch::FetchOptions {
237                method: crate::utils::fetch::HttpMethod::PUT,
238                headers: None,
239                body: Some(serde_json::to_value(&request.body)?),
240                api_options: self.api_options.clone(),
241            },
242        )
243        .await
244    }
245}