feat: added support for account deletion

This commit is contained in:
antifallobst 2023-11-02 01:13:25 +01:00
parent 44095c4235
commit 2921ec4882
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 60 additions and 2 deletions

View File

@ -71,3 +71,21 @@ pub enum VerifyAccountError {
#[error("Failed to verify account: unknown verification token")] #[error("Failed to verify account: unknown verification token")]
UnknownToken, UnknownToken,
} }
#[derive(Debug, Error)]
pub enum DeleteAccountError {
#[error("Failed to delete account: the API complains about a malformed request")]
BadRequest,
#[error("Failed to delete account: unprocessable API response")]
BadResponse,
#[error("Failed to delete account: failed to send request")]
RequestSend(reqwest::Error),
#[error("Failed to delete account: you are not authorized to perform that operation")]
Unauthorized,
#[error("Failed to delete account: blocked for security reasons")]
Forbidden,
}

View File

@ -2,8 +2,8 @@ pub mod error;
pub mod request; pub mod request;
pub mod response; pub mod response;
use crate::error::{AuthError, RegisterError, VerifyAccountError}; use crate::error::*;
use anyhow::Result; use anyhow::{Context, Result};
use reqwest::StatusCode; use reqwest::StatusCode;
use std::fmt::Debug; use std::fmt::Debug;
@ -146,6 +146,41 @@ impl Session {
} }
} }
pub async fn delete_account(
&mut self,
reason: Option<String>,
) -> Result<(), DeleteAccountError> {
let url = format!("{}/account/delete", &self.base_url);
let body = request::AccountDelete { reason };
let request = self
.client
.delete(&url)
.bearer_auth(
self.auth_token
.clone()
.context("")
.map_err(|_| DeleteAccountError::Unauthorized)?,
)
.json(&body);
let response = request
.send()
.await
.map_err(|e| DeleteAccountError::RequestSend(e))?;
match response.status() {
StatusCode::OK => {
self.auth_token = None;
Ok(())
}
StatusCode::BAD_REQUEST => Err(DeleteAccountError::BadRequest),
StatusCode::UNAUTHORIZED => Err(DeleteAccountError::Unauthorized),
StatusCode::FORBIDDEN => Err(DeleteAccountError::Forbidden),
_ => Err(DeleteAccountError::BadResponse),
}
}
pub fn is_authenticated(&self) -> bool { pub fn is_authenticated(&self) -> bool {
self.auth_token.is_some() self.auth_token.is_some()
} }

View File

@ -17,3 +17,8 @@ pub struct AccountRegister {
pub struct AccountVerify { pub struct AccountVerify {
pub token: String, pub token: String,
} }
#[derive(Debug, Serialize, Clone)]
pub struct AccountDelete {
pub reason: Option<String>,
}