vylth-flow 0.3.2

Official Rust SDK for Vylth Flow — self-custody crypto payment processing
Documentation
use std::sync::Arc;

use crate::error::FlowResult;
use crate::http::HttpClient;
use crate::resources::invoices::list_to_map;
use crate::types::*;

pub struct PayoutService {
    pub(crate) http: Arc<HttpClient>,
}

#[derive(serde::Serialize)]
struct BatchBody<'a> {
    payouts: &'a [CreatePayoutParams],
}

#[derive(serde::Deserialize)]
struct BatchResponse {
    payouts: Vec<Payout>,
}

impl PayoutService {
    pub async fn create(&self, params: CreatePayoutParams) -> FlowResult<Payout> {
        let data = self.http.post("/vendor/payout", Some(&params)).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn create_batch(&self, payouts: &[CreatePayoutParams]) -> FlowResult<Vec<Payout>> {
        let body = BatchBody { payouts };
        let data = self.http.post("/vendor/payout/batch", Some(&body)).await?;
        let resp: BatchResponse = serde_json::from_slice(&data)
            .map_err(|e| crate::error::FlowError::Other(e.to_string()))?;
        Ok(resp.payouts)
    }

    pub async fn get(&self, id: &str) -> FlowResult<Payout> {
        let data = self.http.get(&format!("/vendor/query/payout/{}", id), None).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

    pub async fn list(&self, params: Option<&ListParams>) -> FlowResult<PaginatedList<Payout>> {
        let map = params.map(list_to_map);
        let data = self.http.get("/vendor/query/payouts", map.as_ref()).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }
}