use std::sync::Arc;
use serde::Serialize;
use crate::error::Result;
use crate::http::HttpClient;
use crate::resources::{enc, to_value};
use crate::response::{Page, Response};
use crate::transport::Method;
use crate::types::params::SuppressionListParams;
pub struct Suppressions {
http: Arc<HttpClient>,
}
impl Suppressions {
pub(crate) fn new(http: Arc<HttpClient>) -> Self {
Self { http }
}
pub async fn list(&self, params: SuppressionListParams) -> Result<Page> {
self.http.list("/suppressions", Self::query(params)).await
}
pub async fn list_all(&self, params: SuppressionListParams) -> Result<Vec<Response>> {
let mut query = Self::query(params);
query.retain(|(key, _)| *key != "after");
self.http.list_all("/suppressions", query).await
}
pub async fn create(&self, body: impl Serialize) -> Result<Response> {
self.http
.request_object(
Method::Post,
"/suppressions",
Some(to_value(body)?),
false,
None,
)
.await
}
pub async fn get(&self, email: &str, topic: &str) -> Result<Response> {
self.http
.request_object(
Method::Get,
&format!("/suppressions/{}/{}", enc(email), enc(topic)),
None,
false,
None,
)
.await
}
pub async fn delete(&self, email: &str, topic: &str) -> Result<()> {
self.http
.request_empty(
Method::Delete,
&format!("/suppressions/{}/{}", enc(email), enc(topic)),
)
.await
}
pub async fn list_for_email(&self, email: &str) -> Result<Vec<Response>> {
self.http
.request_data(Method::Get, &format!("/suppressions/{}", enc(email)))
.await
}
pub async fn delete_for_email(&self, email: &str) -> Result<()> {
self.http
.request_empty(Method::Delete, &format!("/suppressions/{}", enc(email)))
.await
}
fn query(params: SuppressionListParams) -> Vec<(&'static str, Option<String>)> {
vec![
("limit", params.limit.map(|n| n.to_string())),
("after", params.after),
("email_contains", params.email_contains),
("topic", params.topic),
("reason", params.reason),
("origin", params.origin),
]
}
}