summaryrefslogtreecommitdiff
path: root/src/requests
diff options
context:
space:
mode:
Diffstat (limited to 'src/requests')
-rw-r--r--src/requests/device.rs62
-rw-r--r--src/requests/mod.rs2
-rw-r--r--src/requests/start.rs103
3 files changed, 91 insertions, 76 deletions
diff --git a/src/requests/device.rs b/src/requests/device.rs
index cbc838e..5003c4a 100644
--- a/src/requests/device.rs
+++ b/src/requests/device.rs
@@ -1,20 +1,21 @@
1use crate::{error::CliError, default_headers, format_url, Protocols}; 1use crate::{config::Config, default_headers, error::CliError, format_url, Protocols};
2 2
3pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { 3pub async fn put(
4 let url = format_url("device", Protocols::Http)?; 4 config: &Config,
5 id: String,
6 mac: String,
7 broadcast_addr: String,
8 ip: String,
9) -> Result<(), CliError> {
10 let url = format_url(config, "device", Protocols::Http)?;
5 println!("{}", url); 11 println!("{}", url);
6 let res = reqwest::Client::new() 12 let res = reqwest::Client::new()
7 .put(url) 13 .put(url)
8 .headers(default_headers()?) 14 .headers(default_headers(config)?)
9 .body( 15 .body(format!(
10 format!( 16 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#,
11 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, 17 id, mac, broadcast_addr, ip
12 id, 18 ))
13 mac,
14 broadcast_addr,
15 ip
16 )
17 )
18 .send() 19 .send()
19 .await 20 .await
20 .map_err(CliError::Reqwest)? 21 .map_err(CliError::Reqwest)?
@@ -25,13 +26,11 @@ pub async fn put(id: String, mac: String, broadcast_addr: String, ip: String) ->
25 Ok(()) 26 Ok(())
26} 27}
27 28
28pub async fn get(id: String) -> Result<(), CliError> { 29pub async fn get(config: &Config, id: String) -> Result<(), CliError> {
29 let res = reqwest::Client::new() 30 let res = reqwest::Client::new()
30 .get(format_url("device", Protocols::Http)?) 31 .get(format_url(config, "device", Protocols::Http)?)
31 .headers(default_headers()?) 32 .headers(default_headers(config)?)
32 .body( 33 .body(format!(r#"{{"id": "{}"}}"#, id))
33 format!(r#"{{"id": "{}"}}"#, id)
34 )
35 .send() 34 .send()
36 .await 35 .await
37 .map_err(CliError::Reqwest)? 36 .map_err(CliError::Reqwest)?
@@ -42,19 +41,20 @@ pub async fn get(id: String) -> Result<(), CliError> {
42 Ok(()) 41 Ok(())
43} 42}
44 43
45pub async fn post(id: String, mac: String, broadcast_addr: String, ip: String) -> Result<(), CliError> { 44pub async fn post(
45 config: &Config,
46 id: String,
47 mac: String,
48 broadcast_addr: String,
49 ip: String,
50) -> Result<(), CliError> {
46 let res = reqwest::Client::new() 51 let res = reqwest::Client::new()
47 .post(format_url("device", Protocols::Http)?) 52 .post(format_url(config, "device", Protocols::Http)?)
48 .headers(default_headers()?) 53 .headers(default_headers(config)?)
49 .body( 54 .body(format!(
50 format!( 55 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#,
51 r#"{{"id": "{}", "mac": "{}", "broadcast_addr": "{}", "ip": "{}"}}"#, 56 id, mac, broadcast_addr, ip
52 id, 57 ))
53 mac,
54 broadcast_addr,
55 ip
56 )
57 )
58 .send() 58 .send()
59 .await 59 .await
60 .map_err(CliError::Reqwest)? 60 .map_err(CliError::Reqwest)?
diff --git a/src/requests/mod.rs b/src/requests/mod.rs
deleted file mode 100644
index 6855db1..0000000
--- a/src/requests/mod.rs
+++ /dev/null
@@ -1,2 +0,0 @@
1pub mod start;
2pub mod device;
diff --git a/src/requests/start.rs b/src/requests/start.rs
index ca4ca44..bc63303 100644
--- a/src/requests/start.rs
+++ b/src/requests/start.rs
@@ -1,25 +1,26 @@
1use futures_util::{StreamExt, SinkExt}; 1use futures_util::{SinkExt, StreamExt};
2use indicatif::{MultiProgress, ProgressBar}; 2use indicatif::{MultiProgress, ProgressBar};
3use reqwest::StatusCode; 3use reqwest::StatusCode;
4use serde::Deserialize; 4use serde::Deserialize;
5use tokio_tungstenite::{connect_async, tungstenite::Message}; 5use tokio_tungstenite::{connect_async, tungstenite::Message};
6 6
7use crate::{error::CliError, default_headers, ErrorResponse, format_url, Protocols, OVERVIEW_STYLE, DEFAULT_STYLE, DONE_STYLE, finish_pb, ERROR_STYLE, OVERVIEW_ERROR, OVERVIEW_DONE, add_pb}; 7use crate::{
8 8 add_pb, config::Config, default_headers, error::CliError, finish_pb, format_url, ErrorResponse,
9pub async fn start(id: String, ping: bool) -> Result<(), CliError> { 9 Protocols, DEFAULT_STYLE, DONE_STYLE, ERROR_STYLE, OVERVIEW_DONE, OVERVIEW_ERROR,
10 OVERVIEW_STYLE,
11};
10 12
13pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), CliError> {
11 let send_start = MultiProgress::new(); 14 let send_start = MultiProgress::new();
12 let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id)); 15 let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id));
13 16
14 // TODO: calculate average start-time on server 17 // TODO: calculate average start-time on server
15 let url = format_url("start", Protocols::Http)?; 18 let url = format_url(config, "start", Protocols::Http)?;
16 let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {}", url)); 19 let connect = add_pb(&send_start, DEFAULT_STYLE, format!("connect to {}", url));
17 let res = reqwest::Client::new() 20 let res = reqwest::Client::new()
18 .post(url) 21 .post(url)
19 .headers(default_headers()?) 22 .headers(default_headers(config)?)
20 .body( 23 .body(format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping))
21 format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)
22 )
23 .send() 24 .send()
24 .await 25 .await
25 .map_err(CliError::Reqwest)?; 26 .map_err(CliError::Reqwest)?;
@@ -29,7 +30,7 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
29 match res.status() { 30 match res.status() {
30 StatusCode::OK => { 31 StatusCode::OK => {
31 let body = serde_json::from_str::<StartResponse>( 32 let body = serde_json::from_str::<StartResponse>(
32 &res.text().await.map_err(CliError::Reqwest)? 33 &res.text().await.map_err(CliError::Reqwest)?,
33 ) 34 )
34 .map_err(CliError::Serde)?; 35 .map_err(CliError::Serde)?;
35 36
@@ -38,17 +39,25 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
38 } 39 }
39 40
40 if ping { 41 if ping {
41 let status = status_socket(body.uuid, &send_start, &overview, id).await?; 42 let status = status_socket(config, body.uuid, &send_start, &overview, id).await?;
42 if status { 43 if status {
43 finish_pb(overview, format!("successfully started {}", body.id), OVERVIEW_DONE); 44 finish_pb(
45 overview,
46 format!("successfully started {}", body.id),
47 OVERVIEW_DONE,
48 );
44 } else { 49 } else {
45 finish_pb(overview, format!("error while starting {}", body.id), OVERVIEW_ERROR); 50 finish_pb(
51 overview,
52 format!("error while starting {}", body.id),
53 OVERVIEW_ERROR,
54 );
46 } 55 }
47 } 56 }
48 }, 57 }
49 _ => { 58 _ => {
50 let body = serde_json::from_str::<ErrorResponse>( 59 let body = serde_json::from_str::<ErrorResponse>(
51 &res.text().await.map_err(CliError::Reqwest)? 60 &res.text().await.map_err(CliError::Reqwest)?,
52 ) 61 )
53 .map_err(CliError::Serde)?; 62 .map_err(CliError::Serde)?;
54 63
@@ -59,16 +68,22 @@ pub async fn start(id: String, ping: bool) -> Result<(), CliError> {
59 Ok(()) 68 Ok(())
60} 69}
61 70
62async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, id: String) -> Result<bool, CliError> { 71async fn status_socket(
63 // TODO: Remove unwraps 72 config: &Config,
73 uuid: String,
74 pb: &MultiProgress,
75 overview: &ProgressBar,
76 id: String,
77) -> Result<bool, CliError> {
64 let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); 78 let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string());
65 let (mut ws_stream, _response) = connect_async(format_url("status", Protocols::Websocket)?) 79 let (mut ws_stream, _response) =
66 .await 80 connect_async(format_url(config, "status", Protocols::Websocket)?)
67 .expect("Failed to connect"); 81 .await
82 .expect("Failed to connect");
68 finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); 83 finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE);
69 84
70 ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); 85 ws_stream.send(Message::Text(uuid.clone())).await.unwrap();
71 86
72 // Get ETA 87 // Get ETA
73 let eta_msg = ws_stream.next().await.unwrap().unwrap(); 88 let eta_msg = ws_stream.next().await.unwrap().unwrap();
74 let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs(); 89 let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs();
@@ -86,29 +101,29 @@ async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar,
86 Verified::WrongUuid => { 101 Verified::WrongUuid => {
87 finish_pb(v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); 102 finish_pb(v_pb, "returned wrong uuid".to_string(), ERROR_STYLE);
88 Ok(false) 103 Ok(false)
89 },
90 Verified::ResponseType(res_type) => {
91 match res_type {
92 ResponseType::Start => {
93 finish_pb(v_pb, "device started".to_string(), DONE_STYLE);
94 Ok(true)
95 },
96 ResponseType::Timeout => {
97 finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE);
98 Ok(false)
99 },
100 ResponseType::NotFound => {
101 finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE);
102 Ok(false)
103 },
104 }
105 } 104 }
105 Verified::ResponseType(res_type) => match res_type {
106 ResponseType::Start => {
107 finish_pb(v_pb, "device started".to_string(), DONE_STYLE);
108 Ok(true)
109 }
110 ResponseType::Timeout => {
111 finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE);
112 Ok(false)
113 }
114 ResponseType::NotFound => {
115 finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE);
116 Ok(false)
117 }
118 },
106 } 119 }
107} 120}
108 121
109fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> { 122fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> {
110 let spl: Vec<&str> = msg.split('_').collect(); 123 let spl: Vec<&str> = msg.split('_').collect();
111 if (spl[0] != "eta") || (spl[2] != uuid) { return Err(CliError::WsResponse); }; 124 if (spl[0] != "eta") || (spl[2] != uuid) {
125 return Err(CliError::WsResponse);
126 };
112 Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?) 127 Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?)
113} 128}
114 129
@@ -116,9 +131,11 @@ fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError>
116 let spl: Vec<&str> = res.split('_').collect(); 131 let spl: Vec<&str> = res.split('_').collect();
117 let res_type = spl[0]; 132 let res_type = spl[0];
118 let uuid = spl[1]; 133 let uuid = spl[1];
119 134
120 if uuid != org_uuid { return Ok(Verified::WrongUuid) }; 135 if uuid != org_uuid {
121 136 return Ok(Verified::WrongUuid);
137 };
138
122 Ok(Verified::ResponseType(ResponseType::from(res_type)?)) 139 Ok(Verified::ResponseType(ResponseType::from(res_type)?))
123} 140}
124 141
@@ -131,7 +148,7 @@ struct StartResponse {
131 148
132enum Verified { 149enum Verified {
133 ResponseType(ResponseType), 150 ResponseType(ResponseType),
134 WrongUuid 151 WrongUuid,
135} 152}
136 153
137enum ResponseType { 154enum ResponseType {