use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, Page};
#[derive(Debug, Clone)]
pub struct Campaigns {
client: Sendry,
}
impl Campaigns {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn create(&self, params: CampaignParams) -> Result<Campaign, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/campaigns", &[], Some(¶ms)),
)
.await
}
pub async fn get(&self, id: &str) -> Result<Campaign, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
&format!("/v1/campaigns/{id}"),
&[],
None,
))
.await
}
pub async fn list(&self) -> Result<Page<Campaign>, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/campaigns", &[], None),
)
.await
}
pub async fn send(&self, id: &str) -> Result<Campaign, Error> {
self.client
.request(self.client.build::<()>(
Method::POST,
&format!("/v1/campaigns/{id}/send"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CampaignParams {
pub name: String,
pub subject: String,
pub from: String,
pub audience_id: String,
pub html: String,
pub text: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Campaign {
pub id: String,
pub name: String,
pub status: String,
pub sent_count: u32,
pub total_recipients: u32,
pub created_at: String,
}