use anyhow::Context;
use super::DaemonClient;
use super::types::{
FleetByProjectWireResponse, FleetProjectGroupWire, ManagedActivityResponse,
ManagedAdoptRequest, ManagedAdoptResponse, ManagedAnswerRequest, ManagedAnswerResponse,
ManagedAttachCmdResponse, ManagedListResponse, ManagedSendInputRequest,
ManagedSendInputResponse, ManagedSessionSummary, ManagedSpawnRequest, ManagedSpawnResponse,
};
impl DaemonClient {
pub async fn list_managed_sessions(&self) -> anyhow::Result<Vec<ManagedSessionSummary>> {
let url = format!("{}/api/v1/sessions/managed", self.base);
let body: ManagedListResponse = self
.http
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(body.sessions)
}
pub async fn fleet_managed_sessions(&self) -> anyhow::Result<Vec<FleetProjectGroupWire>> {
let url = format!("{}/api/v1/sessions/managed/fleet", self.base);
let body: FleetByProjectWireResponse = self
.http
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(body.projects)
}
pub async fn get_managed_session(&self, id: &str) -> anyhow::Result<ManagedSessionSummary> {
let url = format!("{}/api/v1/sessions/managed/{id}", self.base);
let summary = self
.http
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(summary)
}
pub async fn spawn_managed_session(
&self,
req: &ManagedSpawnRequest,
) -> anyhow::Result<ManagedSpawnResponse> {
let url = format!("{}/api/v1/sessions/managed", self.base);
let resp = self
.http
.post(&url)
.json(req)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn adopt_managed_session(
&self,
req: &ManagedAdoptRequest,
) -> anyhow::Result<ManagedAdoptResponse> {
let url = format!("{}/api/v1/sessions/managed/adopt", self.base);
let resp = self.http.post(&url).json(req).send().await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("adopt failed (HTTP {status}): {body}");
}
let adopted = resp.json().await.context("decoding adopt response body")?;
Ok(adopted)
}
pub async fn send_to_managed_session(
&self,
id: &str,
text: &str,
) -> anyhow::Result<ManagedSendInputResponse> {
let url = format!("{}/api/v1/sessions/managed/{id}/send", self.base);
let resp = self
.http
.post(&url)
.json(&ManagedSendInputRequest {
text: text.to_string(),
})
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn answer_managed_session(
&self,
id: &str,
answer: &str,
) -> anyhow::Result<ManagedAnswerResponse> {
let url = format!("{}/api/v1/sessions/managed/{id}/answer", self.base);
let resp = self
.http
.post(&url)
.json(&ManagedAnswerRequest {
answer: answer.to_string(),
})
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn managed_session_activity(
&self,
id: &str,
) -> anyhow::Result<ManagedActivityResponse> {
let url = format!("{}/api/v1/sessions/managed/{id}/activity", self.base);
let resp = self
.http
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn managed_session_attach_cmd(
&self,
id: &str,
) -> anyhow::Result<ManagedAttachCmdResponse> {
let url = format!("{}/api/v1/sessions/managed/{id}/attach-cmd", self.base);
let resp = self
.http
.get(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn runtime_stop_managed_session(
&self,
id: &str,
) -> anyhow::Result<ManagedSessionSummary> {
let url = format!("{}/api/v1/sessions/managed/{id}/runtime-stop", self.base);
let resp = self
.http
.post(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
pub async fn resume_managed_session(&self, id: &str) -> anyhow::Result<ManagedSessionSummary> {
let url = format!("{}/api/v1/sessions/managed/{id}/resume", self.base);
let resp = self.http.post(&url).send().await?;
let status = resp.status();
if !status.is_success() {
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("resume failed (HTTP {status}): {body}");
}
let summary = resp.json().await.context("decoding resume response body")?;
Ok(summary)
}
pub async fn decommission_managed_session(
&self,
id: &str,
) -> anyhow::Result<ManagedSessionSummary> {
let url = format!("{}/api/v1/sessions/managed/{id}/decommission", self.base);
let resp = self
.http
.post(&url)
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(resp)
}
}