summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs45
-rw-r--r--src/requests/device.rs56
-rw-r--r--src/requests/get.rs21
-rw-r--r--src/requests/mod.rs2
4 files changed, 97 insertions, 27 deletions
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 @@
1use clap::{Parser, Subcommand}; 1use clap::{Parser, Subcommand};
2use config::SETTINGS; 2use config::SETTINGS;
3use error::CliError; 3use error::CliError;
4use requests::{start::start, get::get}; 4use requests::{start::start, device};
5use reqwest::header::{HeaderMap, HeaderValue}; 5use reqwest::header::{HeaderMap, HeaderValue};
6use serde::Deserialize; 6use serde::Deserialize;
7 7
@@ -23,11 +23,29 @@ enum Commands {
23 /// id of the device 23 /// id of the device
24 id: String 24 id: String
25 }, 25 },
26 Get { 26 Device {
27 id: String 27 #[command(subcommand)]
28 devicecmd: DeviceCmd,
28 } 29 }
29} 30}
30 31
32#[derive(Subcommand)]
33enum DeviceCmd {
34 Add {
35 id: String,
36 mac: String,
37 broadcast_addr: String
38 },
39 Get {
40 id: String,
41 },
42 Edit {
43 id: String,
44 mac: String,
45 broadcast_addr: String
46 },
47}
48
31fn main() -> Result<(), CliError> { 49fn main() -> Result<(), CliError> {
32 let cli = Args::parse(); 50 let cli = Args::parse();
33 51
@@ -35,8 +53,18 @@ fn main() -> Result<(), CliError> {
35 Commands::Start { id } => { 53 Commands::Start { id } => {
36 start(id)?; 54 start(id)?;
37 }, 55 },
38 Commands::Get { id } => { 56 Commands::Device { devicecmd } => {
39 get(id)?; 57 match devicecmd {
58 DeviceCmd::Add { id, mac, broadcast_addr } => {
59 device::put(id, mac, broadcast_addr)?;
60 },
61 DeviceCmd::Get { id } => {
62 device::get(id)?;
63 },
64 DeviceCmd::Edit { id, mac, broadcast_addr } => {
65 device::post(id, mac, broadcast_addr)?;
66 },
67 }
40 } 68 }
41 } 69 }
42 70
@@ -57,7 +85,14 @@ fn default_headers() -> Result<HeaderMap, CliError> {
57 ); 85 );
58 86
59 Ok(map) 87 Ok(map)
88}
60 89
90fn format_url(path: &str) -> Result<String, CliError> {
91 Ok(format!(
92 "{}/{}",
93 SETTINGS.get_string("server").map_err(CliError::Config)?,
94 path
95 ))
61} 96}
62 97
63#[derive(Debug, Deserialize)] 98#[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 @@
1use crate::{error::CliError, default_headers, format_url};
2
3pub fn put(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> {
4 let res = reqwest::blocking::Client::new()
5 .put(format_url("device")?)
6 .headers(default_headers()?)
7 .body(
8 format!(
9 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#,
10 id,
11 mac,
12 broadcast_addr
13 )
14 )
15 .send()
16 .map_err(CliError::Reqwest)?
17 .text();
18
19 println!("{:?}", res);
20 Ok(())
21}
22
23pub fn get(id: String) -> Result<(), CliError> {
24 let res = reqwest::blocking::Client::new()
25 .get(format_url("device")?)
26 .headers(default_headers()?)
27 .body(
28 format!(r#"{{"id": "{}"}}"#, id)
29 )
30 .send()
31 .map_err(CliError::Reqwest)?
32 .text();
33
34 println!("{:?}", res);
35 Ok(())
36}
37
38pub fn post(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> {
39 let res = reqwest::blocking::Client::new()
40 .post(format_url("device")?)
41 .headers(default_headers()?)
42 .body(
43 format!(
44 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#,
45 id,
46 mac,
47 broadcast_addr
48 )
49 )
50 .send()
51 .map_err(CliError::Reqwest)?
52 .text();
53
54 println!("{:?}", res);
55 Ok(())
56}
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 @@
1use crate::{error::CliError, config::SETTINGS, default_headers};
2
3pub fn get(id: String) -> Result<(), CliError> {
4 let res = reqwest::blocking::Client::new()
5 .get(
6 format!(
7 "{}/start",
8 SETTINGS.get_string("server").map_err(CliError::Config)?
9 )
10 )
11 .headers(default_headers()?)
12 .body(
13 format!(r#"{{"id": "{}"}}"#, id)
14 )
15 .send()
16 .map_err(CliError::Reqwest)?
17 .text();
18
19 println!("{:?}", res);
20 Ok(())
21}
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 @@
1pub mod start; 1pub mod start;
2pub mod get; 2pub mod device;