feat: added support for the `/account/id` endpoint
This commit is contained in:
parent
2921ec4882
commit
d3737ff1b4
18
src/error.rs
18
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,
|
||||
}
|
||||
|
|
26
src/lib.rs
26
src/lib.rs
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue