feat: added support for the `/account/id` endpoint

This commit is contained in:
antifallobst 2023-11-03 08:10:12 +01:00
parent 2921ec4882
commit d3737ff1b4
Signed by: antifallobst
GPG Key ID: 2B4F402172791BAF
3 changed files with 50 additions and 0 deletions

View File

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

View File

@ -181,6 +181,32 @@ impl Session {
}
}
pub async fn account_id(&mut self) -> Result<response::AccountId, AccountIdError> {
let url = format!("{}/account/id", &self.base_url);
let request = self.client.get(&url).bearer_auth(
self.auth_token
.clone()
.context("")
.map_err(|_| AccountIdError::Unauthorized)?,
);
let response = request
.send()
.await
.map_err(|e| AccountIdError::RequestSend(e))?;
match response.status() {
StatusCode::OK => Ok(response
.json::<response::AccountId>()
.await
.map_err(|_| AccountIdError::BadResponse)?),
StatusCode::BAD_REQUEST => Err(AccountIdError::BadRequest),
StatusCode::UNAUTHORIZED => Err(AccountIdError::Unauthorized),
StatusCode::FORBIDDEN => Err(AccountIdError::Forbidden),
_ => Err(AccountIdError::BadResponse),
}
}
pub fn is_authenticated(&self) -> bool {
self.auth_token.is_some()
}

View File

@ -24,3 +24,9 @@ pub enum AccountRegisterUnprocessable {
pub struct AccountVerify {
pub token: String,
}
#[derive(Debug, Deserialize, Clone)]
pub struct AccountId {
pub id: u64,
pub username: String,
}