Skip to main content

vylth_flow/
client.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use crate::http::HttpClient;
5use crate::resources::{
6    invoices::InvoiceService, payouts::PayoutService, swaps::SwapService, wallets::WalletService,
7    webhooks::WebhookVerifier, payment_links::PaymentLinkService, subscriptions::SubscriptionService,
8    teams::TeamService,
9};
10
11const DEFAULT_BASE_URL: &str = "https://flow.vylth.com/api/flow";
12const DEFAULT_TIMEOUT_SECS: u64 = 30;
13
14/// The Vylth Flow client.
15pub struct Flow {
16    http: Arc<HttpClient>,
17    webhook_secret: Option<String>,
18}
19
20impl Flow {
21    /// Create a new client with the given API key.
22    pub fn new(api_key: &str) -> Self {
23        let http = Arc::new(HttpClient::new(
24            api_key,
25            DEFAULT_BASE_URL,
26            Duration::from_secs(DEFAULT_TIMEOUT_SECS),
27        ));
28        Self {
29            http,
30            webhook_secret: None,
31        }
32    }
33
34    /// Create a client using the builder pattern.
35    pub fn builder(api_key: &str) -> FlowBuilder {
36        FlowBuilder {
37            api_key: api_key.to_string(),
38            api_secret: None,
39            base_url: DEFAULT_BASE_URL.to_string(),
40            timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
41            webhook_secret: None,
42        }
43    }
44
45    pub fn invoices(&self) -> InvoiceService {
46        InvoiceService {
47            http: self.http.clone(),
48        }
49    }
50
51    pub fn payouts(&self) -> PayoutService {
52        PayoutService {
53            http: self.http.clone(),
54        }
55    }
56
57    pub fn wallets(&self) -> WalletService {
58        WalletService {
59            http: self.http.clone(),
60        }
61    }
62
63    pub fn swaps(&self) -> SwapService {
64        SwapService {
65            http: self.http.clone(),
66        }
67    }
68
69    pub fn payment_links(&self) -> PaymentLinkService {
70        PaymentLinkService {
71            http: self.http.clone(),
72        }
73    }
74
75    pub fn subscriptions(&self) -> SubscriptionService {
76        SubscriptionService {
77            http: self.http.clone(),
78        }
79    }
80
81    pub fn teams(&self) -> TeamService {
82        TeamService {
83            http: self.http.clone(),
84        }
85    }
86
87    pub fn webhooks(&self) -> Option<WebhookVerifier> {
88        self.webhook_secret
89            .as_ref()
90            .map(|s| WebhookVerifier::new(s))
91    }
92}
93
94/// Builder for configuring the Flow client.
95pub struct FlowBuilder {
96    api_key: String,
97    api_secret: Option<String>,
98    base_url: String,
99    timeout: Duration,
100    webhook_secret: Option<String>,
101}
102
103impl FlowBuilder {
104    pub fn base_url(mut self, url: &str) -> Self {
105        self.base_url = url.to_string();
106        self
107    }
108
109    pub fn timeout(mut self, timeout: Duration) -> Self {
110        self.timeout = timeout;
111        self
112    }
113
114    pub fn api_secret(mut self, secret: &str) -> Self {
115        self.api_secret = Some(secret.to_string());
116        self
117    }
118
119    pub fn webhook_secret(mut self, secret: &str) -> Self {
120        self.webhook_secret = Some(secret.to_string());
121        self
122    }
123
124    pub fn build(self) -> Flow {
125        let mut client = HttpClient::new(&self.api_key, &self.base_url, self.timeout);
126        if let Some(ref s) = self.api_secret {
127            client = client.with_secret(s);
128        }
129        let http = Arc::new(client);
130        Flow {
131            http,
132            webhook_secret: self.webhook_secret,
133        }
134    }
135}