use crate::client::Keplars;
use crate::errors::KeplarsResult;
use crate::models::{
ApiDataResponse, ApiListResponse, ApiMessageResponse, ApiResponse, Audience,
};
use serde::Serialize;
#[derive(Serialize)]
struct CreateAudienceRequest<'a> {
name: &'a str,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'a str>,
}
pub struct Audiences<'a> {
client: &'a Keplars,
}
impl<'a> Audiences<'a> {
pub fn new(client: &'a Keplars) -> Self {
Self { client }
}
pub async fn create(
&self,
name: &str,
description: Option<&str>,
) -> KeplarsResult<ApiResponse<Audience>> {
let body = CreateAudienceRequest { name, description };
self.client.post("/api/v1/public/audiences/add-audience", &body).await
}
pub async fn list(
&self,
page: Option<u32>,
limit: Option<u32>,
) -> KeplarsResult<ApiListResponse<Audience>> {
let mut params = vec![];
if let Some(p) = page {
params.push(format!("page={}", p));
}
if let Some(l) = limit {
params.push(format!("limit={}", l));
}
let query = if params.is_empty() {
String::new()
} else {
format!("?{}", params.join("&"))
};
let path = format!("/api/v1/public/audiences/get-audiences{}", query);
self.client.get(&path).await
}
pub async fn get(&self, id: &str) -> KeplarsResult<ApiDataResponse<Audience>> {
let path = format!(
"/api/v1/public/audiences/get-audience?id={}",
urlencoding::encode(id)
);
self.client.get(&path).await
}
pub async fn delete(&self, id: &str) -> KeplarsResult<ApiMessageResponse> {
let path = format!(
"/api/v1/public/audiences/delete-audience?id={}",
urlencoding::encode(id)
);
self.client.delete(&path).await
}
}