keplars 2.1.0

Official Rust SDK for Keplars Email API
Documentation
use crate::client::Keplars;
use crate::errors::KeplarsResult;
use crate::models::{ApiDataResponse, ApiListResponse, ApiMessageResponse, Automation};
use serde::Serialize;

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

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

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

    pub async fn list(
        &self,
        page: Option<u32>,
        limit: Option<u32>,
    ) -> KeplarsResult<ApiListResponse<Automation>> {
        let mut params = vec![];
        if let Some(p) = page {
            params.push(format!("page={}", p));
        }
        if let Some(l) = limit {
            params.push(format!("limit={}", l));
        }
        let query = if params.is_empty() {
            String::new()
        } else {
            format!("?{}", params.join("&"))
        };
        let path = format!("/api/v1/public/automations/get-all{}", query);
        self.client.get(&path).await
    }

    pub async fn get(&self, id: &str) -> KeplarsResult<ApiDataResponse<Automation>> {
        let path = format!(
            "/api/v1/public/automations/get-automation/{}",
            urlencoding::encode(id)
        );
        self.client.get(&path).await
    }

    pub async fn enroll(&self, id: &str, email: &str) -> KeplarsResult<ApiMessageResponse> {
        let path = format!(
            "/api/v1/public/automations/add-automation/{}/enroll",
            urlencoding::encode(id)
        );
        let body = EnrollRequest { email };
        self.client.post(&path, &body).await
    }

    pub async fn unenroll(&self, id: &str, email: &str) -> KeplarsResult<ApiMessageResponse> {
        let path = format!(
            "/api/v1/public/automations/delete-automation/{}/subscribers",
            urlencoding::encode(id)
        );
        let body = EnrollRequest { email };
        self.client.delete_with_body(&path, &body).await
    }
}