use std::{sync::Arc, time::Duration};
use hyper_util::{client::legacy::Client as HyperClient, rt::TokioExecutor};
use crate::Configuration;
const CRATE_VERSION: &str = env!("CARGO_PKG_VERSION");
#[derive(Debug, Clone)]
pub struct AfinspOptions {
pub debug: bool,
pub server_url: Option<String>,
pub timeout: Option<Duration>,
pub num_retries: Option<u32>,
pub retry_schedule: Option<Vec<Duration>>,
}
impl Default for AfinspOptions {
fn default() -> Self {
Self {
debug: false,
server_url: None,
timeout: Some(Duration::from_secs(15)),
num_retries: None,
retry_schedule: None,
}
}
}
#[derive(Clone)]
pub struct Afinsp {
pub(super) cfg: Arc<Configuration>,
}
impl Afinsp {
pub fn new(token: String, options: Option<AfinspOptions>) -> Self {
let options = options.unwrap_or_default();
let cfg = Arc::new(Configuration {
user_agent: Some(format!("afinsp-rust/{CRATE_VERSION}")),
client: HyperClient::builder(TokioExecutor::new()).build(crate::make_connector()),
timeout: options.timeout,
base_path: options
.server_url
.unwrap_or_else(|| "https://api.afinsp.com".to_string()),
bearer_access_token: Some(token),
num_retries: options.num_retries.unwrap_or(2),
retry_schedule: options.retry_schedule,
});
Self { cfg }
}
pub fn with_token(&self, token: String) -> Self {
let cfg = Arc::new(Configuration {
base_path: self.cfg.base_path.clone(),
user_agent: self.cfg.user_agent.clone(),
bearer_access_token: Some(token),
client: self.cfg.client.clone(),
timeout: self.cfg.timeout,
num_retries: self.cfg.num_retries,
retry_schedule: self.cfg.retry_schedule.clone(),
});
Self { cfg }
}
pub fn add_ons(&self) -> super::AddOns<'_> {
super::AddOns::new(&self.cfg)
}
pub fn batch_jobs(&self) -> super::BatchJobs<'_> {
super::BatchJobs::new(&self.cfg)
}
pub fn checkout_sessions(&self) -> super::CheckoutSessions<'_> {
super::CheckoutSessions::new(&self.cfg)
}
pub fn connect(&self) -> super::Connect<'_> {
super::Connect::new(&self.cfg)
}
pub fn coupons(&self) -> super::Coupons<'_> {
super::Coupons::new(&self.cfg)
}
pub fn credit_notes(&self) -> super::CreditNotes<'_> {
super::CreditNotes::new(&self.cfg)
}
pub fn custom_properties(&self) -> super::CustomProperties<'_> {
super::CustomProperties::new(&self.cfg)
}
pub fn customers(&self) -> super::Customers<'_> {
super::Customers::new(&self.cfg)
}
pub fn events(&self) -> super::Events<'_> {
super::Events::new(&self.cfg)
}
pub fn features(&self) -> super::Features<'_> {
super::Features::new(&self.cfg)
}
pub fn invoices(&self) -> super::Invoices<'_> {
super::Invoices::new(&self.cfg)
}
pub fn metrics(&self) -> super::Metrics<'_> {
super::Metrics::new(&self.cfg)
}
pub fn o_auth(&self) -> super::OAuth<'_> {
super::OAuth::new(&self.cfg)
}
pub fn o_auth_apps(&self) -> super::OAuthApps<'_> {
super::OAuthApps::new(&self.cfg)
}
pub fn plans(&self) -> super::Plans<'_> {
super::Plans::new(&self.cfg)
}
pub fn product_families(&self) -> super::ProductFamilies<'_> {
super::ProductFamilies::new(&self.cfg)
}
pub fn products(&self) -> super::Products<'_> {
super::Products::new(&self.cfg)
}
pub fn subscriptions(&self) -> super::Subscriptions<'_> {
super::Subscriptions::new(&self.cfg)
}
pub fn usage(&self) -> super::Usage<'_> {
super::Usage::new(&self.cfg)
}
}
#[cfg(test)]
mod tests {
use super::Afinsp;
#[test]
fn test_future_send_sync() {
fn require_send_sync<T: Send + Sync>(_: T) {}
let afinsp = Afinsp::new(String::new(), None);
let customer_api = afinsp.customers();
let fut = customer_api.list_customers(None);
require_send_sync(fut);
}
}