use crate::client::Client;
use crate::error::Error;
use crate::types::{
AddContactsRequest, Audience, Contact, CreateAudienceRequest, EmptyResponse, ListParams,
PaginatedList, UpdateAudienceRequest,
};
pub struct Audiences<'a> {
client: &'a Client,
}
impl<'a> Audiences<'a> {
pub fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn list(
&self,
params: Option<ListParams>,
) -> Result<PaginatedList<Audience>, Error> {
let params = params.unwrap_or_default();
let query_params = params.to_query_params();
self.client
.get_with_params("/audiences", &query_params)
.await
}
pub async fn get(&self, id: &str) -> Result<Audience, Error> {
self.client.get(&format!("/audiences/{}", id)).await
}
pub async fn create(&self, data: CreateAudienceRequest) -> Result<Audience, Error> {
self.client.post("/audiences", &data).await
}
pub async fn update(&self, id: &str, data: UpdateAudienceRequest) -> Result<Audience, Error> {
self.client.put(&format!("/audiences/{}", id), &data).await
}
pub async fn delete(&self, id: &str) -> Result<EmptyResponse, Error> {
self.client.delete(&format!("/audiences/{}", id)).await
}
pub async fn add_contacts(
&self,
id: &str,
contact_ids: Vec<String>,
) -> Result<EmptyResponse, Error> {
let request = AddContactsRequest { contact_ids };
self.client
.post(&format!("/audiences/{}/contacts", id), &request)
.await
}
pub async fn remove_contact(
&self,
audience_id: &str,
contact_id: &str,
) -> Result<EmptyResponse, Error> {
self.client
.delete(&format!("/audiences/{}/contacts/{}", audience_id, contact_id))
.await
}
pub async fn list_contacts(
&self,
audience_id: &str,
params: Option<ListParams>,
) -> Result<PaginatedList<Contact>, Error> {
let params = params.unwrap_or_default();
let query_params = params.to_query_params();
self.client
.get_with_params(&format!("/audiences/{}/contacts", audience_id), &query_params)
.await
}
}