From 8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420 Mon Sep 17 00:00:00 2001 From: fx Date: Sat, 21 Oct 2023 17:43:31 +0200 Subject: added device add and update --- src/main.rs | 45 +++++++++++++++++++++++++++++++++++----- src/requests/device.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/requests/get.rs | 21 ------------------- src/requests/mod.rs | 2 +- 4 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 src/requests/device.rs delete mode 100644 src/requests/get.rs (limited to 'src') diff --git a/src/main.rs b/src/main.rs index e6ca3ef..ab7e476 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,7 @@ use clap::{Parser, Subcommand}; use config::SETTINGS; use error::CliError; -use requests::{start::start, get::get}; +use requests::{start::start, device}; use reqwest::header::{HeaderMap, HeaderValue}; use serde::Deserialize; @@ -23,11 +23,29 @@ enum Commands { /// id of the device id: String }, - Get { - id: String + Device { + #[command(subcommand)] + devicecmd: DeviceCmd, } } +#[derive(Subcommand)] +enum DeviceCmd { + Add { + id: String, + mac: String, + broadcast_addr: String + }, + Get { + id: String, + }, + Edit { + id: String, + mac: String, + broadcast_addr: String + }, +} + fn main() -> Result<(), CliError> { let cli = Args::parse(); @@ -35,8 +53,18 @@ fn main() -> Result<(), CliError> { Commands::Start { id } => { start(id)?; }, - Commands::Get { id } => { - get(id)?; + Commands::Device { devicecmd } => { + match devicecmd { + DeviceCmd::Add { id, mac, broadcast_addr } => { + device::put(id, mac, broadcast_addr)?; + }, + DeviceCmd::Get { id } => { + device::get(id)?; + }, + DeviceCmd::Edit { id, mac, broadcast_addr } => { + device::post(id, mac, broadcast_addr)?; + }, + } } } @@ -57,7 +85,14 @@ fn default_headers() -> Result { ); Ok(map) +} +fn format_url(path: &str) -> Result { + Ok(format!( + "{}/{}", + SETTINGS.get_string("server").map_err(CliError::Config)?, + path + )) } #[derive(Debug, Deserialize)] diff --git a/src/requests/device.rs b/src/requests/device.rs new file mode 100644 index 0000000..525745a --- /dev/null +++ b/src/requests/device.rs @@ -0,0 +1,56 @@ +use crate::{error::CliError, default_headers, format_url}; + +pub fn put(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> { + let res = reqwest::blocking::Client::new() + .put(format_url("device")?) + .headers(default_headers()?) + .body( + format!( + r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#, + id, + mac, + broadcast_addr + ) + ) + .send() + .map_err(CliError::Reqwest)? + .text(); + + println!("{:?}", res); + Ok(()) +} + +pub fn get(id: String) -> Result<(), CliError> { + let res = reqwest::blocking::Client::new() + .get(format_url("device")?) + .headers(default_headers()?) + .body( + format!(r#"{{"id": "{}"}}"#, id) + ) + .send() + .map_err(CliError::Reqwest)? + .text(); + + println!("{:?}", res); + Ok(()) +} + +pub fn post(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> { + let res = reqwest::blocking::Client::new() + .post(format_url("device")?) + .headers(default_headers()?) + .body( + format!( + r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#, + id, + mac, + broadcast_addr + ) + ) + .send() + .map_err(CliError::Reqwest)? + .text(); + + println!("{:?}", res); + Ok(()) +} diff --git a/src/requests/get.rs b/src/requests/get.rs deleted file mode 100644 index 225562d..0000000 --- a/src/requests/get.rs +++ /dev/null @@ -1,21 +0,0 @@ -use crate::{error::CliError, config::SETTINGS, default_headers}; - -pub fn get(id: String) -> Result<(), CliError> { - let res = reqwest::blocking::Client::new() - .get( - format!( - "{}/start", - SETTINGS.get_string("server").map_err(CliError::Config)? - ) - ) - .headers(default_headers()?) - .body( - format!(r#"{{"id": "{}"}}"#, id) - ) - .send() - .map_err(CliError::Reqwest)? - .text(); - - println!("{:?}", res); - Ok(()) -} diff --git a/src/requests/mod.rs b/src/requests/mod.rs index ddf0ea9..6855db1 100644 --- a/src/requests/mod.rs +++ b/src/requests/mod.rs @@ -1,2 +1,2 @@ pub mod start; -pub mod get; +pub mod device; -- cgit v1.2.3