emailit 2.0.3

The official Rust SDK for the Emailit Email API
Documentation
//! Sending domain management service.

use std::sync::Arc;

use crate::client::BaseClient;
use crate::collection::Collection;
use crate::error::Error;
use crate::types::{CreateDomainParams, Domain, ListParams, UpdateDomainParams};

/// Service for managing sending domains.
///
/// Accessed via [`Emailit::domains`](crate::Emailit::domains).
pub struct DomainService {
    pub(crate) client: Arc<BaseClient>,
}

impl DomainService {
    /// Adds a new sending domain.
    ///
    /// `POST /v2/domains`
    pub async fn create(&self, params: CreateDomainParams) -> Result<Domain, Error> {
        self.client
            .request("POST", "/v2/domains", to_body(&params), None)
            .await
    }

    /// Retrieves a domain by ID.
    ///
    /// `GET /v2/domains/:id`
    pub async fn get(&self, id: &str) -> Result<Domain, Error> {
        let path = format!("/v2/domains/{}", urlencoding::encode(id));
        self.client.request("GET", &path, None, None).await
    }

    /// Triggers DNS verification for a domain.
    ///
    /// `POST /v2/domains/:id/verify`
    pub async fn verify(&self, id: &str) -> Result<Domain, Error> {
        let path = format!("/v2/domains/{}/verify", urlencoding::encode(id));
        self.client.request("POST", &path, None, None).await
    }

    /// Updates a domain's settings (e.g. tracking).
    ///
    /// `PATCH /v2/domains/:id`
    pub async fn update(&self, id: &str, params: UpdateDomainParams) -> Result<Domain, Error> {
        let path = format!("/v2/domains/{}", urlencoding::encode(id));
        self.client
            .request("PATCH", &path, to_body(&params), None)
            .await
    }

    /// Lists domains with optional pagination.
    ///
    /// `GET /v2/domains`
    pub async fn list(&self, params: Option<ListParams>) -> Result<Collection<Domain>, Error> {
        let query = params.map(|p| {
            let mut q = Vec::new();
            if let Some(page) = p.page {
                q.push(("page", page.to_string()));
            }
            if let Some(limit) = p.limit {
                q.push(("limit", limit.to_string()));
            }
            q
        });
        self.client
            .request::<Collection<Domain>>("GET", "/v2/domains", None, query.as_deref())
            .await
    }

    /// Deletes a domain by ID.
    ///
    /// `DELETE /v2/domains/:id`
    pub async fn delete(&self, id: &str) -> Result<Domain, Error> {
        let path = format!("/v2/domains/{}", urlencoding::encode(id));
        self.client.request("DELETE", &path, None, None).await
    }
}

fn to_body(v: &impl serde::Serialize) -> Option<serde_json::Value> {
    serde_json::to_value(v).ok()
}