diff options
author | FxQnLr <[email protected]> | 2023-11-02 21:01:16 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2023-11-02 21:01:16 +0100 |
commit | f4d3d921460b606a9ff6686c9bb9a79bf546f264 (patch) | |
tree | 13c41a8dc306b0c16a3f7e1aa2ffc100c6b2edf3 /src/requests | |
parent | 8fab2e7c3a38a91c8f5549b639e7f2ac4ae1a420 (diff) | |
download | webol-cli-f4d3d921460b606a9ff6686c9bb9a79bf546f264.tar webol-cli-f4d3d921460b606a9ff6686c9bb9a79bf546f264.tar.gz webol-cli-f4d3d921460b606a9ff6686c9bb9a79bf546f264.zip |
baseline ping
Diffstat (limited to 'src/requests')
-rw-r--r-- | src/requests/device.rs | 42 | ||||
-rw-r--r-- | src/requests/start.rs | 73 |
2 files changed, 85 insertions, 30 deletions
diff --git a/src/requests/device.rs b/src/requests/device.rs index 525745a..f7754a4 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs | |||
@@ -1,55 +1,63 @@ | |||
1 | use crate::{error::CliError, default_headers, format_url}; | 1 | use crate::{error::CliError, default_headers, format_url, Protocols}; |
2 | 2 | ||
3 | pub fn put(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> { | 3 | pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { |
4 | let res = reqwest::blocking::Client::new() | 4 | let res = reqwest::Client::new() |
5 | .put(format_url("device")?) | 5 | .put(format_url("device", Protocols::Http)?) |
6 | .headers(default_headers()?) | 6 | .headers(default_headers()?) |
7 | .body( | 7 | .body( |
8 | format!( | 8 | format!( |
9 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#, | 9 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, |
10 | id, | 10 | id, |
11 | mac, | 11 | mac, |
12 | broadcast_addr | 12 | broadcast_addr, |
13 | ip | ||
13 | ) | 14 | ) |
14 | ) | 15 | ) |
15 | .send() | 16 | .send() |
17 | .await | ||
16 | .map_err(CliError::Reqwest)? | 18 | .map_err(CliError::Reqwest)? |
17 | .text(); | 19 | .text() |
20 | .await; | ||
18 | 21 | ||
19 | println!("{:?}", res); | 22 | println!("{:?}", res); |
20 | Ok(()) | 23 | Ok(()) |
21 | } | 24 | } |
22 | 25 | ||
23 | pub fn get(id: String) -> Result<(), CliError> { | 26 | pub async fn get(id: String) -> Result<(), CliError> { |
24 | let res = reqwest::blocking::Client::new() | 27 | let res = reqwest::Client::new() |
25 | .get(format_url("device")?) | 28 | .get(format_url("device", Protocols::Http)?) |
26 | .headers(default_headers()?) | 29 | .headers(default_headers()?) |
27 | .body( | 30 | .body( |
28 | format!(r#"{{"id": "{}"}}"#, id) | 31 | format!(r#"{{"id": "{}"}}"#, id) |
29 | ) | 32 | ) |
30 | .send() | 33 | .send() |
34 | .await | ||
31 | .map_err(CliError::Reqwest)? | 35 | .map_err(CliError::Reqwest)? |
32 | .text(); | 36 | .text() |
37 | .await; | ||
33 | 38 | ||
34 | println!("{:?}", res); | 39 | println!("{:?}", res); |
35 | Ok(()) | 40 | Ok(()) |
36 | } | 41 | } |
37 | 42 | ||
38 | pub fn post(id: String, mac: String, broadcast_addr: String) -> Result<(), CliError> { | 43 | pub async fn post(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { |
39 | let res = reqwest::blocking::Client::new() | 44 | let res = reqwest::Client::new() |
40 | .post(format_url("device")?) | 45 | .post(format_url("device", Protocols::Http)?) |
41 | .headers(default_headers()?) | 46 | .headers(default_headers()?) |
42 | .body( | 47 | .body( |
43 | format!( | 48 | format!( |
44 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}"}}"#, | 49 | r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, |
45 | id, | 50 | id, |
46 | mac, | 51 | mac, |
47 | broadcast_addr | 52 | broadcast_addr, |
53 | ip | ||
48 | ) | 54 | ) |
49 | ) | 55 | ) |
50 | .send() | 56 | .send() |
57 | .await | ||
51 | .map_err(CliError::Reqwest)? | 58 | .map_err(CliError::Reqwest)? |
52 | .text(); | 59 | .text() |
60 | .await; | ||
53 | 61 | ||
54 | println!("{:?}", res); | 62 | println!("{:?}", res); |
55 | Ok(()) | 63 | Ok(()) |
diff --git a/src/requests/start.rs b/src/requests/start.rs index 30f65b9..d0c4411 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs | |||
@@ -1,37 +1,57 @@ | |||
1 | use std::time::Duration; | ||
2 | |||
3 | use futures_util::{StreamExt, SinkExt}; | ||
4 | use indicatif::{ProgressBar, ProgressStyle}; | ||
1 | use reqwest::StatusCode; | 5 | use reqwest::StatusCode; |
2 | use serde::Deserialize; | 6 | use serde::Deserialize; |
7 | use tokio_tungstenite::{connect_async, tungstenite::Message}; | ||
3 | 8 | ||
4 | use crate::{config::SETTINGS, error::CliError, default_headers, ErrorResponse}; | 9 | use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protocols}; |
5 | 10 | ||
6 | pub fn start(id: String) -> Result<(), CliError> { | 11 | pub async fn start(id: String, ping: bool) -> Result<(), CliError> { |
7 | let res = reqwest::blocking::Client::new() | 12 | |
8 | .post( | 13 | let send_start = ProgressBar::new(1); |
9 | format!( | 14 | |
10 | "{}/start", | 15 | // TODO: calculate average start-time on server |
11 | SETTINGS.get_string("server").map_err(CliError::Config)? | 16 | send_start.set_style( |
12 | ) | 17 | ProgressStyle::with_template("{spinner:.green} ({elapsed}) {wide_msg}") |
13 | ) | 18 | .unwrap() |
19 | .tick_chars("|/-\\\\") | ||
20 | ); | ||
21 | |||
22 | let url = format_url("start", Protocols::Http)?; | ||
23 | |||
24 | send_start.set_message(format!("connect to {}", url)); | ||
25 | send_start.enable_steady_tick(Duration::from_millis(125)); | ||
26 | |||
27 | let res = reqwest::Client::new() | ||
28 | .post(url) | ||
14 | .headers(default_headers()?) | 29 | .headers(default_headers()?) |
15 | .body( | 30 | .body( |
16 | format!(r#"{{"id": "{}"}}"#, id) | 31 | format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping) |
17 | ) | 32 | ) |
18 | .send() | 33 | .send() |
34 | .await | ||
19 | .map_err(CliError::Reqwest)?; | 35 | .map_err(CliError::Reqwest)?; |
20 | 36 | ||
21 | match res.status() { | 37 | match res.status() { |
22 | StatusCode::OK => { | 38 | StatusCode::OK => { |
23 | let body = serde_json::from_str::<StartResponse>( | 39 | let body = serde_json::from_str::<StartResponse>( |
24 | &res.text().map_err(CliError::Reqwest)? | 40 | &res.text().await.map_err(CliError::Reqwest)? |
25 | ) | 41 | ) |
26 | .map_err(CliError::Serde)?; | 42 | .map_err(CliError::Serde)?; |
27 | 43 | ||
28 | if body.boot { | 44 | if body.boot { |
29 | println!("successfully started {}", body.id); | 45 | send_start.println("connected, sent start packet"); |
46 | } | ||
47 | |||
48 | if ping { | ||
49 | send_start.println(status_socket(body.uuid, &send_start).await?.to_string()); | ||
30 | } | 50 | } |
31 | }, | 51 | }, |
32 | _ => { | 52 | _ => { |
33 | let body = serde_json::from_str::<ErrorResponse>( | 53 | let body = serde_json::from_str::<ErrorResponse>( |
34 | &res.text().map_err(CliError::Reqwest)? | 54 | &res.text().await.map_err(CliError::Reqwest)? |
35 | ) | 55 | ) |
36 | .map_err(CliError::Serde)?; | 56 | .map_err(CliError::Serde)?; |
37 | 57 | ||
@@ -42,8 +62,35 @@ pub fn start(id: String) -> Result<(), CliError> { | |||
42 | Ok(()) | 62 | Ok(()) |
43 | } | 63 | } |
44 | 64 | ||
65 | async fn status_socket(uuid: String, pb: &ProgressBar) -> Result<bool, CliError> { | ||
66 | pb.set_message("setup websocket"); | ||
67 | |||
68 | let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) | ||
69 | .await | ||
70 | .expect("Failed to connect"); | ||
71 | pb.println("connected to websocket"); | ||
72 | |||
73 | pb.set_message("send uuid message"); | ||
74 | ws_stream.send(Message::Text(uuid)).await.unwrap(); | ||
75 | pb.println("sent uuid message"); | ||
76 | |||
77 | pb.set_message("wait for message"); | ||
78 | let msg = ws_stream.next().await.unwrap(); | ||
79 | |||
80 | pb.println(format!("msg: {:?}", msg)); | ||
81 | |||
82 | ws_stream.close(None).await.unwrap(); | ||
83 | pb.println("connection closed"); | ||
84 | // TODO: Check for correct UUID and timeout | ||
85 | pb.set_message("verifying message"); | ||
86 | if msg.is_ok() { return Ok(true) } | ||
87 | |||
88 | Ok(false) | ||
89 | } | ||
90 | |||
45 | #[derive(Debug, Deserialize)] | 91 | #[derive(Debug, Deserialize)] |
46 | struct StartResponse { | 92 | struct StartResponse { |
47 | boot: bool, | 93 | boot: bool, |
48 | id: String, | 94 | id: String, |
95 | uuid: String, | ||
49 | } | 96 | } |