aw_test/services/
teams.rs

1use crate::client::{Client, ParamType};
2use std::collections::HashMap;
3use crate::services::AppwriteException;
4use crate::models;
5use serde_json::json;
6use std::io::Read;
7
8#[derive(Clone)]
9pub struct Teams {
10  client: Client
11}
12
13impl Teams {  
14    pub fn new(client: &Client) -> Self {
15        Self {
16            client: client.clone()
17        }
18    }
19
20    /// Get a list of all the teams in which the current user is a member. You can
21    /// use the parameters to filter your results.
22    /// 
23    /// In admin mode, this endpoint returns a list of all the teams in the current
24    /// project. [Learn more about different API modes](/docs/admin).
25    pub fn list(&self, search: Option<&str>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_type: Option<&str>) -> Result<models::TeamList, AppwriteException> {
26        let path = "/teams";
27        let  headers: HashMap<String, String> = [
28            ("content-type".to_string(), "application/json".to_string()),
29        ].iter().cloned().collect();
30
31        let search:&str = match search {
32            Some(data) => data,
33            None => ""
34        };
35
36        let cursor:&str = match cursor {
37            Some(data) => data,
38            None => ""
39        };
40
41        let cursor_direction:&str = match cursor_direction {
42            Some(data) => data,
43            None => ""
44        };
45
46        let order_type:&str = match order_type {
47            Some(data) => data,
48            None => ""
49        };
50
51        let  params: HashMap<String, ParamType> = [
52            ("search".to_string(), ParamType::String(search.to_string())),
53            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
54            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
55            ("cursor".to_string(), ParamType::String(cursor.to_string())),
56            ("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
57            ("orderType".to_string(), ParamType::String(order_type.to_string())),
58        ].iter().cloned().collect();
59
60        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
61
62        let processedResponse:models::TeamList = match response {
63            Ok(r) => {
64                match r.json() {
65                    Ok(json) => json,
66                    Err(e) => {
67                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
68                    }
69                }
70            }
71            Err(e) => {
72                return Err(e);
73            }
74        };
75
76        Ok(processedResponse)
77    }
78
79    /// Create a new team. The user who creates the team will automatically be
80    /// assigned as the owner of the team. Only the users with the owner role can
81    /// invite new members, add new owners and delete or update the team.
82    pub fn create(&self, team_id: &str, name: &str, roles: Option<&[&str]>) -> Result<models::Team, AppwriteException> {
83        let path = "/teams";
84        let  headers: HashMap<String, String> = [
85            ("content-type".to_string(), "application/json".to_string()),
86        ].iter().cloned().collect();
87
88        let roles:&[&str] = match roles {
89            Some(data) => data,
90            None => &[]
91        };
92
93        let  params: HashMap<String, ParamType> = [
94            ("teamId".to_string(), ParamType::String(team_id.to_string())),
95            ("name".to_string(), ParamType::String(name.to_string())),
96            ("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
97        ].iter().cloned().collect();
98
99        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
100
101        let processedResponse:models::Team = match response {
102            Ok(r) => {
103                match r.json() {
104                    Ok(json) => json,
105                    Err(e) => {
106                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
107                    }
108                }
109            }
110            Err(e) => {
111                return Err(e);
112            }
113        };
114
115        Ok(processedResponse)
116    }
117
118    /// Get a team by its ID. All team members have read access for this resource.
119    pub fn get(&self, team_id: &str) -> Result<models::Team, AppwriteException> {
120        let path = "/teams/teamId".replace("teamId", &team_id);
121        let  headers: HashMap<String, String> = [
122            ("content-type".to_string(), "application/json".to_string()),
123        ].iter().cloned().collect();
124
125        let  params: HashMap<String, ParamType> = [
126        ].iter().cloned().collect();
127
128        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
129
130        let processedResponse:models::Team = match response {
131            Ok(r) => {
132                match r.json() {
133                    Ok(json) => json,
134                    Err(e) => {
135                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
136                    }
137                }
138            }
139            Err(e) => {
140                return Err(e);
141            }
142        };
143
144        Ok(processedResponse)
145    }
146
147    /// Update a team using its ID. Only members with the owner role can update the
148    /// team.
149    pub fn update(&self, team_id: &str, name: &str) -> Result<models::Team, AppwriteException> {
150        let path = "/teams/teamId".replace("teamId", &team_id);
151        let  headers: HashMap<String, String> = [
152            ("content-type".to_string(), "application/json".to_string()),
153        ].iter().cloned().collect();
154
155        let  params: HashMap<String, ParamType> = [
156            ("name".to_string(), ParamType::String(name.to_string())),
157        ].iter().cloned().collect();
158
159        let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
160
161        let processedResponse:models::Team = match response {
162            Ok(r) => {
163                match r.json() {
164                    Ok(json) => json,
165                    Err(e) => {
166                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
167                    }
168                }
169            }
170            Err(e) => {
171                return Err(e);
172            }
173        };
174
175        Ok(processedResponse)
176    }
177
178    /// Delete a team using its ID. Only team members with the owner role can
179    /// delete the team.
180    pub fn delete(&self, team_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
181        let path = "/teams/teamId".replace("teamId", &team_id);
182        let  headers: HashMap<String, String> = [
183            ("content-type".to_string(), "application/json".to_string()),
184        ].iter().cloned().collect();
185
186        let  params: HashMap<String, ParamType> = [
187        ].iter().cloned().collect();
188
189        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
190
191        match response {
192            Ok(r) => {
193                let status_code = r.status();
194                if status_code == reqwest::StatusCode::NO_CONTENT {
195                    Ok(json!(true))
196                } else {
197                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
198                }
199            }
200            Err(e) => {
201                Err(e)
202            }
203        }
204    }
205
206    /// Use this endpoint to list a team's members using the team's ID. All team
207    /// members have read access to this endpoint.
208    pub fn get_memberships(&self, team_id: &str, search: Option<&str>, limit: Option<i64>, offset: Option<i64>, cursor: Option<&str>, cursor_direction: Option<&str>, order_type: Option<&str>) -> Result<models::MembershipList, AppwriteException> {
209        let path = "/teams/teamId/memberships".replace("teamId", &team_id);
210        let  headers: HashMap<String, String> = [
211            ("content-type".to_string(), "application/json".to_string()),
212        ].iter().cloned().collect();
213
214        let search:&str = match search {
215            Some(data) => data,
216            None => ""
217        };
218
219        let cursor:&str = match cursor {
220            Some(data) => data,
221            None => ""
222        };
223
224        let cursor_direction:&str = match cursor_direction {
225            Some(data) => data,
226            None => ""
227        };
228
229        let order_type:&str = match order_type {
230            Some(data) => data,
231            None => ""
232        };
233
234        let  params: HashMap<String, ParamType> = [
235            ("search".to_string(), ParamType::String(search.to_string())),
236            ("limit".to_string(),  ParamType::OptionalNumber(limit)),
237            ("offset".to_string(),  ParamType::OptionalNumber(offset)),
238            ("cursor".to_string(), ParamType::String(cursor.to_string())),
239            ("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
240            ("orderType".to_string(), ParamType::String(order_type.to_string())),
241        ].iter().cloned().collect();
242
243        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
244
245        let processedResponse:models::MembershipList = match response {
246            Ok(r) => {
247                match r.json() {
248                    Ok(json) => json,
249                    Err(e) => {
250                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
251                    }
252                }
253            }
254            Err(e) => {
255                return Err(e);
256            }
257        };
258
259        Ok(processedResponse)
260    }
261
262    /// Invite a new member to join your team. If initiated from the client SDK, an
263    /// email with a link to join the team will be sent to the member's email
264    /// address and an account will be created for them should they not be signed
265    /// up already. If initiated from server-side SDKs, the new member will
266    /// automatically be added to the team.
267    /// 
268    /// Use the 'url' parameter to redirect the user from the invitation email back
269    /// to your app. When the user is redirected, use the [Update Team Membership
270    /// Status](/docs/client/teams#teamsUpdateMembershipStatus) endpoint to allow
271    /// the user to accept the invitation to the team. 
272    /// 
273    /// Please note that to avoid a [Redirect
274    /// Attack](https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Unvalidated_Redirects_and_Forwards_Cheat_Sheet.md)
275    /// the only valid redirect URL's are the once from domains you have set when
276    /// adding your platforms in the console interface.
277    pub fn create_membership(&self, team_id: &str, email: &str, roles: &[&str], url: &str, name: Option<&str>) -> Result<models::Membership, AppwriteException> {
278        let path = "/teams/teamId/memberships".replace("teamId", &team_id);
279        let  headers: HashMap<String, String> = [
280            ("content-type".to_string(), "application/json".to_string()),
281        ].iter().cloned().collect();
282
283        let name:&str = match name {
284            Some(data) => data,
285            None => ""
286        };
287
288        let  params: HashMap<String, ParamType> = [
289            ("email".to_string(), ParamType::String(email.to_string())),
290            ("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
291            ("url".to_string(), ParamType::String(url.to_string())),
292            ("name".to_string(), ParamType::String(name.to_string())),
293        ].iter().cloned().collect();
294
295        let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
296
297        let processedResponse:models::Membership = match response {
298            Ok(r) => {
299                match r.json() {
300                    Ok(json) => json,
301                    Err(e) => {
302                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
303                    }
304                }
305            }
306            Err(e) => {
307                return Err(e);
308            }
309        };
310
311        Ok(processedResponse)
312    }
313
314    /// Get a team member by the membership unique id. All team members have read
315    /// access for this resource.
316    pub fn get_membership(&self, team_id: &str, membership_id: &str) -> Result<models::MembershipList, AppwriteException> {
317        let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
318        let  headers: HashMap<String, String> = [
319            ("content-type".to_string(), "application/json".to_string()),
320        ].iter().cloned().collect();
321
322        let  params: HashMap<String, ParamType> = [
323        ].iter().cloned().collect();
324
325        let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
326
327        let processedResponse:models::MembershipList = match response {
328            Ok(r) => {
329                match r.json() {
330                    Ok(json) => json,
331                    Err(e) => {
332                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
333                    }
334                }
335            }
336            Err(e) => {
337                return Err(e);
338            }
339        };
340
341        Ok(processedResponse)
342    }
343
344    /// Modify the roles of a team member. Only team members with the owner role
345    /// have access to this endpoint. Learn more about [roles and
346    /// permissions](/docs/permissions).
347    pub fn update_membership_roles(&self, team_id: &str, membership_id: &str, roles: &[&str]) -> Result<models::Membership, AppwriteException> {
348        let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
349        let  headers: HashMap<String, String> = [
350            ("content-type".to_string(), "application/json".to_string()),
351        ].iter().cloned().collect();
352
353        let  params: HashMap<String, ParamType> = [
354            ("roles".to_string(), ParamType::Array(roles.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
355        ].iter().cloned().collect();
356
357        let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
358
359        let processedResponse:models::Membership = match response {
360            Ok(r) => {
361                match r.json() {
362                    Ok(json) => json,
363                    Err(e) => {
364                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
365                    }
366                }
367            }
368            Err(e) => {
369                return Err(e);
370            }
371        };
372
373        Ok(processedResponse)
374    }
375
376    /// This endpoint allows a user to leave a team or for a team owner to delete
377    /// the membership of any other team member. You can also use this endpoint to
378    /// delete a user membership even if it is not accepted.
379    pub fn delete_membership(&self, team_id: &str, membership_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
380        let path = "/teams/teamId/memberships/membershipId".replace("teamId", &team_id).replace("membershipId", &membership_id);
381        let  headers: HashMap<String, String> = [
382            ("content-type".to_string(), "application/json".to_string()),
383        ].iter().cloned().collect();
384
385        let  params: HashMap<String, ParamType> = [
386        ].iter().cloned().collect();
387
388        let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
389
390        match response {
391            Ok(r) => {
392                let status_code = r.status();
393                if status_code == reqwest::StatusCode::NO_CONTENT {
394                    Ok(json!(true))
395                } else {
396                    Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
397                }
398            }
399            Err(e) => {
400                Err(e)
401            }
402        }
403    }
404
405    /// Use this endpoint to allow a user to accept an invitation to join a team
406    /// after being redirected back to your app from the invitation email received
407    /// by the user.
408    /// 
409    /// If the request is successful, a session for the user is automatically
410    /// created.
411    /// 
412    pub fn update_membership_status(&self, team_id: &str, membership_id: &str, user_id: &str, secret: &str) -> Result<models::Membership, AppwriteException> {
413        let path = "/teams/teamId/memberships/membershipId/status".replace("teamId", &team_id).replace("membershipId", &membership_id);
414        let  headers: HashMap<String, String> = [
415            ("content-type".to_string(), "application/json".to_string()),
416        ].iter().cloned().collect();
417
418        let  params: HashMap<String, ParamType> = [
419            ("userId".to_string(), ParamType::String(user_id.to_string())),
420            ("secret".to_string(), ParamType::String(secret.to_string())),
421        ].iter().cloned().collect();
422
423        let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
424
425        let processedResponse:models::Membership = match response {
426            Ok(r) => {
427                match r.json() {
428                    Ok(json) => json,
429                    Err(e) => {
430                        return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
431                    }
432                }
433            }
434            Err(e) => {
435                return Err(e);
436            }
437        };
438
439        Ok(processedResponse)
440    }
441}