use crate::adapters::common::encode_path_segment;
use crate::error::{FinanceError, Result};
use super::super::build_client;
use super::super::models::*;
pub async fn stock_sma(ticker: &str, params: &[(&str, &str)]) -> Result<IndicatorResponse> {
fetch_indicator(ticker, "sma", params).await
}
pub async fn stock_ema(ticker: &str, params: &[(&str, &str)]) -> Result<IndicatorResponse> {
fetch_indicator(ticker, "ema", params).await
}
pub async fn stock_macd(ticker: &str, params: &[(&str, &str)]) -> Result<IndicatorResponse> {
fetch_indicator(ticker, "macd", params).await
}
pub async fn stock_rsi(ticker: &str, params: &[(&str, &str)]) -> Result<IndicatorResponse> {
fetch_indicator(ticker, "rsi", params).await
}
async fn fetch_indicator(
ticker: &str,
indicator: &str,
params: &[(&str, &str)],
) -> Result<IndicatorResponse> {
let client = build_client()?;
let path = format!(
"/v1/indicators/{}/{}",
indicator,
encode_path_segment(ticker)
);
let json = client.get_raw(&path, params).await?;
serde_json::from_value(json).map_err(|e| FinanceError::ResponseStructureError {
field: indicator.to_string(),
context: format!("Failed to parse {indicator} response: {e}"),
})
}