use futures::Stream;
use crate::client::Kranth;
use crate::errors::Result;
use crate::sse::stream_debate_events;
use crate::types::*;
pub struct DebatesResource<'a> {
pub(crate) client: &'a Kranth,
}
impl<'a> DebatesResource<'a> {
pub async fn create(&self, req: &CreateDebateRequest) -> Result<DebateRow> {
self.client.post("/v1/debates", req, None).await
}
pub async fn create_with_idempotency_key(
&self,
req: &CreateDebateRequest,
idempotency_key: &str,
) -> Result<DebateRow> {
self.client
.post("/v1/debates", req, Some(idempotency_key))
.await
}
pub async fn get(&self, debate_id: &str) -> Result<DebateDetail> {
self.client
.get(&format!("/v1/debates/{debate_id}"), &[])
.await
}
pub async fn list(&self) -> Result<ListDebatesResponse> {
self.client.get("/v1/debates", &[]).await
}
pub async fn turns(&self, debate_id: &str) -> Result<Vec<DebateTurn>> {
#[derive(serde::Deserialize)]
struct Wrap {
turns: Vec<DebateTurn>,
}
let w: Wrap = self
.client
.get(&format!("/v1/debates/{debate_id}/turns"), &[])
.await?;
Ok(w.turns)
}
pub async fn set_public(&self, debate_id: &str, public: bool) -> Result<DebateDetail> {
self.client
.patch(
&format!("/v1/debates/{debate_id}"),
&serde_json::json!({ "public": public }),
)
.await
}
pub async fn cancel(&self, debate_id: &str) -> Result<serde_json::Value> {
self.client
.post(
&format!("/v1/debates/{debate_id}/cancel"),
&serde_json::json!({}),
None,
)
.await
}
pub async fn export(&self, debate_id: &str) -> Result<serde_json::Value> {
self.client
.get(&format!("/v1/debates/{debate_id}/export"), &[])
.await
}
pub fn stream(&self, debate_id: &str) -> impl Stream<Item = Result<DebateEvent>> + 'a {
stream_debate_events(self.client.clone(), debate_id.to_string())
}
}