delopay 0.4.0

Official Rust SDK for the DeloPay payments API
Documentation
//! The `feature_matrix` resource: which connectors support which payment
//! methods, currencies and capabilities. Open response shape — see the API
//! reference.

use std::sync::Arc;

use serde_json::Value;

use crate::core::encoding::encode_segment;
use crate::core::error::Result;
use crate::http::client::Inner;
use crate::http::transport::HttpMethod;

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

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

    /// Retrieve the full feature matrix.
    pub async fn retrieve(&self) -> Result<Value> {
        self.inner
            .request(HttpMethod::Get, "/feature-matrix", None, &[], None)
            .await
    }

    /// Retrieve the feature matrix scoped to a merchant. Beta connectors are
    /// filtered against the merchant's allowlist, so the result only lists
    /// connectors the merchant can actually attach.
    pub async fn retrieve_for_merchant(&self, merchant_id: &str) -> Result<Value> {
        let path = format!("/feature-matrix/{}", encode_segment(merchant_id));
        self.inner
            .request(HttpMethod::Get, &path, None, &[], None)
            .await
    }
}

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

    #[tokio::test]
    async fn retrieve_gets_the_feature_matrix_root() {
        let (client, mock) = client_with(vec![Step::ok(r#"{"connectors":[]}"#)]);
        client.feature_matrix().retrieve().await.unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "GET");
        assert_eq!(seen[0].url, "https://api.test/feature-matrix");
    }

    #[tokio::test]
    async fn retrieve_for_merchant_encodes_the_merchant_id() {
        let (client, mock) = client_with(vec![Step::ok(r#"{"connectors":[]}"#)]);
        client
            .feature_matrix()
            .retrieve_for_merchant("mer/1")
            .await
            .unwrap();

        let seen = mock.seen.lock().unwrap();
        assert_eq!(seen[0].method, "GET");
        assert_eq!(seen[0].url, "https://api.test/feature-matrix/mer%2F1");
    }
}