delopay 0.3.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `analytics-dashboard` resource: the pre-aggregated dashboard feed.
//! Open request and response shapes — see the API reference.

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;

/// The `analytics-dashboard` resource, obtained via
/// [`crate::Delopay::analytics_dashboard`].
#[derive(Debug, Clone)]
pub struct AnalyticsDashboard {
    inner: Arc<Inner>,
}

impl AnalyticsDashboard {
    pub(crate) fn new(inner: Arc<Inner>) -> Self {
        Self { inner }
    }

    /// Get analytics dashboard data. Query keys are open — pass `&[]` for
    /// none; see the API reference for the accepted filters.
    pub async fn retrieve(&self, query: &[(&str, String)]) -> Result<Value> {
        self.inner
            .request(HttpMethod::Get, "/analytics-dashboard", None, query, None)
            .await
    }

    /// Generate an analytics dashboard report. The body shape is open — see
    /// the API reference.
    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");
    }
}