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;
pub struct Flow {
http: Arc<HttpClient>,
webhook_secret: Option<String>,
}
impl Flow {
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,
}
}
pub fn builder(api_key: &str) -> FlowBuilder {
FlowBuilder {
api_key: api_key.to_string(),
api_secret: None,
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))
}
}
pub struct FlowBuilder {
api_key: String,
api_secret: Option<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 api_secret(mut self, secret: &str) -> Self {
self.api_secret = Some(secret.to_string());
self
}
pub fn webhook_secret(mut self, secret: &str) -> Self {
self.webhook_secret = Some(secret.to_string());
self
}
pub fn build(self) -> Flow {
let mut client = HttpClient::new(&self.api_key, &self.base_url, self.timeout);
if let Some(ref s) = self.api_secret {
client = client.with_secret(s);
}
let http = Arc::new(client);
Flow {
http,
webhook_secret: self.webhook_secret,
}
}
}