polyoxide_data/api/
live_volume.rs1use polyoxide_core::{HttpClient, QueryBuilder, Request};
2use serde::{Deserialize, Serialize};
3
4use crate::error::DataApiError;
5
6#[derive(Clone)]
8pub struct LiveVolumeApi {
9 pub(crate) http_client: HttpClient,
10}
11
12impl LiveVolumeApi {
13 pub async fn get(&self, event_id: u64) -> Result<Vec<LiveVolume>, DataApiError> {
15 Request::<Vec<LiveVolume>, DataApiError>::new(self.http_client.clone(), "/live-volume")
16 .query("id", event_id)
17 .send()
18 .await
19 }
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24pub struct LiveVolume {
25 pub total: f64,
27 pub markets: Vec<MarketVolume>,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct MarketVolume {
34 pub market: String,
36 pub value: f64,
38}
39
40#[cfg(test)]
41mod tests {
42 use super::*;
43
44 #[test]
45 fn deserialize_live_volume() {
46 let json = r#"{
47 "total": 750000.0,
48 "markets": [
49 {"market": "cond_001", "value": 500000.0},
50 {"market": "cond_002", "value": 250000.0}
51 ]
52 }"#;
53
54 let vol: LiveVolume = serde_json::from_str(json).unwrap();
55 assert!((vol.total - 750000.0).abs() < f64::EPSILON);
56 assert_eq!(vol.markets.len(), 2);
57 assert_eq!(vol.markets[0].market, "cond_001");
58 assert!((vol.markets[0].value - 500000.0).abs() < f64::EPSILON);
59 assert_eq!(vol.markets[1].market, "cond_002");
60 assert!((vol.markets[1].value - 250000.0).abs() < f64::EPSILON);
61 }
62
63 #[test]
64 fn deserialize_live_volume_empty_markets() {
65 let json = r#"{"total": 0.0, "markets": []}"#;
66 let vol: LiveVolume = serde_json::from_str(json).unwrap();
67 assert!((vol.total - 0.0).abs() < f64::EPSILON);
68 assert!(vol.markets.is_empty());
69 }
70
71 #[test]
72 fn deserialize_market_volume() {
73 let json = r#"{"market": "cond_xyz", "value": 12345.67}"#;
74 let mv: MarketVolume = serde_json::from_str(json).unwrap();
75 assert_eq!(mv.market, "cond_xyz");
76 assert!((mv.value - 12345.67).abs() < f64::EPSILON);
77 }
78}