use crate::client::Client;
use crate::error::Error;
use crate::types::{
Contact, CreateTopicRequest, EmptyResponse, ListParams, PaginatedList, Topic,
TopicAddContactsRequest, UpdateTopicRequest,
};
pub struct Topics<'a> {
client: &'a Client,
}
impl<'a> Topics<'a> {
pub fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn list(
&self,
audience_id: &str,
params: Option<ListParams>,
) -> Result<PaginatedList<Topic>, Error> {
let params = params.unwrap_or_default();
let query_params = params.to_query_params();
self.client
.get_with_params(&format!("/audiences/{}/topics", audience_id), &query_params)
.await
}
pub async fn get(&self, audience_id: &str, id: &str) -> Result<Topic, Error> {
self.client
.get(&format!("/audiences/{}/topics/{}", audience_id, id))
.await
}
pub async fn create(&self, audience_id: &str, data: CreateTopicRequest) -> Result<Topic, Error> {
self.client
.post(&format!("/audiences/{}/topics", audience_id), &data)
.await
}
pub async fn update(
&self,
audience_id: &str,
id: &str,
data: UpdateTopicRequest,
) -> Result<Topic, Error> {
self.client
.put(&format!("/audiences/{}/topics/{}", audience_id, id), &data)
.await
}
pub async fn delete(&self, audience_id: &str, id: &str) -> Result<EmptyResponse, Error> {
self.client
.delete(&format!("/audiences/{}/topics/{}", audience_id, id))
.await
}
pub async fn add_contacts(
&self,
audience_id: &str,
topic_id: &str,
contact_ids: Vec<String>,
) -> Result<EmptyResponse, Error> {
let request = TopicAddContactsRequest { contact_ids };
self.client
.post(
&format!("/audiences/{}/topics/{}/contacts", audience_id, topic_id),
&request,
)
.await
}
pub async fn remove_contact(
&self,
audience_id: &str,
topic_id: &str,
contact_id: &str,
) -> Result<EmptyResponse, Error> {
self.client
.delete(&format!(
"/audiences/{}/topics/{}/contacts/{}",
audience_id, topic_id, contact_id
))
.await
}
pub async fn list_contacts(
&self,
audience_id: &str,
topic_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/{}/topics/{}/contacts", audience_id, topic_id),
&query_params,
)
.await
}
}