vylth-flow 0.2.0

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

use crate::error::FlowResult;
use crate::http::HttpClient;
use crate::types::*;

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

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

    pub async fn get(&self, id: &str) -> FlowResult<Invoice> {
        let data = self.http.get(&format!("/invoices/{}", 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<Invoice>> {
        let map = params.map(list_to_map);
        let data = self.http.get("/invoices", map.as_ref()).await?;
        Ok(serde_json::from_slice(&data).map_err(|e| crate::error::FlowError::Other(e.to_string()))?)
    }

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

pub(crate) fn list_to_map(p: &ListParams) -> HashMap<String, String> {
    let mut m = HashMap::new();
    if let Some(page) = p.page {
        m.insert("page".into(), page.to_string());
    }
    if let Some(limit) = p.limit {
        m.insert("limit".into(), limit.to_string());
    }
    if let Some(ref status) = p.status {
        m.insert("status".into(), status.clone());
    }
    if let Some(ref currency) = p.currency {
        m.insert("currency".into(), currency.clone());
    }
    if let Some(ref network) = p.network {
        m.insert("network".into(), network.clone());
    }
    m
}