use crate::client::{Client, RequestOptions};
use crate::error::ApiResult;
use crate::models::explorer::StoragePolicy;
use crate::models::user::*;
use async_trait::async_trait;
#[async_trait]
pub trait UserApi {
async fn login(&self, email: &str, password: &str) -> ApiResult<LoginResponse>;
async fn login_2fa(&self, otp: &str, session_id: &str) -> ApiResult<LoginResponse>;
async fn get_user_me(&self) -> ApiResult<User>;
async fn get_user_capacity(&self) -> ApiResult<Capacity>;
async fn get_user_settings(&self) -> ApiResult<UserSettings>;
async fn patch_user_settings(&self, settings: &PatchUserSetting) -> ApiResult<()>;
async fn get_credit_log(&self, params: &GetCreditLogService) -> ApiResult<CreditChangeLogResponse>;
async fn sign_up(&self, request: &SignUpService) -> ApiResult<()>;
async fn send_reset_email(&self, request: &SendResetEmailService) -> ApiResult<()>;
async fn reset_password(&self, request: &ResetPasswordService) -> ApiResult<()>;
async fn get_user_storage_policies(&self) -> ApiResult<Vec<StoragePolicy>>;
}
#[async_trait]
impl UserApi for Client {
async fn login(&self, email: &str, password: &str) -> ApiResult<LoginResponse> {
let request = PasswordLoginRequest {
email: email.to_string(),
password: password.to_string(),
captcha: None,
};
self.post(
"/session/token",
&request,
RequestOptions::new().no_credential(),
).await
}
async fn login_2fa(&self, otp: &str, session_id: &str) -> ApiResult<LoginResponse> {
let request = TwoFALoginRequest {
otp: otp.to_string(),
session_id: session_id.to_string(),
};
self.post(
"/session/token/2fa",
&request,
RequestOptions::new().no_credential(),
).await
}
async fn get_user_me(&self) -> ApiResult<User> {
self.get("/user/me", RequestOptions::new()).await
}
async fn get_user_capacity(&self) -> ApiResult<Capacity> {
self.get("/user/capacity", RequestOptions::new()).await
}
async fn get_user_storage_policies(&self) -> ApiResult<Vec<StoragePolicy>> {
self.get("/user/setting/policies", RequestOptions::new()).await
}
async fn get_user_settings(&self) -> ApiResult<UserSettings> {
self.get("/user/setting", RequestOptions::new()).await
}
async fn patch_user_settings(&self, settings: &PatchUserSetting) -> ApiResult<()> {
self.patch("/user/setting", settings, RequestOptions::new()).await
}
async fn get_credit_log(&self, params: &GetCreditLogService) -> ApiResult<CreditChangeLogResponse> {
let mut query_params = vec![];
if let Some(page_size) = params.page_size {
query_params.push(format!("page_size={}", page_size));
}
if let Some(order_by) = ¶ms.order_by {
query_params.push(format!("order_by={}", order_by));
}
if let Some(order_direction) = ¶ms.order_direction {
query_params.push(format!("order_direction={}", order_direction));
}
if let Some(next_page_token) = ¶ms.next_page_token {
query_params.push(format!("next_page_token={}", next_page_token));
}
let query = if query_params.is_empty() {
String::new()
} else {
format!("?{}", query_params.join("&"))
};
self.get(&format!("/user/credit/log{}", query), RequestOptions::new()).await
}
async fn sign_up(&self, request: &SignUpService) -> ApiResult<()> {
self.post(
"/user",
request,
RequestOptions::new().no_credential(),
).await
}
async fn send_reset_email(&self, request: &SendResetEmailService) -> ApiResult<()> {
self.post(
"/user/reset",
request,
RequestOptions::new().no_credential(),
).await
}
async fn reset_password(&self, request: &ResetPasswordService) -> ApiResult<()> {
self.post(
"/user/reset/confirm",
request,
RequestOptions::new().no_credential(),
).await
}
}