use reqwest::Method;
use serde::Deserialize;
use crate::client::{HeyoClient, HeyoClientOptions, RequestOptions};
use crate::commands::encode_path;
use crate::errors::HeyoError;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DaemonStatus {
Online,
Stale,
Offline,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DaemonInfo {
pub id: String,
#[serde(default)]
pub name: Option<String>,
pub status: DaemonStatus,
pub last_seen_at: String,
pub created_at: String,
}
#[derive(Deserialize)]
struct DaemonsEnvelope {
#[serde(default)]
daemons: Vec<DaemonInfo>,
}
pub struct Daemons;
impl Daemons {
pub async fn list(client_options: HeyoClientOptions) -> Result<Vec<DaemonInfo>, HeyoError> {
let client = HeyoClient::new(client_options)?;
let env: DaemonsEnvelope = client
.request(Method::GET, "/me/daemons", None::<&()>, RequestOptions::default())
.await?;
Ok(env.daemons)
}
pub async fn get(
id: &str,
client_options: HeyoClientOptions,
) -> Result<DaemonInfo, HeyoError> {
let client = HeyoClient::new(client_options)?;
let path = format!("/me/daemons/{}", encode_path(id));
client
.request(Method::GET, &path, None::<&()>, RequestOptions::default())
.await
}
pub async fn delete(id: &str, client_options: HeyoClientOptions) -> Result<(), HeyoError> {
let client = HeyoClient::new(client_options)?;
let path = format!("/me/daemons/{}", encode_path(id));
match client
.request::<serde_json::Value>(
Method::DELETE,
&path,
None::<&()>,
RequestOptions::default(),
)
.await
{
Ok(_) => Ok(()),
Err(HeyoError::NotFound(_)) => Ok(()),
Err(e) => Err(e),
}
}
}