use crate::{
api::common::{latest_workday_string, validate_base_date},
client::Client,
data::{ApiResponse, esg::*},
error::Result,
};
use polars::prelude::DataFrame;
pub struct EsgApi<'a> {
pub(crate) client: &'a Client,
}
impl<'a> EsgApi<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub fn sri_bond_info(&self) -> SriBondInfoBuilder<'a> {
SriBondInfoBuilder::new(self.client)
}
}
#[must_use = "Builder does nothing unless you call .fetch()"]
pub struct SriBondInfoBuilder<'a> {
client: &'a Client,
base_date: Option<String>,
}
impl<'a> SriBondInfoBuilder<'a> {
fn new(client: &'a Client) -> Self {
Self {
client,
base_date: None,
}
}
pub fn date(mut self, date: impl Into<String>) -> Self {
self.base_date = Some(date.into());
self
}
pub fn latest(mut self) -> Self {
self.base_date = Some(latest_workday_string());
self
}
pub async fn fetch(self) -> Result<DataFrame> {
let base_date = validate_base_date(self.base_date)?;
let response = self
.client
.get::<ApiResponse<SriBondInfoRecord>>("/esg/sri_bond_info", &[("basDd", &base_date)])
.await?;
parse_sri_bond_info(response)
}
}