feat: added support for account deletion
This commit is contained in:
parent
44095c4235
commit
2921ec4882
18
src/error.rs
18
src/error.rs
|
@ -71,3 +71,21 @@ pub enum VerifyAccountError {
|
|||
#[error("Failed to verify account: unknown verification token")]
|
||||
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,
|
||||
}
|
||||
|
|
39
src/lib.rs
39
src/lib.rs
|
@ -2,8 +2,8 @@ pub mod error;
|
|||
pub mod request;
|
||||
pub mod response;
|
||||
|
||||
use crate::error::{AuthError, RegisterError, VerifyAccountError};
|
||||
use anyhow::Result;
|
||||
use crate::error::*;
|
||||
use anyhow::{Context, Result};
|
||||
use reqwest::StatusCode;
|
||||
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 {
|
||||
self.auth_token.is_some()
|
||||
}
|
||||
|
|
|
@ -17,3 +17,8 @@ pub struct AccountRegister {
|
|||
pub struct AccountVerify {
|
||||
pub token: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
pub struct AccountDelete {
|
||||
pub reason: Option<String>,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue