use super::urls::builders;
use crate::client::YahooClient;
use crate::constants::screeners::Screener;
use crate::error::Result;
use crate::models::screeners::{ScreenerField, ScreenerQuery, ScreenerResults};
pub async fn fetch(
client: &YahooClient,
screener_type: Screener,
count: u32,
) -> Result<ScreenerResults> {
let url = builders::screener(screener_type, count);
let response = client.request_with_crumb(&url).await?;
let json: serde_json::Value = response.json().await?;
parse_screeners_response(&json)
}
pub async fn fetch_custom<F: ScreenerField>(
client: &YahooClient,
query: ScreenerQuery<F>,
) -> Result<ScreenerResults> {
let url = builders::custom_screener();
let response = client.request_post_with_crumb(&url, &query).await?;
let json: serde_json::Value = response.json().await?;
parse_custom_screeners_response(&json)
}
fn parse_screeners_response(json: &serde_json::Value) -> Result<ScreenerResults> {
ScreenerResults::from_response(json).map_err(|e| {
crate::error::FinanceError::ResponseStructureError {
field: "screeners".to_string(),
context: e,
}
})
}
fn parse_custom_screeners_response(json: &serde_json::Value) -> Result<ScreenerResults> {
ScreenerResults::from_custom_response(json).map_err(|e| {
crate::error::FinanceError::ResponseStructureError {
field: "custom_screener".to_string(),
context: e,
}
})
}