use serde::{Deserialize, Serialize};
use crate::error::{FinanceError, Result};
use super::super::build_client;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IndexSession {
pub change: Option<f64>,
pub change_percent: Option<f64>,
pub close: Option<f64>,
pub high: Option<f64>,
pub low: Option<f64>,
pub open: Option<f64>,
pub previous_close: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IndexSnapshot {
pub value: Option<f64>,
pub name: Option<String>,
#[serde(rename = "type")]
pub index_type: Option<String>,
pub ticker: Option<String>,
pub market_status: Option<String>,
pub session: Option<IndexSession>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct IndexSnapshotResponse {
pub status: Option<String>,
pub request_id: Option<String>,
pub results: Option<Vec<IndexSnapshot>>,
}
pub async fn index_snapshot(ticker: &str) -> Result<IndexSnapshotResponse> {
let client = build_client()?;
let path = "/v3/snapshot/indices";
let params = [("ticker.any_of", ticker)];
let json = client.get_raw(path, ¶ms).await?;
serde_json::from_value(json).map_err(|e| FinanceError::ResponseStructureError {
field: "index_snapshot".to_string(),
context: format!("Failed to parse index snapshot response: {e}"),
})
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_index_snapshot_mock() {
let mut server = mockito::Server::new_async().await;
let _mock = server
.mock("GET", "/v3/snapshot/indices")
.match_query(mockito::Matcher::AllOf(vec![
mockito::Matcher::UrlEncoded("apiKey".into(), "test-key".into()),
mockito::Matcher::UrlEncoded("ticker.any_of".into(), "I:SPX".into()),
]))
.with_status(200)
.with_header("content-type", "application/json")
.with_body(
serde_json::json!({
"status": "OK",
"request_id": "abc123",
"results": [
{
"value": 4790.0,
"name": "S&P 500",
"type": "indices",
"ticker": "I:SPX",
"market_status": "open",
"session": {
"change": 20.0,
"change_percent": 0.42,
"close": 4790.0,
"high": 4800.0,
"low": 4760.0,
"open": 4770.0,
"previous_close": 4770.0
}
}
]
})
.to_string(),
)
.create_async()
.await;
let client = super::super::super::build_test_client(&server.url()).unwrap();
let json = client
.get_raw("/v3/snapshot/indices", &[("ticker.any_of", "I:SPX")])
.await
.unwrap();
let resp: IndexSnapshotResponse = serde_json::from_value(json).unwrap();
assert_eq!(resp.status.as_deref(), Some("OK"));
let results = resp.results.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].ticker.as_deref(), Some("I:SPX"));
assert!((results[0].value.unwrap() - 4790.0).abs() < 0.01);
let session = results[0].session.as_ref().unwrap();
assert!((session.change.unwrap() - 20.0).abs() < 0.01);
}
}