use std::fmt::Display;
use chrono::{DateTime, TimeZone, Timelike};
use super::BaseApi;
use crate::{Client, Query, Language, History};
pub struct HistoryApi<'a, Tz: TimeZone>
where
Tz::Offset: Display
{
client: &'a Client,
query: Option<Query>,
dt: Option<DateTime<Tz>>,
end_dt: Option<DateTime<Tz>>,
hour: bool,
lang: Option<Language>
}
impl<'a, Tz: TimeZone> HistoryApi<'a, Tz>
where
Tz::Offset: Display,
{
pub fn new(client: &'a Client) -> Self {
Self {
client,
query: None,
dt: None,
end_dt: None,
hour: false,
lang: None
}
}
pub fn query(mut self, query: Query) -> Self {
self.query = Some(query);
self
}
pub fn dt(mut self, dt: DateTime<Tz>) -> Self {
self.dt = Some(dt);
self
}
pub fn end_dt(mut self, end_dt: DateTime<Tz>) -> Self {
self.end_dt = Some(end_dt);
self
}
pub fn hour(mut self) -> Self {
self.hour = true;
self
}
pub fn lang(mut self, lang: Language) -> Self {
self.lang = Some(lang);
self
}
}
impl<'a, Tz: TimeZone> BaseApi<'a> for HistoryApi<'a, Tz>
where
Tz::Offset: Display,
{
type Model = History;
fn path(&self) -> &str {
"history"
}
fn client(&self) -> &'a Client {
self.client
}
fn params(&self) -> Vec<(&str, String)> {
let query = self.query.as_ref().unwrap();
let dt = self.dt.as_ref().unwrap();
let mut params = vec![
("key", self.client.api_key.clone()), ("q", query.to_string()),
("dt", format!("{}", dt.format("%Y-%m-%d"))),
];
if let Some(end_dt) = &self.end_dt {
params.push(("end_dt", format!("{}", end_dt.format("%Y-%m-%d"))))
}
if self.hour {
params.push(("hour", dt.hour().to_string()))
}
if let Some(lang) = &self.lang {
params.push(("lang", lang.to_string()))
}
params
}
}