Skip to main content

cloudreve_sdk_api/api/
user.rs

1use crate::client::{Client, RequestOptions};
2use crate::error::ApiResult;
3use crate::models::explorer::StoragePolicy;
4use crate::models::user::*;
5use async_trait::async_trait;
6
7/// User and authentication API methods
8#[async_trait]
9pub trait UserApi {
10    /// Login with email and password
11    async fn login(&self, email: &str, password: &str) -> ApiResult<LoginResponse>;
12    
13    /// Login with 2FA code
14    async fn login_2fa(&self, otp: &str, session_id: &str) -> ApiResult<LoginResponse>;
15    
16    /// Get current user information
17    async fn get_user_me(&self) -> ApiResult<User>;
18    
19    /// Get user capacity information
20    async fn get_user_capacity(&self) -> ApiResult<Capacity>;
21    
22    /// Get user settings
23    async fn get_user_settings(&self) -> ApiResult<UserSettings>;
24    
25    /// Update user settings
26    async fn patch_user_settings(&self, settings: &PatchUserSetting) -> ApiResult<()>;
27    
28    /// Get credit change log
29    async fn get_credit_log(&self, params: &GetCreditLogService) -> ApiResult<CreditChangeLogResponse>;
30    
31    /// Sign up a new user
32    async fn sign_up(&self, request: &SignUpService) -> ApiResult<()>;
33    
34    /// Send password reset email
35    async fn send_reset_email(&self, request: &SendResetEmailService) -> ApiResult<()>;
36    
37    /// Reset password with secret
38    async fn reset_password(&self, request: &ResetPasswordService) -> ApiResult<()>;
39
40    /// Get user storage policies
41    async fn get_user_storage_policies(&self) -> ApiResult<Vec<StoragePolicy>>;
42}
43
44#[async_trait]
45impl UserApi for Client {
46    async fn login(&self, email: &str, password: &str) -> ApiResult<LoginResponse> {
47        let request = PasswordLoginRequest {
48            email: email.to_string(),
49            password: password.to_string(),
50            captcha: None,
51        };
52        
53        self.post(
54            "/session/token",
55            &request,
56            RequestOptions::new().no_credential(),
57        ).await
58    }
59    
60    async fn login_2fa(&self, otp: &str, session_id: &str) -> ApiResult<LoginResponse> {
61        let request = TwoFALoginRequest {
62            otp: otp.to_string(),
63            session_id: session_id.to_string(),
64        };
65        
66        self.post(
67            "/session/token/2fa",
68            &request,
69            RequestOptions::new().no_credential(),
70        ).await
71    }
72    
73    async fn get_user_me(&self) -> ApiResult<User> {
74        self.get("/user/me", RequestOptions::new()).await
75    }
76    
77    async fn get_user_capacity(&self) -> ApiResult<Capacity> {
78        self.get("/user/capacity", RequestOptions::new()).await
79    }
80
81    async fn get_user_storage_policies(&self) -> ApiResult<Vec<StoragePolicy>> {
82        self.get("/user/setting/policies", RequestOptions::new()).await
83    }
84    
85    async fn get_user_settings(&self) -> ApiResult<UserSettings> {
86        self.get("/user/setting", RequestOptions::new()).await
87    }
88    
89    async fn patch_user_settings(&self, settings: &PatchUserSetting) -> ApiResult<()> {
90        self.patch("/user/setting", settings, RequestOptions::new()).await
91    }
92    
93    async fn get_credit_log(&self, params: &GetCreditLogService) -> ApiResult<CreditChangeLogResponse> {
94        // Build query string from params
95        let mut query_params = vec![];
96        if let Some(page_size) = params.page_size {
97            query_params.push(format!("page_size={}", page_size));
98        }
99        if let Some(order_by) = &params.order_by {
100            query_params.push(format!("order_by={}", order_by));
101        }
102        if let Some(order_direction) = &params.order_direction {
103            query_params.push(format!("order_direction={}", order_direction));
104        }
105        if let Some(next_page_token) = &params.next_page_token {
106            query_params.push(format!("next_page_token={}", next_page_token));
107        }
108        
109        let query = if query_params.is_empty() {
110            String::new()
111        } else {
112            format!("?{}", query_params.join("&"))
113        };
114        
115        self.get(&format!("/user/credit/log{}", query), RequestOptions::new()).await
116    }
117    
118    async fn sign_up(&self, request: &SignUpService) -> ApiResult<()> {
119        self.post(
120            "/user",
121            request,
122            RequestOptions::new().no_credential(),
123        ).await
124    }
125    
126    async fn send_reset_email(&self, request: &SendResetEmailService) -> ApiResult<()> {
127        self.post(
128            "/user/reset",
129            request,
130            RequestOptions::new().no_credential(),
131        ).await
132    }
133    
134    async fn reset_password(&self, request: &ResetPasswordService) -> ApiResult<()> {
135        self.post(
136            "/user/reset/confirm",
137            request,
138            RequestOptions::new().no_credential(),
139        ).await
140    }
141}
142