emailit 2.0.3

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

use std::sync::Arc;

use crate::client::BaseClient;
use crate::collection::Collection;
use crate::error::Error;
use crate::types::{CreateSuppressionParams, ListParams, Suppression, UpdateSuppressionParams};

/// Service for managing email suppressions (bounces, complaints, manual blocks).
///
/// Accessed via [`Emailit::suppressions`](crate::Emailit::suppressions).
pub struct SuppressionService {
    pub(crate) client: Arc<BaseClient>,
}

impl SuppressionService {
    /// Creates a new suppression entry.
    ///
    /// `POST /v2/suppressions`
    pub async fn create(&self, params: CreateSuppressionParams) -> Result<Suppression, Error> {
        self.client
            .request("POST", "/v2/suppressions", to_body(&params), None)
            .await
    }

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

    /// Updates a suppression by ID.
    ///
    /// `POST /v2/suppressions/:id`
    pub async fn update(
        &self,
        id: &str,
        params: UpdateSuppressionParams,
    ) -> Result<Suppression, Error> {
        let path = format!("/v2/suppressions/{}", urlencoding::encode(id));
        self.client
            .request("POST", &path, to_body(&params), None)
            .await
    }

    /// Lists suppressions with optional pagination.
    ///
    /// `GET /v2/suppressions`
    pub async fn list(&self, params: Option<ListParams>) -> Result<Collection<Suppression>, 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<Suppression>>("GET", "/v2/suppressions", None, query.as_deref())
            .await
    }

    /// Deletes a suppression by ID.
    ///
    /// `DELETE /v2/suppressions/:id`
    pub async fn delete(&self, id: &str) -> Result<Suppression, Error> {
        let path = format!("/v2/suppressions/{}", 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()
}