use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, Page};
#[derive(Debug, Clone)]
pub struct Emails {
client: Sendry,
}
impl Emails {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn send(&self, params: SendEmail) -> Result<EmailResponse, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/emails", &[], Some(¶ms)),
)
.await
}
pub async fn send_batch(&self, params: BatchEmail) -> Result<BatchResponse, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/emails/batch", &[], Some(¶ms)),
)
.await
}
pub async fn get(&self, id: &str) -> Result<Email, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, &format!("/v1/emails/{id}"), &[], None),
)
.await
}
pub async fn list(&self, query: ListEmails) -> Result<Page<Email>, Error> {
let q = query.to_query();
self.client
.request(self.client.build::<()>(Method::GET, "/v1/emails", &q, None))
.await
}
pub async fn send_marketing(
&self,
params: SendMarketingEmail,
) -> Result<EmailResponse, Error> {
self.client
.request(self.client.build(
Method::POST,
"/v1/emails/marketing",
&[],
Some(¶ms),
))
.await
}
pub async fn cancel(&self, id: &str) -> Result<CancelEmailResponse, Error> {
self.client
.request(self.client.build::<()>(
Method::POST,
&format!("/v1/emails/{id}/cancel"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SendMarketingEmail {
pub from: String,
pub to: Vec<String>,
pub subject: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub html: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "reply_to")]
pub reply_to: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Tag>>,
pub unsubscribe_url: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub list_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub scheduled_at: Option<String>,
#[serde(skip_serializing_if = "Option::is_none", rename = "template_id")]
pub template_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct CancelEmailResponse {
pub id: String,
pub status: String,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct SendEmail {
pub from: String,
pub to: Vec<String>,
pub subject: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub html: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cc: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub bcc: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none", rename = "reply_to")]
pub reply_to: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub headers: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<Tag>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub attachments: Option<Vec<Attachment>>,
#[serde(skip_serializing_if = "Option::is_none", rename = "template_id")]
pub template_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub variables: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tag {
pub name: String,
pub value: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Attachment {
pub filename: String,
pub content: String,
#[serde(rename = "contentType")]
pub content_type: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct EmailResponse {
pub id: String,
pub status: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Email {
pub id: String,
pub from: String,
pub to: Vec<String>,
pub subject: String,
pub status: String,
pub created_at: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchEmail {
pub from: String,
pub emails: Vec<BatchEmailItem>,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct BatchEmailItem {
pub to: String,
pub subject: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub html: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchResponse {
pub sent: u32,
pub failed: Vec<BatchFailure>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchFailure {
pub to: String,
pub error: String,
}
#[derive(Debug, Clone, Default)]
pub struct ListEmails {
pub limit: Option<u32>,
pub cursor: Option<String>,
pub status: Option<String>,
}
impl ListEmails {
fn to_query(&self) -> Vec<(&'static str, String)> {
let mut q = Vec::new();
if let Some(l) = self.limit {
q.push(("limit", l.to_string()));
}
if let Some(c) = &self.cursor {
q.push(("cursor", c.clone()));
}
if let Some(s) = &self.status {
q.push(("status", s.clone()));
}
q
}
}