use reqwest::Method;
use serde::{Deserialize, Serialize};
use crate::{client::Sendry, error::Error, Page};
#[derive(Debug, Clone)]
pub struct Contacts {
client: Sendry,
}
impl Contacts {
pub(crate) fn new(client: Sendry) -> Self {
Self { client }
}
pub async fn upsert(&self, params: ContactParams) -> Result<Contact, Error> {
self.client
.request(
self.client
.build(Method::POST, "/v1/contacts", &[], Some(¶ms)),
)
.await
}
pub async fn get(&self, id: &str) -> Result<Contact, Error> {
self.client
.request(
self.client
.build::<()>(Method::GET, &format!("/v1/contacts/{id}"), &[], None),
)
.await
}
pub async fn list(&self, audience_id: Option<&str>) -> Result<Page<Contact>, Error> {
let q: Vec<(&str, String)> = audience_id
.map(|a| vec![("audience_id", a.to_string())])
.unwrap_or_default();
self.client
.request(
self.client
.build::<()>(Method::GET, "/v1/contacts", &q, None),
)
.await
}
pub async fn delete(&self, id: &str) -> Result<(), Error> {
self.client
.request_unit(self.client.build::<()>(
Method::DELETE,
&format!("/v1/contacts/{id}"),
&[],
None,
))
.await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct ContactParams {
pub email: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub first_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub last_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub audience_id: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Contact {
pub id: String,
pub email: String,
pub unsubscribed: bool,
pub created_at: String,
}