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 Account {
10 client: Client
11}
12
13impl Account {
14 pub fn new(client: &Client) -> Self {
15 Self {
16 client: client.clone()
17 }
18 }
19
20 pub fn get(&self) -> Result<models::User, AppwriteException> {
22 let path = "/account";
23 let headers: HashMap<String, String> = [
24 ("content-type".to_string(), "application/json".to_string()),
25 ].iter().cloned().collect();
26
27 let params: HashMap<String, ParamType> = [
28 ].iter().cloned().collect();
29
30 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
31
32 let processedResponse:models::User = match response {
33 Ok(r) => {
34 match r.json() {
35 Ok(json) => json,
36 Err(e) => {
37 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
38 }
39 }
40 }
41 Err(e) => {
42 return Err(e);
43 }
44 };
45
46 Ok(processedResponse)
47 }
48
49 pub fn create(&self, user_id: &str, email: &str, password: &str, name: Option<&str>) -> Result<models::User, AppwriteException> {
56 let path = "/account";
57 let headers: HashMap<String, String> = [
58 ("content-type".to_string(), "application/json".to_string()),
59 ].iter().cloned().collect();
60
61 let name:&str = match name {
62 Some(data) => data,
63 None => ""
64 };
65
66 let params: HashMap<String, ParamType> = [
67 ("userId".to_string(), ParamType::String(user_id.to_string())),
68 ("email".to_string(), ParamType::String(email.to_string())),
69 ("password".to_string(), ParamType::String(password.to_string())),
70 ("name".to_string(), ParamType::String(name.to_string())),
71 ].iter().cloned().collect();
72
73 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
74
75 let processedResponse:models::User = match response {
76 Ok(r) => {
77 match r.json() {
78 Ok(json) => json,
79 Err(e) => {
80 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
81 }
82 }
83 }
84 Err(e) => {
85 return Err(e);
86 }
87 };
88
89 Ok(processedResponse)
90 }
91
92 pub fn delete(&self) -> Result<serde_json::value::Value, AppwriteException> {
98 let path = "/account";
99 let headers: HashMap<String, String> = [
100 ("content-type".to_string(), "application/json".to_string()),
101 ].iter().cloned().collect();
102
103 let params: HashMap<String, ParamType> = [
104 ].iter().cloned().collect();
105
106 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
107
108 match response {
109 Ok(r) => {
110 let status_code = r.status();
111 if status_code == reqwest::StatusCode::NO_CONTENT {
112 Ok(json!(true))
113 } else {
114 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
115 }
116 }
117 Err(e) => {
118 Err(e)
119 }
120 }
121 }
122
123 pub fn update_email(&self, email: &str, password: &str) -> Result<models::User, AppwriteException> {
132 let path = "/account/email";
133 let headers: HashMap<String, String> = [
134 ("content-type".to_string(), "application/json".to_string()),
135 ].iter().cloned().collect();
136
137 let params: HashMap<String, ParamType> = [
138 ("email".to_string(), ParamType::String(email.to_string())),
139 ("password".to_string(), ParamType::String(password.to_string())),
140 ].iter().cloned().collect();
141
142 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
143
144 let processedResponse:models::User = match response {
145 Ok(r) => {
146 match r.json() {
147 Ok(json) => json,
148 Err(e) => {
149 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
150 }
151 }
152 }
153 Err(e) => {
154 return Err(e);
155 }
156 };
157
158 Ok(processedResponse)
159 }
160
161 pub fn create_jwt(&self) -> Result<models::Jwt, AppwriteException> {
167 let path = "/account/jwt";
168 let headers: HashMap<String, String> = [
169 ("content-type".to_string(), "application/json".to_string()),
170 ].iter().cloned().collect();
171
172 let params: HashMap<String, ParamType> = [
173 ].iter().cloned().collect();
174
175 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
176
177 let processedResponse:models::Jwt = match response {
178 Ok(r) => {
179 match r.json() {
180 Ok(json) => json,
181 Err(e) => {
182 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
183 }
184 }
185 }
186 Err(e) => {
187 return Err(e);
188 }
189 };
190
191 Ok(processedResponse)
192 }
193
194 pub fn get_logs(&self, limit: Option<i64>, offset: Option<i64>) -> Result<models::LogList, AppwriteException> {
197 let path = "/account/logs";
198 let headers: HashMap<String, String> = [
199 ("content-type".to_string(), "application/json".to_string()),
200 ].iter().cloned().collect();
201
202 let params: HashMap<String, ParamType> = [
203 ("limit".to_string(), ParamType::OptionalNumber(limit)),
204 ("offset".to_string(), ParamType::OptionalNumber(offset)),
205 ].iter().cloned().collect();
206
207 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
208
209 let processedResponse:models::LogList = match response {
210 Ok(r) => {
211 match r.json() {
212 Ok(json) => json,
213 Err(e) => {
214 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
215 }
216 }
217 }
218 Err(e) => {
219 return Err(e);
220 }
221 };
222
223 Ok(processedResponse)
224 }
225
226 pub fn update_name(&self, name: &str) -> Result<models::User, AppwriteException> {
228 let path = "/account/name";
229 let headers: HashMap<String, String> = [
230 ("content-type".to_string(), "application/json".to_string()),
231 ].iter().cloned().collect();
232
233 let params: HashMap<String, ParamType> = [
234 ("name".to_string(), ParamType::String(name.to_string())),
235 ].iter().cloned().collect();
236
237 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
238
239 let processedResponse:models::User = match response {
240 Ok(r) => {
241 match r.json() {
242 Ok(json) => json,
243 Err(e) => {
244 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
245 }
246 }
247 }
248 Err(e) => {
249 return Err(e);
250 }
251 };
252
253 Ok(processedResponse)
254 }
255
256 pub fn update_password(&self, password: &str, old_password: Option<&str>) -> Result<models::User, AppwriteException> {
260 let path = "/account/password";
261 let headers: HashMap<String, String> = [
262 ("content-type".to_string(), "application/json".to_string()),
263 ].iter().cloned().collect();
264
265 let old_password:&str = match old_password {
266 Some(data) => data,
267 None => ""
268 };
269
270 let params: HashMap<String, ParamType> = [
271 ("password".to_string(), ParamType::String(password.to_string())),
272 ("oldPassword".to_string(), ParamType::String(old_password.to_string())),
273 ].iter().cloned().collect();
274
275 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
276
277 let processedResponse:models::User = match response {
278 Ok(r) => {
279 match r.json() {
280 Ok(json) => json,
281 Err(e) => {
282 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
283 }
284 }
285 }
286 Err(e) => {
287 return Err(e);
288 }
289 };
290
291 Ok(processedResponse)
292 }
293
294 pub fn get_prefs(&self) -> Result<models::Preferences, AppwriteException> {
296 let path = "/account/prefs";
297 let headers: HashMap<String, String> = [
298 ("content-type".to_string(), "application/json".to_string()),
299 ].iter().cloned().collect();
300
301 let params: HashMap<String, ParamType> = [
302 ].iter().cloned().collect();
303
304 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
305
306 let processedResponse:models::Preferences = match response {
307 Ok(r) => {
308 match r.json() {
309 Ok(json) => json,
310 Err(e) => {
311 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
312 }
313 }
314 }
315 Err(e) => {
316 return Err(e);
317 }
318 };
319
320 Ok(processedResponse)
321 }
322
323 pub fn update_prefs(&self, prefs: Option<HashMap<String, crate::client::ParamType>>) -> Result<models::User, AppwriteException> {
327 let path = "/account/prefs";
328 let headers: HashMap<String, String> = [
329 ("content-type".to_string(), "application/json".to_string()),
330 ].iter().cloned().collect();
331
332 let params: HashMap<String, ParamType> = [
333 ("prefs".to_string(), ParamType::Object(prefs.unwrap())),
334 ].iter().cloned().collect();
335
336 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
337
338 let processedResponse:models::User = match response {
339 Ok(r) => {
340 match r.json() {
341 Ok(json) => json,
342 Err(e) => {
343 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
344 }
345 }
346 }
347 Err(e) => {
348 return Err(e);
349 }
350 };
351
352 Ok(processedResponse)
353 }
354
355 pub fn create_recovery(&self, email: &str, url: &str) -> Result<models::Token, AppwriteException> {
364 let path = "/account/recovery";
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 ("email".to_string(), ParamType::String(email.to_string())),
371 ("url".to_string(), ParamType::String(url.to_string())),
372 ].iter().cloned().collect();
373
374 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
375
376 let processedResponse:models::Token = match response {
377 Ok(r) => {
378 match r.json() {
379 Ok(json) => json,
380 Err(e) => {
381 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
382 }
383 }
384 }
385 Err(e) => {
386 return Err(e);
387 }
388 };
389
390 Ok(processedResponse)
391 }
392
393 pub fn update_recovery(&self, user_id: &str, secret: &str, password: &str, password_again: &str) -> Result<models::Token, AppwriteException> {
403 let path = "/account/recovery";
404 let headers: HashMap<String, String> = [
405 ("content-type".to_string(), "application/json".to_string()),
406 ].iter().cloned().collect();
407
408 let params: HashMap<String, ParamType> = [
409 ("userId".to_string(), ParamType::String(user_id.to_string())),
410 ("secret".to_string(), ParamType::String(secret.to_string())),
411 ("password".to_string(), ParamType::String(password.to_string())),
412 ("passwordAgain".to_string(), ParamType::String(password_again.to_string())),
413 ].iter().cloned().collect();
414
415 let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
416
417 let processedResponse:models::Token = match response {
418 Ok(r) => {
419 match r.json() {
420 Ok(json) => json,
421 Err(e) => {
422 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
423 }
424 }
425 }
426 Err(e) => {
427 return Err(e);
428 }
429 };
430
431 Ok(processedResponse)
432 }
433
434 pub fn get_sessions(&self) -> Result<models::SessionList, AppwriteException> {
437 let path = "/account/sessions";
438 let headers: HashMap<String, String> = [
439 ("content-type".to_string(), "application/json".to_string()),
440 ].iter().cloned().collect();
441
442 let params: HashMap<String, ParamType> = [
443 ].iter().cloned().collect();
444
445 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
446
447 let processedResponse:models::SessionList = match response {
448 Ok(r) => {
449 match r.json() {
450 Ok(json) => json,
451 Err(e) => {
452 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
453 }
454 }
455 }
456 Err(e) => {
457 return Err(e);
458 }
459 };
460
461 Ok(processedResponse)
462 }
463
464 pub fn create_session(&self, email: &str, password: &str) -> Result<models::Session, AppwriteException> {
467 let path = "/account/sessions";
468 let headers: HashMap<String, String> = [
469 ("content-type".to_string(), "application/json".to_string()),
470 ].iter().cloned().collect();
471
472 let params: HashMap<String, ParamType> = [
473 ("email".to_string(), ParamType::String(email.to_string())),
474 ("password".to_string(), ParamType::String(password.to_string())),
475 ].iter().cloned().collect();
476
477 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
478
479 let processedResponse:models::Session = match response {
480 Ok(r) => {
481 match r.json() {
482 Ok(json) => json,
483 Err(e) => {
484 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
485 }
486 }
487 }
488 Err(e) => {
489 return Err(e);
490 }
491 };
492
493 Ok(processedResponse)
494 }
495
496 pub fn delete_sessions(&self) -> Result<serde_json::value::Value, AppwriteException> {
499 let path = "/account/sessions";
500 let headers: HashMap<String, String> = [
501 ("content-type".to_string(), "application/json".to_string()),
502 ].iter().cloned().collect();
503
504 let params: HashMap<String, ParamType> = [
505 ].iter().cloned().collect();
506
507 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
508
509 match response {
510 Ok(r) => {
511 let status_code = r.status();
512 if status_code == reqwest::StatusCode::NO_CONTENT {
513 Ok(json!(true))
514 } else {
515 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
516 }
517 }
518 Err(e) => {
519 Err(e)
520 }
521 }
522 }
523
524 pub fn create_anonymous_session(&self) -> Result<models::Session, AppwriteException> {
531 let path = "/account/sessions/anonymous";
532 let headers: HashMap<String, String> = [
533 ("content-type".to_string(), "application/json".to_string()),
534 ].iter().cloned().collect();
535
536 let params: HashMap<String, ParamType> = [
537 ].iter().cloned().collect();
538
539 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
540
541 let processedResponse:models::Session = match response {
542 Ok(r) => {
543 match r.json() {
544 Ok(json) => json,
545 Err(e) => {
546 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
547 }
548 }
549 }
550 Err(e) => {
551 return Err(e);
552 }
553 };
554
555 Ok(processedResponse)
556 }
557
558 pub fn create_magic_url_session(&self, user_id: &str, email: &str, url: Option<&str>) -> Result<models::Token, AppwriteException> {
569 let path = "/account/sessions/magic-url";
570 let headers: HashMap<String, String> = [
571 ("content-type".to_string(), "application/json".to_string()),
572 ].iter().cloned().collect();
573
574 let url:&str = match url {
575 Some(data) => data,
576 None => ""
577 };
578
579 let params: HashMap<String, ParamType> = [
580 ("userId".to_string(), ParamType::String(user_id.to_string())),
581 ("email".to_string(), ParamType::String(email.to_string())),
582 ("url".to_string(), ParamType::String(url.to_string())),
583 ].iter().cloned().collect();
584
585 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
586
587 let processedResponse:models::Token = match response {
588 Ok(r) => {
589 match r.json() {
590 Ok(json) => json,
591 Err(e) => {
592 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
593 }
594 }
595 }
596 Err(e) => {
597 return Err(e);
598 }
599 };
600
601 Ok(processedResponse)
602 }
603
604 pub fn update_magic_url_session(&self, user_id: &str, secret: &str) -> Result<models::Session, AppwriteException> {
616 let path = "/account/sessions/magic-url";
617 let headers: HashMap<String, String> = [
618 ("content-type".to_string(), "application/json".to_string()),
619 ].iter().cloned().collect();
620
621 let params: HashMap<String, ParamType> = [
622 ("userId".to_string(), ParamType::String(user_id.to_string())),
623 ("secret".to_string(), ParamType::String(secret.to_string())),
624 ].iter().cloned().collect();
625
626 let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
627
628 let processedResponse:models::Session = match response {
629 Ok(r) => {
630 match r.json() {
631 Ok(json) => json,
632 Err(e) => {
633 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
634 }
635 }
636 }
637 Err(e) => {
638 return Err(e);
639 }
640 };
641
642 Ok(processedResponse)
643 }
644
645 pub fn create_o_auth2_session(&self, provider: &str, success: Option<&str>, failure: Option<&str>, scopes: Option<&[&str]>) -> Result<serde_json::value::Value, AppwriteException> {
658 let path = "/account/sessions/oauth2/provider".replace("provider", &provider);
659 let headers: HashMap<String, String> = [
660 ("content-type".to_string(), "application/json".to_string()),
661 ].iter().cloned().collect();
662
663 let success:&str = match success {
664 Some(data) => data,
665 None => ""
666 };
667
668 let failure:&str = match failure {
669 Some(data) => data,
670 None => ""
671 };
672
673 let scopes:&[&str] = match scopes {
674 Some(data) => data,
675 None => &[]
676 };
677
678 let params: HashMap<String, ParamType> = [
679 ("success".to_string(), ParamType::String(success.to_string())),
680 ("failure".to_string(), ParamType::String(failure.to_string())),
681 ("scopes".to_string(), ParamType::Array(scopes.into_iter().map(|x| ParamType::String(x.to_string())).collect())),
682 ].iter().cloned().collect();
683
684 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
685
686 match response {
687 Ok(r) => {
688 let status_code = r.status();
689 if status_code == reqwest::StatusCode::NO_CONTENT {
690 Ok(json!(true))
691 } else {
692 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
693 }
694 }
695 Err(e) => {
696 Err(e)
697 }
698 }
699 }
700
701 pub fn get_session(&self, session_id: &str) -> Result<models::Session, AppwriteException> {
704 let path = "/account/sessions/sessionId".replace("sessionId", &session_id);
705 let headers: HashMap<String, String> = [
706 ("content-type".to_string(), "application/json".to_string()),
707 ].iter().cloned().collect();
708
709 let params: HashMap<String, ParamType> = [
710 ].iter().cloned().collect();
711
712 let response = self.client.clone().call("GET", &path, Some(headers), Some(params) );
713
714 let processedResponse:models::Session = match response {
715 Ok(r) => {
716 match r.json() {
717 Ok(json) => json,
718 Err(e) => {
719 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
720 }
721 }
722 }
723 Err(e) => {
724 return Err(e);
725 }
726 };
727
728 Ok(processedResponse)
729 }
730
731 pub fn update_session(&self, session_id: &str) -> Result<models::Session, AppwriteException> {
732 let path = "/account/sessions/sessionId".replace("sessionId", &session_id);
733 let headers: HashMap<String, String> = [
734 ("content-type".to_string(), "application/json".to_string()),
735 ].iter().cloned().collect();
736
737 let params: HashMap<String, ParamType> = [
738 ].iter().cloned().collect();
739
740 let response = self.client.clone().call("PATCH", &path, Some(headers), Some(params) );
741
742 let processedResponse:models::Session = match response {
743 Ok(r) => {
744 match r.json() {
745 Ok(json) => json,
746 Err(e) => {
747 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
748 }
749 }
750 }
751 Err(e) => {
752 return Err(e);
753 }
754 };
755
756 Ok(processedResponse)
757 }
758
759 pub fn delete_session(&self, session_id: &str) -> Result<serde_json::value::Value, AppwriteException> {
764 let path = "/account/sessions/sessionId".replace("sessionId", &session_id);
765 let headers: HashMap<String, String> = [
766 ("content-type".to_string(), "application/json".to_string()),
767 ].iter().cloned().collect();
768
769 let params: HashMap<String, ParamType> = [
770 ].iter().cloned().collect();
771
772 let response = self.client.clone().call("DELETE", &path, Some(headers), Some(params) );
773
774 match response {
775 Ok(r) => {
776 let status_code = r.status();
777 if status_code == reqwest::StatusCode::NO_CONTENT {
778 Ok(json!(true))
779 } else {
780 Ok(serde_json::from_str(&r.text().unwrap()).unwrap())
781 }
782 }
783 Err(e) => {
784 Err(e)
785 }
786 }
787 }
788
789 pub fn create_verification(&self, url: &str) -> Result<models::Token, AppwriteException> {
805 let path = "/account/verification";
806 let headers: HashMap<String, String> = [
807 ("content-type".to_string(), "application/json".to_string()),
808 ].iter().cloned().collect();
809
810 let params: HashMap<String, ParamType> = [
811 ("url".to_string(), ParamType::String(url.to_string())),
812 ].iter().cloned().collect();
813
814 let response = self.client.clone().call("POST", &path, Some(headers), Some(params) );
815
816 let processedResponse:models::Token = match response {
817 Ok(r) => {
818 match r.json() {
819 Ok(json) => json,
820 Err(e) => {
821 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
822 }
823 }
824 }
825 Err(e) => {
826 return Err(e);
827 }
828 };
829
830 Ok(processedResponse)
831 }
832
833 pub fn update_verification(&self, user_id: &str, secret: &str) -> Result<models::Token, AppwriteException> {
838 let path = "/account/verification";
839 let headers: HashMap<String, String> = [
840 ("content-type".to_string(), "application/json".to_string()),
841 ].iter().cloned().collect();
842
843 let params: HashMap<String, ParamType> = [
844 ("userId".to_string(), ParamType::String(user_id.to_string())),
845 ("secret".to_string(), ParamType::String(secret.to_string())),
846 ].iter().cloned().collect();
847
848 let response = self.client.clone().call("PUT", &path, Some(headers), Some(params) );
849
850 let processedResponse:models::Token = match response {
851 Ok(r) => {
852 match r.json() {
853 Ok(json) => json,
854 Err(e) => {
855 return Err(AppwriteException::new(format!("Error parsing response json: {}", e), 0, "".to_string()));
856 }
857 }
858 }
859 Err(e) => {
860 return Err(e);
861 }
862 };
863
864 Ok(processedResponse)
865 }
866}