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/requests/device.rs | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/requests/get.rs | 21 ------------------- src/requests/mod.rs | 2 +- 3 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 src/requests/device.rs delete mode 100644 src/requests/get.rs (limited to 'src/requests') 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