keplars 1.11.4

Official Rust SDK for Keplars Email API
Documentation
use crate::client::Keplars;
use crate::errors::KeplarsResult;
use crate::models::{
    AddContactRequest, ApiDataResponse, ApiListResponse, ApiMessageResponse, ApiResponse,
    Contact, UpdateContactRequest,
};

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

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

    pub async fn add(&self, request: AddContactRequest) -> KeplarsResult<ApiResponse<Contact>> {
        self.client.post("/api/v1/public/contacts/add-contact", &request).await
    }

    pub async fn get(&self, email: &str) -> KeplarsResult<ApiDataResponse<Contact>> {
        let path = format!(
            "/api/v1/public/contacts/get-contact?email={}",
            urlencoding::encode(email)
        );
        self.client.get(&path).await
    }

    pub async fn list(
        &self,
        audience_id: Option<&str>,
        page: Option<u32>,
        limit: Option<u32>,
    ) -> KeplarsResult<ApiListResponse<Contact>> {
        let mut params = vec![];
        if let Some(id) = audience_id {
            params.push(format!("audience_id={}", urlencoding::encode(id)));
        }
        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/contacts/get-contacts{}", query);
        self.client.get(&path).await
    }

    pub async fn update(
        &self,
        email: &str,
        request: UpdateContactRequest,
    ) -> KeplarsResult<ApiResponse<Contact>> {
        let path = format!(
            "/api/v1/public/contacts/update-contact?email={}",
            urlencoding::encode(email)
        );
        self.client.patch(&path, &request).await
    }

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