use error::{ApiError, ZarinResult};
use methods::ApiMethod;
pub mod error;
pub mod extensions;
pub mod methods;
pub mod prelude;
pub mod results;
#[async_trait::async_trait]
pub trait ZarinpalClient {
fn client(&self) -> &reqwest::Client;
fn merchant_id(&self) -> &str;
fn base_url(&self) -> &reqwest::Url;
async fn send<M: ApiMethod + Send + Sync>(&self, mut method: M) -> ZarinResult<M::Result> {
let mut url = self.base_url().clone();
url.set_path(M::PATH);
method.set_merchant_id_if_needed(self.merchant_id().clone());
let result = self
.client()
.post(url)
.json(&method)
.send()
.await?
.json::<crate::results::__private::ApiResult<M::Result>>()
.await;
result
.map(|f| Into::<Result<M::Result, ApiError>>::into(f))?
.map_err(|e| e.into())
}
}
#[derive(Debug, Clone)]
pub struct Zarinpal {
client: reqwest::Client,
merchant_id: String,
base_url: reqwest::Url,
}
#[async_trait::async_trait]
impl ZarinpalClient for Zarinpal {
fn client(&self) -> &reqwest::Client {
&self.client
}
fn merchant_id(&self) -> &str {
&self.merchant_id
}
fn base_url(&self) -> &reqwest::Url {
&self.base_url
}
}
impl Zarinpal {
pub fn new(merchant_id: &str) -> Result<Self, uuid::Error> {
let merchant_id_uuid = uuid::Uuid::parse_str(merchant_id)?;
Ok(Self {
client: reqwest::Client::new(),
merchant_id: merchant_id_uuid.to_string(),
base_url: "https://api.zarinpal.com/".parse().unwrap(),
})
}
pub fn new_with_client(
merchant_id: &str,
client: reqwest::Client,
) -> Result<Self, uuid::Error> {
let merchant_id_uuid = uuid::Uuid::parse_str(merchant_id)?;
Ok(Self {
client,
merchant_id: merchant_id_uuid.to_string(),
base_url: "https://api.zarinpal.com/".parse().unwrap(),
})
}
}