use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, DeleteResponse, Page, PaginationParams};
#[derive(Debug, Clone)]
pub struct Suppression {
client: Sendry,
}
impl Suppression {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn list(&self, params: PaginationParams) -> Result<Page<SuppressionEntry>, Error> {
let q = params.to_query();
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/suppression", &q, None),
)
.await
}
pub async fn add(&self, params: AddSuppression) -> Result<SuppressionEntry, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/suppression", &[], Some(¶ms)),
)
.await
}
pub async fn remove(&self, email: &str) -> Result<DeleteResponse, Error> {
let encoded = urlencode(email);
self.client
.request(self.client.build::<()>(
Method::DELETE,
&format!("/v1/suppression/{encoded}"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct AddSuppression {
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct SuppressionEntry {
pub email: String,
pub reason: String,
pub created_at: String,
}
fn urlencode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for b in s.bytes() {
match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
out.push(b as char);
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}