keplars 2.1.0

Official Rust SDK for Keplars Email API
Documentation
use crate::client::Keplars;
use crate::errors::KeplarsResult;
use crate::models::{
    ApiMessageResponse, ApiResponse, CreateDomainApiKeyRequest, CreateDomainData,
    DomainApiKeyData, DomainStatusData, DomainVerificationData,
};
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;

#[derive(Serialize)]
struct AddDomainRequest<'a> {
    domain: &'a str,
}

pub struct Domains<'a> {
    client: &'a Keplars,
}

impl<'a> Domains<'a> {
    pub fn new(client: &'a Keplars) -> Self {
        Self { client }
    }

    pub async fn add(&self, domain: &str) -> KeplarsResult<ApiResponse<CreateDomainData>> {
        let body = AddDomainRequest { domain };
        self.client.post("/api/v1/public/domains/add-domain", &body).await
    }

    pub async fn list(&self) -> KeplarsResult<HashMap<String, Value>> {
        self.client.get("/api/v1/public/domains/get-domains").await
    }

    pub async fn get_status(&self, domain_id: &str) -> KeplarsResult<ApiResponse<DomainStatusData>> {
        let path = format!(
            "/api/v1/public/domains/domain-status/{}",
            urlencoding::encode(domain_id)
        );
        self.client.get(&path).await
    }

    pub async fn verify(&self, domain_id: &str) -> KeplarsResult<ApiResponse<DomainVerificationData>> {
        let path = format!(
            "/api/v1/public/domains/verify-domain/{}",
            urlencoding::encode(domain_id)
        );
        self.client.post(&path, &serde_json::json!({})).await
    }

    pub async fn delete(&self, domain_id: &str) -> KeplarsResult<ApiMessageResponse> {
        let path = format!(
            "/api/v1/public/domains/delete-domain/{}",
            urlencoding::encode(domain_id)
        );
        self.client.delete(&path).await
    }

    pub async fn create_api_key(
        &self,
        domain_id: &str,
        name: Option<&str>,
    ) -> KeplarsResult<ApiResponse<DomainApiKeyData>> {
        let body = CreateDomainApiKeyRequest {
            domain_id: domain_id.to_string(),
            name: name.map(|s| s.to_string()),
        };
        self.client.post("/api/v1/public/domains/api-keys/create", &body).await
    }
}