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 Users {
10 client: Client
11}
12
13impl Users {
14 pub fn new(client: &Client) -> Self {
15 Self {
16 client: client.clone()
17 }
18 }
19
20 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::UserList, AppwriteException> {
23 let path = "/users";
24 let headers: HashMap<String, String> = [
25 ("content-type".to_string(), "application/json".to_string()),
26 ].iter().cloned().collect();
27
28 let search:&str = match search {
29 Some(data) => data,
30 None => ""
31 };
32
33 let cursor:&str = match cursor {
34 Some(data) => data,
35 None => ""
36 };
37
38 let cursor_direction:&str = match cursor_direction {
39 Some(data) => data,
40 None => ""
41 };
42
43 let order_type:&str = match order_type {
44 Some(data) => data,
45 None => ""
46 };
47
48 let params: HashMap<String, ParamType> = [
49 ("search".to_string(), ParamType::String(search.to_string())),
50 ("limit".to_string(), ParamType::OptionalNumber(limit)),
51 ("offset".to_string(), ParamType::OptionalNumber(offset)),
52 ("cursor".to_string(), ParamType::String(cursor.to_string())),
53 ("cursorDirection".to_string(), ParamType::String(cursor_direction.to_string())),
54 ("orderType".to_string(), ParamType::String(order_type.to_string())),
55 ].iter().cloned().collect();
56
57 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
58
59 let processedResponse:models::UserList = match response {
60 Ok(r) => {
61 match r.json() {
62 Ok(json) => json,
63 Err(e) => {
64 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
65 }
66 }
67 }
68 Err(e) => {
69 return Err(e);
70 }
71 };
72
73 Ok(processedResponse)
74 }
75
76 pub fn create(&self, user_id: &str, email: &str, password: &str, name: Option<&str>) -> Result<models::User, AppwriteException> {
78 let path = "/users";
79 let headers: HashMap<String, String> = [
80 ("content-type".to_string(), "application/json".to_string()),
81 ].iter().cloned().collect();
82
83 let name:&str = match name {
84 Some(data) => data,
85 None => ""
86 };
87
88 let params: HashMap<String, ParamType> = [
89 ("userId".to_string(), ParamType::String(user_id.to_string())),
90 ("email".to_string(), ParamType::String(email.to_string())),
91 ("password".to_string(), ParamType::String(password.to_string())),
92 ("name".to_string(), ParamType::String(name.to_string())),
93 ].iter().cloned().collect();
94
95 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
96
97 let processedResponse:models::User = match response {
98 Ok(r) => {
99 match r.json() {
100 Ok(json) => json,
101 Err(e) => {
102 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
103 }
104 }
105 }
106 Err(e) => {
107 return Err(e);
108 }
109 };
110
111 Ok(processedResponse)
112 }
113
114 pub fn get_usage(&self, range: Option<&str>, provider: Option<&str>) -> Result<models::UsageUsers, AppwriteException> {
115 let path = "/users/usage";
116 let headers: HashMap<String, String> = [
117 ("content-type".to_string(), "application/json".to_string()),
118 ].iter().cloned().collect();
119
120 let range:&str = match range {
121 Some(data) => data,
122 None => ""
123 };
124
125 let provider:&str = match provider {
126 Some(data) => data,
127 None => ""
128 };
129
130 let params: HashMap<String, ParamType> = [
131 ("range".to_string(), ParamType::String(range.to_string())),
132 ("provider".to_string(), ParamType::String(provider.to_string())),
133 ].iter().cloned().collect();
134
135 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
136
137 let processedResponse:models::UsageUsers = match response {
138 Ok(r) => {
139 match r.json() {
140 Ok(json) => json,
141 Err(e) => {
142 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
143 }
144 }
145 }
146 Err(e) => {
147 return Err(e);
148 }
149 };
150
151 Ok(processedResponse)
152 }
153
154 pub fn get(&self, user_id: &str) -> Result<models::User, AppwriteException> {
156 let path = "/users/userId".replace("userId", &user_id);
157 let headers: HashMap<String, String> = [
158 ("content-type".to_string(), "application/json".to_string()),
159 ].iter().cloned().collect();
160
161 let params: HashMap<String, ParamType> = [
162 ].iter().cloned().collect();
163
164 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
165
166 let processedResponse:models::User = match response {
167 Ok(r) => {
168 match r.json() {
169 Ok(json) => json,
170 Err(e) => {
171 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
172 }
173 }
174 }
175 Err(e) => {
176 return Err(e);
177 }
178 };
179
180 Ok(processedResponse)
181 }
182
183 pub fn delete(&self, user_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
185 let path = "/users/userId".replace("userId", &user_id);
186 let headers: HashMap<String, String> = [
187 ("content-type".to_string(), "application/json".to_string()),
188 ].iter().cloned().collect();
189
190 let params: HashMap<String, ParamType> = [
191 ].iter().cloned().collect();
192
193 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
194
195 match response {
196 Ok(r) => {
197 let status_code = r.status();
198 if status_code == reqwest::StatusCode::NO_CONTENT {
199 Ok(json!(true))
200 } else {
201 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
202 }
203 }
204 Err(e) => {
205 Err(e)
206 }
207 }
208 }
209
210 pub fn update_email(&self, user_id: &str, email: &str) -> Result<models::User, AppwriteException> {
212 let path = "/users/userId/email".replace("userId", &user_id);
213 let headers: HashMap<String, String> = [
214 ("content-type".to_string(), "application/json".to_string()),
215 ].iter().cloned().collect();
216
217 let params: HashMap<String, ParamType> = [
218 ("email".to_string(), ParamType::String(email.to_string())),
219 ].iter().cloned().collect();
220
221 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
222
223 let processedResponse:models::User = match response {
224 Ok(r) => {
225 match r.json() {
226 Ok(json) => json,
227 Err(e) => {
228 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
229 }
230 }
231 }
232 Err(e) => {
233 return Err(e);
234 }
235 };
236
237 Ok(processedResponse)
238 }
239
240 pub fn get_logs(&self, user_id: &str, limit: Option<i64>, offset: Option<i64>) -> Result<models::LogList, AppwriteException> {
242 let path = "/users/userId/logs".replace("userId", &user_id);
243 let headers: HashMap<String, String> = [
244 ("content-type".to_string(), "application/json".to_string()),
245 ].iter().cloned().collect();
246
247 let params: HashMap<String, ParamType> = [
248 ("limit".to_string(), ParamType::OptionalNumber(limit)),
249 ("offset".to_string(), ParamType::OptionalNumber(offset)),
250 ].iter().cloned().collect();
251
252 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
253
254 let processedResponse:models::LogList = match response {
255 Ok(r) => {
256 match r.json() {
257 Ok(json) => json,
258 Err(e) => {
259 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
260 }
261 }
262 }
263 Err(e) => {
264 return Err(e);
265 }
266 };
267
268 Ok(processedResponse)
269 }
270
271 pub fn update_name(&self, user_id: &str, name: &str) -> Result<models::User, AppwriteException> {
273 let path = "/users/userId/name".replace("userId", &user_id);
274 let headers: HashMap<String, String> = [
275 ("content-type".to_string(), "application/json".to_string()),
276 ].iter().cloned().collect();
277
278 let params: HashMap<String, ParamType> = [
279 ("name".to_string(), ParamType::String(name.to_string())),
280 ].iter().cloned().collect();
281
282 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
283
284 let processedResponse:models::User = match response {
285 Ok(r) => {
286 match r.json() {
287 Ok(json) => json,
288 Err(e) => {
289 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
290 }
291 }
292 }
293 Err(e) => {
294 return Err(e);
295 }
296 };
297
298 Ok(processedResponse)
299 }
300
301 pub fn update_password(&self, user_id: &str, password: &str) -> Result<models::User, AppwriteException> {
303 let path = "/users/userId/password".replace("userId", &user_id);
304 let headers: HashMap<String, String> = [
305 ("content-type".to_string(), "application/json".to_string()),
306 ].iter().cloned().collect();
307
308 let params: HashMap<String, ParamType> = [
309 ("password".to_string(), ParamType::String(password.to_string())),
310 ].iter().cloned().collect();
311
312 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
313
314 let processedResponse:models::User = match response {
315 Ok(r) => {
316 match r.json() {
317 Ok(json) => json,
318 Err(e) => {
319 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
320 }
321 }
322 }
323 Err(e) => {
324 return Err(e);
325 }
326 };
327
328 Ok(processedResponse)
329 }
330
331 pub fn get_prefs(&self, user_id: &str) -> Result<models::Preferences, AppwriteException> {
333 let path = "/users/userId/prefs".replace("userId", &user_id);
334 let headers: HashMap<String, String> = [
335 ("content-type".to_string(), "application/json".to_string()),
336 ].iter().cloned().collect();
337
338 let params: HashMap<String, ParamType> = [
339 ].iter().cloned().collect();
340
341 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
342
343 let processedResponse:models::Preferences = match response {
344 Ok(r) => {
345 match r.json() {
346 Ok(json) => json,
347 Err(e) => {
348 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
349 }
350 }
351 }
352 Err(e) => {
353 return Err(e);
354 }
355 };
356
357 Ok(processedResponse)
358 }
359
360 pub fn update_prefs(&self, user_id: &str, prefs: Option<HashMap<String, crate::client::ParamType>>) -> Result<models::Preferences, AppwriteException> {
364 let path = "/users/userId/prefs".replace("userId", &user_id);
365 let headers: HashMap<String, String> = [
366 ("content-type".to_string(), "application/json".to_string()),
367 ].iter().cloned().collect();
368
369 let params: HashMap<String, ParamType> = [
370 ("prefs".to_string(), ParamType::Object(prefs.unwrap())),
371 ].iter().cloned().collect();
372
373 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
374
375 let processedResponse:models::Preferences = match response {
376 Ok(r) => {
377 match r.json() {
378 Ok(json) => json,
379 Err(e) => {
380 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
381 }
382 }
383 }
384 Err(e) => {
385 return Err(e);
386 }
387 };
388
389 Ok(processedResponse)
390 }
391
392 pub fn get_sessions(&self, user_id: &str) -> Result<models::SessionList, AppwriteException> {
394 let path = "/users/userId/sessions".replace("userId", &user_id);
395 let headers: HashMap<String, String> = [
396 ("content-type".to_string(), "application/json".to_string()),
397 ].iter().cloned().collect();
398
399 let params: HashMap<String, ParamType> = [
400 ].iter().cloned().collect();
401
402 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
403
404 let processedResponse:models::SessionList = match response {
405 Ok(r) => {
406 match r.json() {
407 Ok(json) => json,
408 Err(e) => {
409 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
410 }
411 }
412 }
413 Err(e) => {
414 return Err(e);
415 }
416 };
417
418 Ok(processedResponse)
419 }
420
421 pub fn delete_sessions(&self, user_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
423 let path = "/users/userId/sessions".replace("userId", &user_id);
424 let headers: HashMap<String, String> = [
425 ("content-type".to_string(), "application/json".to_string()),
426 ].iter().cloned().collect();
427
428 let params: HashMap<String, ParamType> = [
429 ].iter().cloned().collect();
430
431 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
432
433 match response {
434 Ok(r) => {
435 let status_code = r.status();
436 if status_code == reqwest::StatusCode::NO_CONTENT {
437 Ok(json!(true))
438 } else {
439 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
440 }
441 }
442 Err(e) => {
443 Err(e)
444 }
445 }
446 }
447
448 pub fn delete_session(&self, user_id: &str, session_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
450 let path = "/users/userId/sessions/sessionId".replace("userId", &user_id).replace("sessionId", &session_id);
451 let headers: HashMap<String, String> = [
452 ("content-type".to_string(), "application/json".to_string()),
453 ].iter().cloned().collect();
454
455 let params: HashMap<String, ParamType> = [
456 ].iter().cloned().collect();
457
458 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
459
460 match response {
461 Ok(r) => {
462 let status_code = r.status();
463 if status_code == reqwest::StatusCode::NO_CONTENT {
464 Ok(json!(true))
465 } else {
466 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
467 }
468 }
469 Err(e) => {
470 Err(e)
471 }
472 }
473 }
474
475 pub fn update_status(&self, user_id: &str, status: bool) -> Result<models::User, AppwriteException> {
477 let path = "/users/userId/status".replace("userId", &user_id);
478 let headers: HashMap<String, String> = [
479 ("content-type".to_string(), "application/json".to_string()),
480 ].iter().cloned().collect();
481
482 let params: HashMap<String, ParamType> = [
483 ("status".to_string(), ParamType::Bool(status)),
484 ].iter().cloned().collect();
485
486 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
487
488 let processedResponse:models::User = match response {
489 Ok(r) => {
490 match r.json() {
491 Ok(json) => json,
492 Err(e) => {
493 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
494 }
495 }
496 }
497 Err(e) => {
498 return Err(e);
499 }
500 };
501
502 Ok(processedResponse)
503 }
504
505 pub fn update_verification(&self, user_id: &str, email_verification: bool) -> Result<models::User, AppwriteException> {
507 let path = "/users/userId/verification".replace("userId", &user_id);
508 let headers: HashMap<String, String> = [
509 ("content-type".to_string(), "application/json".to_string()),
510 ].iter().cloned().collect();
511
512 let params: HashMap<String, ParamType> = [
513 ("emailVerification".to_string(), ParamType::Bool(email_verification)),
514 ].iter().cloned().collect();
515
516 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
517
518 let processedResponse:models::User = match response {
519 Ok(r) => {
520 match r.json() {
521 Ok(json) => json,
522 Err(e) => {
523 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
524 }
525 }
526 }
527 Err(e) => {
528 return Err(e);
529 }
530 };
531
532 Ok(processedResponse)
533 }
534}