use std::sync::Arc;
use crate::client::BaseClient;
use crate::collection::Collection;
use crate::error::Error;
use crate::types::{Audience, CreateAudienceParams, ListParams, UpdateAudienceParams};
pub struct AudienceService {
pub(crate) client: Arc<BaseClient>,
}
impl AudienceService {
pub async fn create(&self, params: CreateAudienceParams) -> Result<Audience, Error> {
self.client
.request("POST", "/v2/audiences", to_body(¶ms), None)
.await
}
pub async fn get(&self, id: &str) -> Result<Audience, Error> {
let path = format!("/v2/audiences/{}", urlencoding::encode(id));
self.client.request("GET", &path, None, None).await
}
pub async fn update(&self, id: &str, params: UpdateAudienceParams) -> Result<Audience, Error> {
let path = format!("/v2/audiences/{}", urlencoding::encode(id));
self.client
.request("POST", &path, to_body(¶ms), None)
.await
}
pub async fn list(&self, params: Option<ListParams>) -> Result<Collection<Audience>, Error> {
let query = params.map(|p| {
let mut q = Vec::new();
if let Some(page) = p.page {
q.push(("page", page.to_string()));
}
if let Some(limit) = p.limit {
q.push(("limit", limit.to_string()));
}
q
});
self.client
.request::<Collection<Audience>>("GET", "/v2/audiences", None, query.as_deref())
.await
}
pub async fn delete(&self, id: &str) -> Result<Audience, Error> {
let path = format!("/v2/audiences/{}", urlencoding::encode(id));
self.client.request("DELETE", &path, None, None).await
}
}
fn to_body(v: &impl serde::Serialize) -> Option<serde_json::Value> {
serde_json::to_value(v).ok()
}