use crate::client::Unosend;
use crate::error::UnosendError;
use serde::{Deserialize, Serialize};
pub struct ContactsApi {
client: Unosend,
}
impl ContactsApi {
pub(crate) fn new(client: Unosend) -> Self {
Self { client }
}
pub async fn create(&self, request: CreateContactRequest) -> Result<Contact, UnosendError> {
self.client.post("/contacts", &request).await
}
pub async fn list(&self, audience_id: &str) -> Result<ContactList, UnosendError> {
self.client
.get(&format!("/audiences/{}/contacts", audience_id))
.await
}
pub async fn get(&self, contact_id: &str) -> Result<Contact, UnosendError> {
self.client.get(&format!("/contacts/{}", contact_id)).await
}
pub async fn delete(&self, contact_id: &str) -> Result<(), UnosendError> {
self.client.delete(&format!("/contacts/{}", contact_id)).await
}
}
#[derive(Debug, Clone, Serialize)]
pub struct CreateContactRequest {
pub audience_id: String,
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 unsubscribed: Option<bool>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Contact {
pub id: String,
pub email: String,
pub first_name: Option<String>,
pub last_name: Option<String>,
pub unsubscribed: bool,
pub created_at: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct ContactList {
pub data: Vec<Contact>,
}