From d3737ff1b4c1bd55e51d00b351f36b72460a66d1 Mon Sep 17 00:00:00 2001 From: antifallobst Date: Fri, 3 Nov 2023 08:10:12 +0100 Subject: [PATCH] feat: added support for the `/account/id` endpoint --- src/error.rs | 18 ++++++++++++++++++ src/lib.rs | 26 ++++++++++++++++++++++++++ src/response.rs | 6 ++++++ 3 files changed, 50 insertions(+) diff --git a/src/error.rs b/src/error.rs index 34dcee0..5adbba1 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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, +} diff --git a/src/lib.rs b/src/lib.rs index ac84652..610dd45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -181,6 +181,32 @@ impl Session { } } + pub async fn account_id(&mut self) -> Result { + 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::() + .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() } diff --git a/src/response.rs b/src/response.rs index 01723df..77cefff 100644 --- a/src/response.rs +++ b/src/response.rs @@ -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, +}