use super::{DhanPayload, DhanPayloadBuilder, DhanResponse};
#[derive(Debug, Default)]
pub struct DhanClient {
client: reqwest::Client,
base_url: String,
}
impl DhanClient {
pub fn new() -> Self {
let client = reqwest::Client::new();
let base_url = "https://ticks.dhan.co".to_string();
Self { client, base_url }
}
pub fn builder() -> DhanClientBuilder {
DhanClientBuilder::new()
}
pub fn payload_builder(start: u32, end: u32) -> DhanPayloadBuilder {
DhanPayloadBuilder::new(start, end)
}
pub async fn get_prices(
&self,
payload: &DhanPayload,
) -> Result<DhanResponse, Box<dyn std::error::Error>> {
let url = format!("{}/getData", self.base_url);
let res = self.client.post(url).json(payload).send().await?;
Ok(res.json::<DhanResponse>().await?)
}
}
#[derive(Debug, Default)]
pub struct DhanClientBuilder {
client: reqwest::Client,
base_url: String,
}
impl DhanClientBuilder {
pub fn new() -> Self {
Self {
client: reqwest::Client::new(),
base_url: "https://ticks.dhan.co".to_string(),
}
}
pub fn base_url(mut self, base_url: &str) -> Self {
self.base_url = base_url.to_string();
self
}
pub fn build(self) -> DhanClient {
DhanClient {
client: self.client,
base_url: self.base_url,
}
}
}