use std::sync::Arc;
use serde_json::Value;
use crate::core::encoding::to_json_vec;
use crate::core::error::Result;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;
#[derive(Debug, Clone)]
pub struct AnalyticsDashboard {
inner: Arc<Inner>,
}
impl AnalyticsDashboard {
pub(crate) fn new(inner: Arc<Inner>) -> Self {
Self { inner }
}
pub async fn retrieve(&self, query: &[(&str, String)]) -> Result<Value> {
self.inner
.request(HttpMethod::Get, "/analytics-dashboard", None, query, None)
.await
}
pub async fn generate(&self, params: &Value) -> Result<Value> {
let body = to_json_vec(params)?;
self.inner
.request(
HttpMethod::Post,
"/analytics-dashboard",
Some(&body),
&[],
None,
)
.await
}
}
#[cfg(test)]
mod tests {
use crate::http::mock::{Step, client_with};
use serde_json::json;
#[tokio::test]
async fn retrieve_gets_the_dashboard_path_with_its_query() {
let (client, mock) = client_with(vec![Step::ok(r#"{"total":0}"#)]);
client
.analytics_dashboard()
.retrieve(&[
("start_time", "2026-01-01".to_string()),
("granularity", "day".to_string()),
])
.await
.unwrap();
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "GET");
assert_eq!(
seen[0].url,
"https://api.test/analytics-dashboard?start_time=2026-01-01&granularity=day"
);
}
#[tokio::test]
async fn generate_posts_the_body_to_the_dashboard_path() {
let (client, mock) = client_with(vec![Step::ok("{}")]);
client
.analytics_dashboard()
.generate(&json!({"metrics": ["payment_count"]}))
.await
.unwrap();
let seen = mock.seen.lock().unwrap();
assert_eq!(seen[0].method, "POST");
assert_eq!(seen[0].url, "https://api.test/analytics-dashboard");
assert_eq!(seen[0].body_json()["metrics"][0], "payment_count");
}
}