use std::collections::HashMap;
use crate::client::Sentd;
use crate::error::Error;
use crate::types::*;
pub struct Emails {
client: Sentd,
}
impl Emails {
pub(crate) fn new(client: Sentd) -> Self {
Self { client }
}
pub async fn send(&self, request: SendEmailRequest) -> Result<SendEmailResponse, Error> {
self.client.post("/api/send", &request).await
}
pub async fn list(&self, params: Option<ListEmailsParams>) -> Result<ListEmailsResponse, Error> {
let mut query = String::from("/api/emails");
if let Some(p) = params {
let mut parts = vec![];
if let Some(limit) = p.limit {
parts.push(format!("limit={}", limit));
}
if let Some(offset) = p.offset {
parts.push(format!("offset={}", offset));
}
if let Some(status) = p.status {
parts.push(format!("status={}", status));
}
if let Some(from) = p.from {
parts.push(format!("from={}", from));
}
if let Some(to) = p.to {
parts.push(format!("to={}", to));
}
if !parts.is_empty() {
query = format!("{}?{}", query, parts.join("&"));
}
}
self.client.get(&query).await
}
pub async fn get(&self, id: &str) -> Result<GetEmailResponse, Error> {
self.client.get(&format!("/api/emails/{}", id)).await
}
pub async fn cancel(&self, id: &str) -> Result<SuccessResponse, Error> {
self.client.delete(&format!("/api/emails/{}", id)).await
}
pub async fn reschedule(&self, id: &str, send_at: &str, timezone: Option<&str>) -> Result<GetEmailResponse, Error> {
let mut body = HashMap::new();
body.insert("send_at", send_at);
if let Some(tz) = timezone {
body.insert("send_at_timezone", tz);
}
self.client.patch(&format!("/api/emails/{}", id), &body).await
}
pub async fn resend(&self, id: &str) -> Result<SendEmailResponse, Error> {
self.client.post(&format!("/api/emails/{}/resend", id), &()).await
}
}
#[derive(Debug, Default)]
pub struct ListEmailsParams {
pub limit: Option<i32>,
pub offset: Option<i32>,
pub status: Option<String>,
pub from: Option<String>,
pub to: Option<String>,
}