use reqwest::Method;
use crate::error::Result;
use crate::http::{Envelope, HttpClient};
use crate::models::{CreateWorkspace, Message, UpdateWorkspace, Workspace, WorkspaceAnalytics};
#[derive(Debug, Clone)]
pub struct Workspaces<'a> {
pub(crate) http: &'a HttpClient,
}
impl Workspaces<'_> {
pub async fn list(&self) -> Result<Vec<Workspace>> {
let body: Envelope<Vec<Workspace>> = self
.http
.send::<_, ()>(Method::GET, "/workspaces", None, None)
.await?;
Ok(body.data)
}
pub async fn get(&self, id: &str) -> Result<Workspace> {
let body: Envelope<Workspace> = self
.http
.send::<_, ()>(Method::GET, &format!("/workspaces/{id}"), None, None)
.await?;
Ok(body.data)
}
pub async fn create(&self, workspace: &CreateWorkspace) -> Result<Workspace> {
let body: Envelope<Workspace> = self
.http
.send(Method::POST, "/workspaces", None, Some(workspace))
.await?;
Ok(body.data)
}
pub async fn update(&self, id: &str, changes: &UpdateWorkspace) -> Result<Workspace> {
let body: Envelope<Workspace> = self
.http
.send(
Method::PUT,
&format!("/workspaces/{id}"),
None,
Some(changes),
)
.await?;
Ok(body.data)
}
pub async fn delete(&self, id: &str) -> Result<Message> {
self.http
.send::<Message, ()>(Method::DELETE, &format!("/workspaces/{id}"), None, None)
.await
}
pub async fn analytics(&self, id: &str) -> Result<WorkspaceAnalytics> {
let body: Envelope<WorkspaceAnalytics> = self
.http
.send::<_, ()>(
Method::GET,
&format!("/workspaces/{id}/analytics"),
None,
None,
)
.await?;
Ok(body.data)
}
}