forked from navos/sheepd
✨ cli + user api
This commit is contained in:
parent
46cf2f4572
commit
b010027549
12 changed files with 504 additions and 25 deletions
65
src/api.rs
65
src/api.rs
|
@ -3,6 +3,20 @@ use rumqttc::{AsyncClient, Event, EventLoop, MqttOptions, Packet, Transport};
|
|||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub fn domain(host: &str) -> String {
|
||||
if host.starts_with("http") {
|
||||
return host.to_string();
|
||||
} else {
|
||||
format!("https://{host}")
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct LoginParam {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
/// Join Request
|
||||
pub struct JoinParams {
|
||||
|
@ -89,20 +103,57 @@ where
|
|||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
/// Generic JSON API result
|
||||
pub struct Result {
|
||||
pub ok: u32,
|
||||
pub struct DeviceList {
|
||||
pub devices: Vec<DeviceEntry>,
|
||||
}
|
||||
|
||||
impl Result {
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct DeviceEntry {
|
||||
pub id: String,
|
||||
pub hostname: String,
|
||||
pub online: bool,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
/// Generic JSON API result
|
||||
pub struct Result<T: Serialize> {
|
||||
pub ok: Option<T>,
|
||||
pub err: Option<String>,
|
||||
}
|
||||
|
||||
impl<T: Serialize> Result<T> {
|
||||
#[allow(non_snake_case)]
|
||||
pub fn OkVal(val: T) -> Self {
|
||||
Self {
|
||||
ok: Some(val),
|
||||
err: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_result(self) -> std::result::Result<T, String> {
|
||||
if let Some(ok) = self.ok {
|
||||
Ok(ok)
|
||||
} else {
|
||||
Err(self.err.unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Result<i32> {
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Ok() -> Self {
|
||||
Self { ok: 1 }
|
||||
Self {
|
||||
ok: Some(1),
|
||||
err: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Err() -> Self {
|
||||
Self { ok: 0 }
|
||||
pub fn Err(msg: &str) -> Self {
|
||||
Self {
|
||||
ok: None,
|
||||
err: Some(msg.to_string()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue