use crate::error::{AfricasTalkingError, Result};
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Environment {
Sandbox,
Production,
}
impl Environment {
pub fn base_url(&self) -> &'static str {
match self {
Environment::Sandbox => "https://api.sandbox.africastalking.com",
Environment::Production => "https://api.africastalking.com",
}
}
}
#[derive(Debug, Clone)]
pub struct Config {
pub api_key: String,
pub username: String,
pub environment: Environment,
pub timeout: Duration,
pub max_retries: u32,
pub user_agent: Option<String>,
}
impl Config {
pub fn new<S: Into<String>>(api_key: S, username: S) -> Self {
Self {
api_key: api_key.into(),
username: username.into(),
environment: Environment::Sandbox,
timeout: Duration::from_secs(30),
max_retries: 3,
user_agent: None,
}
}
pub fn environment(mut self, env: Environment) -> Self {
self.environment = env;
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 user_agent<S: Into<String>>(mut self, user_agent: S) -> Self {
self.user_agent = Some(user_agent.into());
self
}
pub fn validate(&self) -> Result<()> {
if self.api_key.is_empty() {
return Err(AfricasTalkingError::config("API key cannot be empty"));
}
if self.username.is_empty() {
return Err(AfricasTalkingError::config("Username cannot be empty"));
}
if self.timeout.as_secs() == 0 {
return Err(AfricasTalkingError::config(
"Timeout must be greater than 0",
));
}
Ok(())
}
}