delopay 0.2.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `export` resource: bulk transaction exports. 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 `export` resource, obtained via [`crate::Delopay::export`].
#[derive(Debug, Clone)]
pub struct Export {
    inner: Arc<Inner>,
}

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

    /// Export transactions matching the given filters.
    pub async fn transactions(&self, params: &Value) -> Result<Value> {
        let body = to_json_vec(params)?;
        self.inner
            .request(
                HttpMethod::Post,
                "/export/transactions",
                Some(&body),
                &[],
                None,
            )
            .await
    }
}

#[cfg(test)]
mod tests {
    use crate::http::mock::{Step, client_with};
    use serde_json::json;

    #[tokio::test]
    async fn transactions_posts_to_the_export_path() {
        let (client, mock) = client_with(vec![Step::ok(r#"{"export_id":"exp_1"}"#)]);
        client
            .export()
            .transactions(&json!({"format": "csv"}))
            .await
            .unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "POST");
        assert_eq!(seen[0].url, "https://api.test/export/transactions");
        assert_eq!(seen[0].body_json()["format"], "csv");
    }
}