diff options
Diffstat (limited to 'src/requests')
-rw-r--r-- | src/requests/get.rs | 21 | ||||
-rw-r--r-- | src/requests/mod.rs | 2 | ||||
-rw-r--r-- | src/requests/start.rs | 34 |
3 files changed, 57 insertions, 0 deletions
diff --git a/src/requests/get.rs b/src/requests/get.rs new file mode 100644 index 0000000..225562d --- /dev/null +++ b/src/requests/get.rs | |||
@@ -0,0 +1,21 @@ | |||
1 | use crate::{error::CliError, config::SETTINGS, default_headers}; | ||
2 | |||
3 | pub 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 new file mode 100644 index 0000000..ddf0ea9 --- /dev/null +++ b/src/requests/mod.rs | |||
@@ -0,0 +1,2 @@ | |||
1 | pub mod start; | ||
2 | pub mod get; | ||
diff --git a/src/requests/start.rs b/src/requests/start.rs new file mode 100644 index 0000000..1a8cb6c --- /dev/null +++ b/src/requests/start.rs | |||
@@ -0,0 +1,34 @@ | |||
1 | use serde::Deserialize; | ||
2 | |||
3 | use crate::{config::SETTINGS, error::CliError, default_headers}; | ||
4 | |||
5 | pub fn start(id: String) -> Result<(), CliError> { | ||
6 | let res = reqwest::blocking::Client::new() | ||
7 | .post( | ||
8 | format!( | ||
9 | "{}/start", | ||
10 | SETTINGS.get_string("server").map_err(CliError::Config)? | ||
11 | ) | ||
12 | ) | ||
13 | .headers(default_headers()?) | ||
14 | .body( | ||
15 | format!(r#"{{"id": "{}"}}"#, id) | ||
16 | ) | ||
17 | .send() | ||
18 | .map_err(CliError::Reqwest)? | ||
19 | .text(); | ||
20 | |||
21 | let res = serde_json::from_str::<StartResponse>(&res.map_err(CliError::Reqwest)?).map_err(CliError::Serde)?; | ||
22 | |||
23 | if res.boot { | ||
24 | println!("successfully started {}", res.id); | ||
25 | } | ||
26 | |||
27 | Ok(()) | ||
28 | } | ||
29 | |||
30 | #[derive(Debug, Deserialize)] | ||
31 | struct StartResponse { | ||
32 | boot: bool, | ||
33 | id: String, | ||
34 | } | ||