diff options
Diffstat (limited to 'src/requests/start.rs')
-rw-r--r-- | src/requests/start.rs | 103 |
1 files changed, 60 insertions, 43 deletions
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 @@ | |||
1 | use futures_util::{StreamExt, SinkExt}; | 1 | use futures_util::{SinkExt, StreamExt}; |
2 | use indicatif::{MultiProgress, ProgressBar}; | 2 | use indicatif::{MultiProgress, ProgressBar}; |
3 | use reqwest::StatusCode; | 3 | use reqwest::StatusCode; |
4 | use serde::Deserialize; | 4 | use serde::Deserialize; |
5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; | 5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; |
6 | 6 | ||
7 | use 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}; | 7 | use crate::{ |
8 | 8 | add_pb, config::Config, default_headers, error::CliError, finish_pb, format_url, ErrorResponse, | |
9 | pub 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 | ||
13 | pub 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 | ||
62 | async fn status_socket(uuid: String, pb: &MultiProgress, overview: &ProgressBar, id: String) -> Result<bool, CliError> { | 71 | async 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 | ||
109 | fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> { | 122 | fn 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 | ||
132 | enum Verified { | 149 | enum Verified { |
133 | ResponseType(ResponseType), | 150 | ResponseType(ResponseType), |
134 | WrongUuid | 151 | WrongUuid, |
135 | } | 152 | } |
136 | 153 | ||
137 | enum ResponseType { | 154 | enum ResponseType { |