vylth-flow 0.2.0

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

use crate::http::HttpClient;
use crate::resources::{
    invoices::InvoiceService, payouts::PayoutService, swaps::SwapService, wallets::WalletService,
    webhooks::WebhookVerifier, payment_links::PaymentLinkService, subscriptions::SubscriptionService,
    teams::TeamService,
};

const DEFAULT_BASE_URL: &str = "https://flow.vylth.com/api/flow";
const DEFAULT_TIMEOUT_SECS: u64 = 30;

/// The Vylth Flow client.
pub struct Flow {
    http: Arc<HttpClient>,
    webhook_secret: Option<String>,
}

impl Flow {
    /// Create a new client with the given API key.
    pub fn new(api_key: &str) -> Self {
        let http = Arc::new(HttpClient::new(
            api_key,
            DEFAULT_BASE_URL,
            Duration::from_secs(DEFAULT_TIMEOUT_SECS),
        ));
        Self {
            http,
            webhook_secret: None,
        }
    }

    /// Create a client using the builder pattern.
    pub fn builder(api_key: &str) -> FlowBuilder {
        FlowBuilder {
            api_key: api_key.to_string(),
            base_url: DEFAULT_BASE_URL.to_string(),
            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
            webhook_secret: None,
        }
    }

    pub fn invoices(&self) -> InvoiceService {
        InvoiceService {
            http: self.http.clone(),
        }
    }

    pub fn payouts(&self) -> PayoutService {
        PayoutService {
            http: self.http.clone(),
        }
    }

    pub fn wallets(&self) -> WalletService {
        WalletService {
            http: self.http.clone(),
        }
    }

    pub fn swaps(&self) -> SwapService {
        SwapService {
            http: self.http.clone(),
        }
    }

    pub fn payment_links(&self) -> PaymentLinkService {
        PaymentLinkService {
            http: self.http.clone(),
        }
    }

    pub fn subscriptions(&self) -> SubscriptionService {
        SubscriptionService {
            http: self.http.clone(),
        }
    }

    pub fn teams(&self) -> TeamService {
        TeamService {
            http: self.http.clone(),
        }
    }

    pub fn webhooks(&self) -> Option<WebhookVerifier> {
        self.webhook_secret
            .as_ref()
            .map(|s| WebhookVerifier::new(s))
    }
}

/// Builder for configuring the Flow client.
pub struct FlowBuilder {
    api_key: String,
    base_url: String,
    timeout: Duration,
    webhook_secret: Option<String>,
}

impl FlowBuilder {
    pub fn base_url(mut self, url: &str) -> Self {
        self.base_url = url.to_string();
        self
    }

    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn webhook_secret(mut self, secret: &str) -> Self {
        self.webhook_secret = Some(secret.to_string());
        self
    }

    pub fn build(self) -> Flow {
        let http = Arc::new(HttpClient::new(&self.api_key, &self.base_url, self.timeout));
        Flow {
            http,
            webhook_secret: self.webhook_secret,
        }
    }
}