use std::sync::Arc;
use crate::client::{parse_response, InnerClient};
use crate::error::CencoriError;
use crate::types::{
ApproveRejectParams, CreateSessionParams, PaginatedSessionEventsResponse,
PaginatedSessionsResponse, RejectResponse, Session, SessionDeleteResponse, SessionListParams,
TurnParams,
};
#[derive(Debug, Clone)]
pub struct SessionsModule {
inner: Arc<InnerClient>,
}
impl SessionsModule {
pub(crate) fn new(inner: Arc<InnerClient>) -> Self {
Self { inner }
}
pub fn create(&self, params: &CreateSessionParams) -> Result<Session, CencoriError> {
let url = self.inner.url("/v1/sessions");
let response = self
.inner
.blocking_client
.post(&url)
.json(params)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub fn list(&self, params: &SessionListParams) -> Result<PaginatedSessionsResponse, CencoriError> {
let mut path = "/v1/sessions".to_string();
let mut query_parts: Vec<String> = Vec::new();
if let Some(page) = params.page {
query_parts.push(format!("page={}", page));
}
if let Some(limit) = params.limit {
query_parts.push(format!("limit={}", limit));
}
if let Some(ref status) = params.status {
query_parts.push(format!("status={}", status));
}
if let Some(ref agent_id) = params.agent_id {
query_parts.push(format!("agent_id={}", agent_id));
}
if !query_parts.is_empty() {
path.push('?');
path.push_str(&query_parts.join("&"));
}
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.get(&url)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub fn get(&self, session_id: &str) -> Result<Session, CencoriError> {
let path = format!("/v1/sessions/{session_id}");
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.get(&url)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub fn delete(&self, session_id: &str) -> Result<SessionDeleteResponse, CencoriError> {
let path = format!("/v1/sessions/{session_id}");
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.delete(&url)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub fn submit_turn(
&self,
session_id: &str,
params: &TurnParams,
) -> Result<reqwest::blocking::Response, CencoriError> {
let path = format!("/v1/sessions/{session_id}/turns");
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.post(&url)
.json(params)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
if !status.is_success() {
let body = response.text().map_err(CencoriError::Http)?;
return match parse_response(status, &body) {
Err(error) => Err(error),
Ok(_) => Err(CencoriError::api(status.as_u16(), "Session turn request failed")),
};
}
Ok(response)
}
pub fn get_events(
&self,
session_id: &str,
page: Option<u32>,
limit: Option<u32>,
turn_number: Option<u32>,
) -> Result<PaginatedSessionEventsResponse, CencoriError> {
let mut path = format!("/v1/sessions/{session_id}/events");
let mut query_parts: Vec<String> = Vec::new();
if let Some(p) = page {
query_parts.push(format!("page={}", p));
}
if let Some(l) = limit {
query_parts.push(format!("limit={}", l));
}
if let Some(t) = turn_number {
query_parts.push(format!("turn_number={}", t));
}
if !query_parts.is_empty() {
path.push('?');
path.push_str(&query_parts.join("&"));
}
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.get(&url)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub fn approve(
&self,
session_id: &str,
params: &ApproveRejectParams,
) -> Result<reqwest::blocking::Response, CencoriError> {
let path = format!("/v1/sessions/{session_id}/approve");
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.post(&url)
.json(params)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
if !status.is_success() {
let body = response.text().map_err(CencoriError::Http)?;
return match parse_response(status, &body) {
Err(error) => Err(error),
Ok(_) => Err(CencoriError::api(status.as_u16(), "Session approval request failed")),
};
}
Ok(response)
}
pub fn reject(&self, session_id: &str, params: &ApproveRejectParams) -> Result<RejectResponse, CencoriError> {
let path = format!("/v1/sessions/{session_id}/reject");
let url = self.inner.url(&path);
let response = self
.inner
.blocking_client
.post(&url)
.json(params)
.send()
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_create(&self, params: &CreateSessionParams) -> Result<Session, CencoriError> {
let url = self.inner.url("/v1/sessions");
let response = self
.inner
.http_client
.post(&url)
.json(params)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_list(&self, params: &SessionListParams) -> Result<PaginatedSessionsResponse, CencoriError> {
let mut path = "/v1/sessions".to_string();
let mut query_parts: Vec<String> = Vec::new();
if let Some(page) = params.page {
query_parts.push(format!("page={}", page));
}
if let Some(limit) = params.limit {
query_parts.push(format!("limit={}", limit));
}
if let Some(ref status) = params.status {
query_parts.push(format!("status={}", status));
}
if let Some(ref agent_id) = params.agent_id {
query_parts.push(format!("agent_id={}", agent_id));
}
if !query_parts.is_empty() {
path.push('?');
path.push_str(&query_parts.join("&"));
}
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.get(&url)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_get(&self, session_id: &str) -> Result<Session, CencoriError> {
let path = format!("/v1/sessions/{session_id}");
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.get(&url)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_delete(&self, session_id: &str) -> Result<SessionDeleteResponse, CencoriError> {
let path = format!("/v1/sessions/{session_id}");
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.delete(&url)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_submit_turn(
&self,
session_id: &str,
params: &TurnParams,
) -> Result<reqwest::Response, CencoriError> {
let path = format!("/v1/sessions/{session_id}/turns");
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.post(&url)
.json(params)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.map_err(CencoriError::Http)?;
return match parse_response(status, &body) {
Err(error) => Err(error),
Ok(_) => Err(CencoriError::api(status.as_u16(), "Session turn request failed")),
};
}
Ok(response)
}
pub async fn async_get_events(
&self,
session_id: &str,
page: Option<u32>,
limit: Option<u32>,
turn_number: Option<u32>,
) -> Result<PaginatedSessionEventsResponse, CencoriError> {
let mut path = format!("/v1/sessions/{session_id}/events");
let mut query_parts: Vec<String> = Vec::new();
if let Some(p) = page {
query_parts.push(format!("page={}", p));
}
if let Some(l) = limit {
query_parts.push(format!("limit={}", l));
}
if let Some(t) = turn_number {
query_parts.push(format!("turn_number={}", t));
}
if !query_parts.is_empty() {
path.push('?');
path.push_str(&query_parts.join("&"));
}
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.get(&url)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
pub async fn async_approve(
&self,
session_id: &str,
params: &ApproveRejectParams,
) -> Result<reqwest::Response, CencoriError> {
let path = format!("/v1/sessions/{session_id}/approve");
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.post(&url)
.json(params)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.map_err(CencoriError::Http)?;
return match parse_response(status, &body) {
Err(error) => Err(error),
Ok(_) => Err(CencoriError::api(status.as_u16(), "Session approval request failed")),
};
}
Ok(response)
}
pub async fn async_reject(&self, session_id: &str, params: &ApproveRejectParams) -> Result<RejectResponse, CencoriError> {
let path = format!("/v1/sessions/{session_id}/reject");
let url = self.inner.url(&path);
let response = self
.inner
.http_client
.post(&url)
.json(params)
.send()
.await
.map_err(CencoriError::Http)?;
let status = response.status();
let body = response.text().await.map_err(CencoriError::Http)?;
let data = parse_response(status, &body)?;
serde_json::from_value(data).map_err(CencoriError::Json)
}
}