use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, DeleteResponse, Page};
#[derive(Debug, Clone)]
pub struct Unsubscribes {
client: Sendry,
}
impl Unsubscribes {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn list(&self, params: ListUnsubscribes) -> Result<Page<UnsubscribeEntry>, Error> {
let q = params.to_query();
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/unsubscribes", &q, None),
)
.await
}
pub async fn create(&self, params: CreateUnsubscribe) -> Result<UnsubscribeEntry, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/unsubscribes", &[], Some(¶ms)),
)
.await
}
pub async fn create_batch(
&self,
params: BatchUnsubscribe,
) -> Result<BatchUnsubscribeResponse, Error> {
self.client
.request(self.client.build(
Method::POST,
"/v1/unsubscribes/batch",
&[],
Some(¶ms),
))
.await
}
pub async fn get(&self, id: &str) -> Result<UnsubscribeEntry, Error> {
self.client
.request(self.client.build::<()>(
Method::GET,
&format!("/v1/unsubscribes/{id}"),
&[],
None,
))
.await
}
pub async fn remove(&self, id: &str) -> Result<DeleteResponse, Error> {
self.client
.request(self.client.build::<()>(
Method::DELETE,
&format!("/v1/unsubscribes/{id}"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Default)]
pub struct ListUnsubscribes {
pub limit: Option<u32>,
pub cursor: Option<String>,
pub email: Option<String>,
pub list_id: Option<String>,
}
impl ListUnsubscribes {
fn to_query(&self) -> Vec<(&'static str, String)> {
let mut q = Vec::new();
if let Some(v) = self.limit {
q.push(("limit", v.to_string()));
}
if let Some(v) = &self.cursor {
q.push(("cursor", v.clone()));
}
if let Some(v) = &self.email {
q.push(("email", v.clone()));
}
if let Some(v) = &self.list_id {
q.push(("list_id", v.clone()));
}
q
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateUnsubscribe {
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub list_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, Clone, Serialize)]
pub struct BatchUnsubscribe {
pub emails: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub list_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BatchUnsubscribeResponse {
pub inserted: u32,
}
#[derive(Debug, Clone, Deserialize)]
pub struct UnsubscribeEntry {
pub id: String,
pub email: String,
pub list_id: Option<String>,
pub reason: Option<String>,
pub created_at: String,
}