use std::sync::Arc;
use crate::config::Config;
use crate::domains::DomainsSvc;
use crate::emails::EmailsSvc;
use crate::projects::ProjectsSvc;
use crate::templates::TemplatesSvc;
use crate::webhooks::WebhooksSvc;
#[derive(Clone, Debug)]
pub struct Lettr {
pub emails: EmailsSvc,
pub domains: DomainsSvc,
pub webhooks: WebhooksSvc,
pub templates: TemplatesSvc,
pub projects: ProjectsSvc,
config: Arc<Config>,
}
impl Lettr {
#[must_use]
pub fn new(api_key: &str) -> Self {
let config = Arc::new(Config::new(api_key));
Self::from_config(config)
}
#[must_use]
pub fn with_base_url(api_key: &str, base_url: &str) -> Self {
let config = Arc::new(Config::with_base_url(api_key, base_url));
Self::from_config(config)
}
fn from_config(config: Arc<Config>) -> Self {
Self {
emails: EmailsSvc(Arc::clone(&config)),
domains: DomainsSvc(Arc::clone(&config)),
webhooks: WebhooksSvc(Arc::clone(&config)),
templates: TemplatesSvc(Arc::clone(&config)),
projects: ProjectsSvc(Arc::clone(&config)),
config,
}
}
#[must_use]
pub fn from_env() -> Self {
let api_key =
std::env::var("LETTR_API_KEY").expect("LETTR_API_KEY environment variable not set");
Self::new(&api_key)
}
#[maybe_async::maybe_async]
pub async fn health(&self) -> crate::Result<HealthResponse> {
let request = self.config.build(reqwest::Method::GET, "/health");
let response = self.config.send(request).await?;
let body = response.json::<HealthResponse>().await?;
Ok(body)
}
#[maybe_async::maybe_async]
pub async fn auth_check(&self) -> crate::Result<AuthCheckResponse> {
let request = self.config.build(reqwest::Method::GET, "/auth/check");
let response = self.config.send(request).await?;
let body = response.json::<AuthCheckResponse>().await?;
Ok(body)
}
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct HealthResponse {
pub message: String,
pub data: HealthData,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct HealthData {
pub status: String,
pub timestamp: String,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct AuthCheckResponse {
pub message: String,
pub data: AuthCheckData,
}
#[derive(Debug, Clone, serde::Deserialize)]
pub struct AuthCheckData {
pub team_id: i64,
pub timestamp: String,
}