Skip to main content

usesend_api/
client.rs

1use crate::config::{Config, SharedConfig};
2use crate::services::*;
3
4/// Low-level API client for the useSend email service.
5///
6/// Uses a sub-service pattern — access each API resource via its field:
7///
8/// ```no_run
9/// # async fn example() -> usesend_api::ApiResult<()> {
10/// let client = usesend_api::UseSendApiClient::new("us_api_key");
11/// let domains = client.domains.list().await?;
12/// # Ok(())
13/// # }
14/// ```
15#[derive(Debug, Clone)]
16pub struct UseSendApiClient {
17    pub domains: DomainsSvc,
18    pub emails: EmailsSvc,
19    pub contact_books: ContactBooksSvc,
20    pub contacts: ContactsSvc,
21    pub campaigns: CampaignsSvc,
22}
23
24impl UseSendApiClient {
25    pub fn new(api_key: impl Into<String>) -> Self {
26        Self::from_config(Config::new(api_key))
27    }
28
29    pub fn with_base_url(api_key: impl Into<String>, base_url: impl Into<String>) -> Self {
30        Self::from_config(Config::new(api_key).with_base_url(base_url))
31    }
32
33    pub fn with_client(api_key: impl Into<String>, client: reqwest::Client) -> Self {
34        Self::from_config(Config::new(api_key).with_client(client))
35    }
36
37    pub fn from_config(config: Config) -> Self {
38        let shared = SharedConfig::new(config);
39        Self {
40            domains: DomainsSvc(shared.clone()),
41            emails: EmailsSvc(shared.clone()),
42            contact_books: ContactBooksSvc(shared.clone()),
43            contacts: ContactsSvc(shared.clone()),
44            campaigns: CampaignsSvc(shared),
45        }
46    }
47}