use crate::error::AccountBalanceError;
use alloc::{boxed::Box, format};
use asimov_credit::Credits;
#[cfg(not(target_family = "wasm"))]
use asimov_http::proxy::{OriginProxy, ProxyOptions};
use core::{error::Error, str::FromStr};
use derive_more::Display;
pub use asimov_id::{Handle, HandleError};
#[derive(Clone, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[display("@{}", self.0)]
pub struct Account(pub(crate) Handle);
impl FromStr for Account {
type Err = HandleError;
fn from_str(input: &str) -> Result<Self, Self::Err> {
Ok(Self(input.parse()?))
}
}
impl Account {
pub async fn open(id: Handle) -> Result<Self, Box<dyn Error>> {
Ok(Self(id))
}
pub async fn has_credits(&self) -> Result<bool, AccountBalanceError> {
Ok(self.balance().await? > Credits::ZERO)
}
pub async fn balance(&self) -> Result<Credits, AccountBalanceError> {
let url = format!("https://asimov.credit/{}/balance.txt", self.0);
Self::fetch_balance(&url).await
}
async fn fetch_balance(url: &str) -> Result<Credits, AccountBalanceError> {
let builder = reqwest::Client::builder().user_agent("ASIMOV.rs");
#[cfg(not(target_family = "wasm"))]
let url = reqwest::Url::parse(url)
.map_err(|_| asimov_http::proxy::Error::InvalidOrigin("expected an absolute URL"))?;
#[cfg(not(target_family = "wasm"))]
let builder =
OriginProxy::resolve(&url, &ProxyOptions::default())?.configure_reqwest(builder)?;
let client = builder.build()?;
let response = client.get(url).send().await?;
if response.status() != reqwest::StatusCode::OK {
return Err(AccountBalanceError::UnexpectedResponse(response.status()));
}
let response_text = response.text().await?;
Ok(response_text.parse::<Credits>()?)
}
}
#[cfg(all(test, not(target_family = "wasm")))]
mod tests;