use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use crate::error::{OpensmsError, Result};
use crate::http::{
HttpClient, ReqwestTransport, Sleeper, TokioSleeper, Transport, DEFAULT_BASE_URL,
};
use crate::models::Page;
use crate::pagination::{paginate, Paginator};
use crate::resources::{
Analytics, Batches, Compliance, ContactGroups, Contacts, Countries, Inbound, Lookups, Messages,
Numbers, Otp, Pricing, Sandbox, SenderIds, Suppressions, Templates, Wallet, Webhooks,
};
const DEFAULT_TIMEOUT_SECS: u64 = 30;
const DEFAULT_MAX_RETRIES: u32 = 2;
const MIN_SECRET_LEN: usize = 12;
pub struct ClientBuilder {
api_key: String,
base_url: String,
timeout: Duration,
max_retries: u32,
transport: Option<Arc<dyn Transport>>,
sleeper: Option<Arc<dyn Sleeper>>,
}
impl ClientBuilder {
pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
self.base_url = base_url.into();
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.timeout = timeout;
self
}
pub fn max_retries(mut self, max_retries: u32) -> Self {
self.max_retries = max_retries;
self
}
pub fn transport(mut self, transport: Arc<dyn Transport>) -> Self {
self.transport = Some(transport);
self
}
pub fn sleeper(mut self, sleeper: Arc<dyn Sleeper>) -> Self {
self.sleeper = Some(sleeper);
self
}
pub fn build(self) -> Result<Client> {
let environment = environment_of(&self.api_key).ok_or_else(|| {
OpensmsError::invalid_argument(
"api_key must start with sk_test_ or sk_live_ followed by more than 12 characters",
)
})?;
let transport: Arc<dyn Transport> = match self.transport {
Some(t) => t,
None => Arc::new(ReqwestTransport::new()?),
};
let sleeper = self.sleeper.unwrap_or_else(|| Arc::new(TokioSleeper));
let http = HttpClient::new(
self.api_key,
self.base_url,
self.timeout,
self.max_retries,
transport,
sleeper,
);
Ok(Client {
environment,
messages: Messages::new(http.clone()),
batches: Batches::new(http.clone()),
otp: Otp::new(http.clone()),
lookups: Lookups::new(http.clone()),
contacts: Contacts::new(http.clone()),
contact_groups: ContactGroups::new(http.clone()),
templates: Templates::new(http.clone()),
webhooks: Webhooks::new(http.clone()),
inbound: Inbound::new(http.clone()),
numbers: Numbers::new(http.clone()),
sender_ids: SenderIds::new(http.clone()),
suppressions: Suppressions::new(http.clone()),
compliance: Compliance::new(http.clone()),
wallet: Wallet::new(http.clone()),
pricing: Pricing::new(http.clone()),
analytics: Analytics::new(http.clone()),
sandbox: Sandbox::new(http.clone()),
countries: Countries::new(http.clone()),
http,
})
}
}
fn environment_of(key: &str) -> Option<&'static str> {
let (env, rest) = match key.strip_prefix("sk_test_") {
Some(rest) => ("sandbox", rest),
None => ("live", key.strip_prefix("sk_live_")?),
};
(rest.len() > MIN_SECRET_LEN).then_some(env)
}
#[derive(Debug, Clone)]
pub struct Client {
http: HttpClient,
environment: &'static str,
messages: Messages,
batches: Batches,
otp: Otp,
lookups: Lookups,
contacts: Contacts,
contact_groups: ContactGroups,
templates: Templates,
webhooks: Webhooks,
inbound: Inbound,
numbers: Numbers,
sender_ids: SenderIds,
suppressions: Suppressions,
compliance: Compliance,
wallet: Wallet,
pricing: Pricing,
analytics: Analytics,
sandbox: Sandbox,
countries: Countries,
}
impl Client {
pub fn new(api_key: impl Into<String>) -> Result<Self> {
Self::builder(api_key).build()
}
pub fn builder(api_key: impl Into<String>) -> ClientBuilder {
ClientBuilder {
api_key: api_key.into(),
base_url: DEFAULT_BASE_URL.to_string(),
timeout: Duration::from_secs(DEFAULT_TIMEOUT_SECS),
max_retries: DEFAULT_MAX_RETRIES,
transport: None,
sleeper: None,
}
}
pub fn environment(&self) -> &'static str {
self.environment
}
pub fn base_url(&self) -> &str {
self.http.base_url()
}
pub fn paginate<T, F, Fut>(&self, fetch: F) -> Paginator<T, F>
where
F: FnMut(Option<String>) -> Fut,
Fut: Future<Output = Result<Page<T>>>,
{
paginate(fetch)
}
pub fn messages(&self) -> &Messages {
&self.messages
}
pub fn batches(&self) -> &Batches {
&self.batches
}
pub fn otp(&self) -> &Otp {
&self.otp
}
pub fn lookups(&self) -> &Lookups {
&self.lookups
}
pub fn contacts(&self) -> &Contacts {
&self.contacts
}
pub fn contact_groups(&self) -> &ContactGroups {
&self.contact_groups
}
pub fn templates(&self) -> &Templates {
&self.templates
}
pub fn webhooks(&self) -> &Webhooks {
&self.webhooks
}
pub fn inbound(&self) -> &Inbound {
&self.inbound
}
pub fn numbers(&self) -> &Numbers {
&self.numbers
}
pub fn sender_ids(&self) -> &SenderIds {
&self.sender_ids
}
pub fn suppressions(&self) -> &Suppressions {
&self.suppressions
}
pub fn compliance(&self) -> &Compliance {
&self.compliance
}
pub fn wallet(&self) -> &Wallet {
&self.wallet
}
pub fn pricing(&self) -> &Pricing {
&self.pricing
}
pub fn analytics(&self) -> &Analytics {
&self.analytics
}
pub fn sandbox(&self) -> &Sandbox {
&self.sandbox
}
pub fn countries(&self) -> &Countries {
&self.countries
}
}