sentd 1.0.0

Official Rust SDK for the SENTD Email API
Documentation
use serde::Serialize;
use std::collections::HashMap;

use crate::client::Sentd;
use crate::error::Error;
use crate::types::*;

/// Templates API
pub struct Templates {
    client: Sentd,
}

impl Templates {
    pub(crate) fn new(client: Sentd) -> Self {
        Self { client }
    }

    /// List all templates
    pub async fn list(&self) -> Result<ListTemplatesResponse, Error> {
        self.client.get("/api/templates").await
    }

    /// Get a template by ID
    pub async fn get(&self, id: &str) -> Result<TemplateResponse, Error> {
        self.client.get(&format!("/api/templates/{}", id)).await
    }

    /// Create a new template
    pub async fn create(&self, request: CreateTemplateRequest) -> Result<TemplateResponse, Error> {
        self.client.post("/api/templates", &request).await
    }

    /// Update a template
    pub async fn update(&self, id: &str, request: UpdateTemplateRequest) -> Result<TemplateResponse, Error> {
        self.client.put(&format!("/api/templates/{}", id), &request).await
    }

    /// Delete a template
    pub async fn delete(&self, id: &str) -> Result<SuccessResponse, Error> {
        self.client.delete(&format!("/api/templates/{}", id)).await
    }

    /// Preview a template with data
    pub async fn preview(&self, id: &str, data: HashMap<String, serde_json::Value>) -> Result<PreviewTemplateResponse, Error> {
        #[derive(Serialize)]
        struct PreviewRequest {
            data: HashMap<String, serde_json::Value>,
        }

        self.client.post(&format!("/api/templates/{}/preview", id), &PreviewRequest { data }).await
    }

    /// Duplicate a template
    pub async fn duplicate(&self, id: &str, name: Option<&str>) -> Result<TemplateResponse, Error> {
        let body = name.map(|n| serde_json::json!({ "name": n })).unwrap_or_default();
        self.client.post(&format!("/api/templates/{}/duplicate", id), &body).await
    }
}

/// Request to update a template
#[derive(Debug, Clone, Serialize, Default)]
pub struct UpdateTemplateRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub subject_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub html_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub variables: Option<Vec<TemplateVariable>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_from: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reply_to: Option<String>,
}