use chrono::{DateTime, Utc};
use serde::Serialize;
use crate::CandleResolution;
#[cfg(doc)]
use crate::Fyers;
#[must_use = "builders must be finalized with .build()"]
#[derive(Debug, Clone)]
pub struct HistoryBuilder {
symbol: String,
resolution: CandleResolution,
from: DateTime<Utc>,
to: DateTime<Utc>,
include_oi: bool,
}
#[derive(Serialize)]
pub struct HistoryRequest {
symbol: String,
resolution: CandleResolution,
date_format: &'static str,
range_from: String,
range_to: String,
cont_flag: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
oi_flag: Option<&'static str>,
}
impl HistoryBuilder {
pub fn new(symbol: impl Into<String>, from: DateTime<Utc>, to: DateTime<Utc>) -> Self {
Self {
symbol: symbol.into(),
resolution: CandleResolution::Minute5,
from,
to,
include_oi: false,
}
}
pub fn resolution(mut self, resolution: CandleResolution) -> Self {
self.resolution = resolution;
self
}
pub fn include_oi(mut self, include: bool) -> Self {
self.include_oi = include;
self
}
pub fn build(self) -> HistoryRequest {
HistoryRequest {
symbol: self.symbol,
resolution: self.resolution,
date_format: "0",
range_from: self.from.timestamp().to_string(),
range_to: self.to.timestamp().to_string(),
cont_flag: "1",
oi_flag: if self.include_oi { Some("1") } else { None },
}
}
}
impl HistoryRequest {
pub fn builder(
symbol: impl Into<String>,
from: DateTime<Utc>,
to: DateTime<Utc>,
) -> HistoryBuilder {
HistoryBuilder::new(symbol, from, to)
}
}