use std::sync::Arc;
use serde::Serialize;
use crate::error::{Error, Result};
use crate::http::Config;
use crate::types::{
list_query, Broadcast, BroadcastId, BroadcastListItem, CancelBroadcastResponse,
CreateBroadcastOptions, DeleteBroadcastResponse, List, ListOptions, UpdateBroadcastOptions,
};
#[derive(Clone)]
pub struct Broadcasts(pub(crate) Arc<Config>);
impl Broadcasts {
pub async fn create(&self, broadcast: &CreateBroadcastOptions) -> Result<BroadcastId> {
self.0.post(&["broadcasts"], broadcast).await
}
pub async fn get(&self, id: &str) -> Result<Broadcast> {
self.0.get(&["broadcasts", id], &[]).await
}
pub async fn list(&self, options: Option<&ListOptions>) -> Result<List<BroadcastListItem>> {
self.0.get(&["broadcasts"], &list_query(options)).await
}
pub async fn update(&self, id: &str, changes: &UpdateBroadcastOptions) -> Result<BroadcastId> {
if changes.clear_topic_id {
let mut body = serde_json::to_value(changes).map_err(Error::Parse)?;
body["topic_id"] = serde_json::Value::Null;
return self.0.patch(&["broadcasts", id], &body).await;
}
self.0.patch(&["broadcasts", id], changes).await
}
pub async fn delete(&self, id: &str) -> Result<DeleteBroadcastResponse> {
self.0.delete(&["broadcasts", id]).await
}
pub async fn send(&self, id: &str, scheduled_at: Option<&str>) -> Result<BroadcastId> {
self.0
.post(&["broadcasts", id, "send"], &ScheduledAt { scheduled_at })
.await
}
pub async fn cancel(&self, id: &str) -> Result<CancelBroadcastResponse> {
self.0.post_empty(&["broadcasts", id, "cancel"]).await
}
}
#[derive(Serialize)]
struct ScheduledAt<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
scheduled_at: Option<&'a str>,
}