use crate::client::Client;
use reqwest::Method;
use serde_json::json;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Account {
client: Client,
}
impl Account {
pub fn new(client: &Client) -> Self {
Self { client: client.clone() }
}
pub fn client(&self) -> &Client {
&self.client
}
pub async fn get(
&self,
) -> crate::error::Result<crate::models::User> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn create(
&self,
user_id: impl Into<String>,
email: impl Into<String>,
password: impl Into<String>,
name: Option<&str>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("email".to_string(), json!(email.into()));
params.insert("password".to_string(), json!(password.into()));
if let Some(value) = name {
params.insert("name".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_email(
&self,
email: impl Into<String>,
password: impl Into<String>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("email".to_string(), json!(email.into()));
params.insert("password".to_string(), json!(password.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/email".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn list_identities(
&self,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::IdentityList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/identities".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn delete_identity(
&self,
identity_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/account/identities/{identityId}".to_string().replace("{identityId}", &identity_id.into().to_string());
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn create_jwt(
&self,
duration: Option<i64>,
) -> crate::error::Result<crate::models::Jwt> {
let mut params = HashMap::new();
if let Some(value) = duration {
params.insert("duration".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/jwts".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn list_logs(
&self,
queries: Option<Vec<String>>,
total: Option<bool>,
) -> crate::error::Result<crate::models::LogList> {
let mut params = HashMap::new();
if let Some(value) = queries {
params.insert("queries".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
if let Some(value) = total {
params.insert("total".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/logs".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn update_mfa(
&self,
mfa: bool,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("mfa".to_string(), json!(mfa));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn create_mfa_authenticator(
&self,
r#type: crate::enums::AuthenticatorType,
) -> crate::error::Result<crate::models::MfaType> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/authenticators/{type}".to_string().replace("{type}", &r#type.to_string());
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_mfa_authenticator(
&self,
r#type: crate::enums::AuthenticatorType,
otp: impl Into<String>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("otp".to_string(), json!(otp.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/authenticators/{type}".to_string().replace("{type}", &r#type.to_string());
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn delete_mfa_authenticator(
&self,
r#type: crate::enums::AuthenticatorType,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/account/mfa/authenticators/{type}".to_string().replace("{type}", &r#type.to_string());
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn create_mfa_challenge(
&self,
factor: crate::enums::AuthenticationFactor,
) -> crate::error::Result<crate::models::MfaChallenge> {
let mut params = HashMap::new();
params.insert("factor".to_string(), json!(factor));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/challenges".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_mfa_challenge(
&self,
challenge_id: impl Into<String>,
otp: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let mut params = HashMap::new();
params.insert("challengeId".to_string(), json!(challenge_id.into()));
params.insert("otp".to_string(), json!(otp.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/challenges".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn list_mfa_factors(
&self,
) -> crate::error::Result<crate::models::MfaFactors> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/factors".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn get_mfa_recovery_codes(
&self,
) -> crate::error::Result<crate::models::MfaRecoveryCodes> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/recovery-codes".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn create_mfa_recovery_codes(
&self,
) -> crate::error::Result<crate::models::MfaRecoveryCodes> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/recovery-codes".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_mfa_recovery_codes(
&self,
) -> crate::error::Result<crate::models::MfaRecoveryCodes> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/mfa/recovery-codes".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn update_name(
&self,
name: impl Into<String>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("name".to_string(), json!(name.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/name".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn update_password(
&self,
password: impl Into<String>,
old_password: Option<&str>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("password".to_string(), json!(password.into()));
if let Some(value) = old_password {
params.insert("oldPassword".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/password".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn update_phone(
&self,
phone: impl Into<String>,
password: impl Into<String>,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("phone".to_string(), json!(phone.into()));
params.insert("password".to_string(), json!(password.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/phone".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn get_prefs(
&self,
) -> crate::error::Result<crate::models::Preferences> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/prefs".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn update_prefs(
&self,
prefs: serde_json::Value,
) -> crate::error::Result<crate::models::User> {
let mut params = HashMap::new();
params.insert("prefs".to_string(), json!(prefs));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/prefs".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn create_recovery(
&self,
email: impl Into<String>,
url: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("email".to_string(), json!(email.into()));
params.insert("url".to_string(), json!(url.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/recovery".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_recovery(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
password: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
params.insert("password".to_string(), json!(password.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/recovery".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn list_sessions(
&self,
) -> crate::error::Result<crate::models::SessionList> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions".to_string();
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn delete_sessions(
&self,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/account/sessions".to_string();
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn create_anonymous_session(
&self,
) -> crate::error::Result<crate::models::Session> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/anonymous".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn create_email_password_session(
&self,
email: impl Into<String>,
password: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let mut params = HashMap::new();
params.insert("email".to_string(), json!(email.into()));
params.insert("password".to_string(), json!(password.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/email".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_magic_url_session(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/magic-url".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn update_phone_session(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/phone".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn create_session(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/token".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn get_session(
&self,
session_id: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/{sessionId}".to_string().replace("{sessionId}", &session_id.into().to_string());
self.client.call(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn update_session(
&self,
session_id: impl Into<String>,
) -> crate::error::Result<crate::models::Session> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/sessions/{sessionId}".to_string().replace("{sessionId}", &session_id.into().to_string());
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn delete_session(
&self,
session_id: impl Into<String>,
) -> crate::error::Result<()> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
let path = "/account/sessions/{sessionId}".to_string().replace("{sessionId}", &session_id.into().to_string());
self.client.call(Method::DELETE, &path, Some(api_headers), Some(params)).await
}
pub async fn update_status(
&self,
) -> crate::error::Result<crate::models::User> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/status".to_string();
self.client.call(Method::PATCH, &path, Some(api_headers), Some(params)).await
}
pub async fn create_email_token(
&self,
user_id: impl Into<String>,
email: impl Into<String>,
phrase: Option<bool>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("email".to_string(), json!(email.into()));
if let Some(value) = phrase {
params.insert("phrase".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/tokens/email".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn create_magic_url_token(
&self,
user_id: impl Into<String>,
email: impl Into<String>,
url: Option<&str>,
phrase: Option<bool>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("email".to_string(), json!(email.into()));
if let Some(value) = url {
params.insert("url".to_string(), json!(value));
}
if let Some(value) = phrase {
params.insert("phrase".to_string(), json!(value));
}
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/tokens/magic-url".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn create_o_auth2_token(
&self,
provider: crate::enums::OAuthProvider,
success: Option<&str>,
failure: Option<&str>,
scopes: Option<Vec<String>>,
) -> crate::error::Result<String> {
let mut params = HashMap::new();
if let Some(value) = success {
params.insert("success".to_string(), json!(value));
}
if let Some(value) = failure {
params.insert("failure".to_string(), json!(value));
}
if let Some(value) = scopes {
params.insert("scopes".to_string(), json!(value.into_iter().map(|s| s.into()).collect::<Vec<String>>()));
}
let mut api_headers = HashMap::new();
api_headers.insert("accept".to_string(), "text/html".to_string());
let path = "/account/tokens/oauth2/{provider}".to_string().replace("{provider}", &provider.to_string());
self.client.call_location(Method::GET, &path, Some(api_headers), Some(params)).await
}
pub async fn create_phone_token(
&self,
user_id: impl Into<String>,
phone: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("phone".to_string(), json!(phone.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/tokens/phone".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn create_email_verification(
&self,
url: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("url".to_string(), json!(url.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/email".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn create_verification(
&self,
url: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("url".to_string(), json!(url.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/email".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_email_verification(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/email".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn update_verification(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/email".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
pub async fn create_phone_verification(
&self,
) -> crate::error::Result<crate::models::Token> {
let params = HashMap::new();
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/phone".to_string();
self.client.call(Method::POST, &path, Some(api_headers), Some(params)).await
}
pub async fn update_phone_verification(
&self,
user_id: impl Into<String>,
secret: impl Into<String>,
) -> crate::error::Result<crate::models::Token> {
let mut params = HashMap::new();
params.insert("userId".to_string(), json!(user_id.into()));
params.insert("secret".to_string(), json!(secret.into()));
let mut api_headers = HashMap::new();
api_headers.insert("content-type".to_string(), "application/json".to_string());
api_headers.insert("accept".to_string(), "application/json".to_string());
let path = "/account/verifications/phone".to_string();
self.client.call(Method::PUT, &path, Some(api_headers), Some(params)).await
}
}
impl crate::services::Service for Account {
fn client(&self) -> &Client {
&self.client
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_account_creation() {
let client = Client::new();
let service = Account::new(&client);
assert!(service.client().endpoint().contains("cloud.appwrite.io/v1"));
}
}