diff options
author | FxQnLr <[email protected]> | 2024-02-25 16:54:03 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-25 16:54:03 +0100 |
commit | 465a71b6780921fb7ec19682702cbe864decd212 (patch) | |
tree | 66d9a386045a98e95a50d7f84e7e3044b578a163 /src/requests/start.rs | |
parent | 03bea24f9de698375033af92a08762446d0e20cc (diff) | |
download | webol-cli-465a71b6780921fb7ec19682702cbe864decd212.tar webol-cli-465a71b6780921fb7ec19682702cbe864decd212.tar.gz webol-cli-465a71b6780921fb7ec19682702cbe864decd212.zip |
Closes #3. Use thiserror. Fix clippy stuff
Diffstat (limited to 'src/requests/start.rs')
-rw-r--r-- | src/requests/start.rs | 108 |
1 files changed, 49 insertions, 59 deletions
diff --git a/src/requests/start.rs b/src/requests/start.rs index bc63303..7abbbe0 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs | |||
@@ -5,64 +5,54 @@ use serde::Deserialize; | |||
5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; | 5 | use tokio_tungstenite::{connect_async, tungstenite::Message}; |
6 | 6 | ||
7 | use crate::{ | 7 | use crate::{ |
8 | add_pb, config::Config, default_headers, error::CliError, finish_pb, format_url, ErrorResponse, | 8 | add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, |
9 | Protocols, DEFAULT_STYLE, DONE_STYLE, ERROR_STYLE, OVERVIEW_DONE, OVERVIEW_ERROR, | 9 | Protocols, DEFAULT_STYLE, DONE_STYLE, ERROR_STYLE, OVERVIEW_DONE, OVERVIEW_ERROR, |
10 | OVERVIEW_STYLE, | 10 | OVERVIEW_STYLE, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), CliError> { | 13 | pub async fn start(config: &Config, id: String, ping: bool) -> Result<(), Error> { |
14 | let send_start = MultiProgress::new(); | 14 | let send_start = MultiProgress::new(); |
15 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {}", id)); | 15 | let overview = add_pb(&send_start, OVERVIEW_STYLE, format!(") start {id}")); |
16 | 16 | ||
17 | // TODO: calculate average start-time on server | 17 | let url = format_url(config, "start", &Protocols::Http); |
18 | let url = format_url(config, "start", Protocols::Http)?; | 18 | 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)); | ||
20 | let res = reqwest::Client::new() | 19 | let res = reqwest::Client::new() |
21 | .post(url) | 20 | .post(url) |
22 | .headers(default_headers(config)?) | 21 | .headers(default_headers(config)?) |
23 | .body(format!(r#"{{"id": "{}", "ping": {}}}"#, id, ping)) | 22 | .body(format!(r#"{{"id": "{id}", "ping": {ping}}}"#)) |
24 | .send() | 23 | .send() |
25 | .await | 24 | .await?; |
26 | .map_err(CliError::Reqwest)?; | 25 | finish_pb(&connect, "connected, got response".to_string(), DONE_STYLE); |
27 | finish_pb(connect, "connected, got response".to_string(), DONE_STYLE); | ||
28 | 26 | ||
29 | let res_pb = add_pb(&send_start, DEFAULT_STYLE, "analyzing response".to_string()); | 27 | let res_pb = add_pb(&send_start, DEFAULT_STYLE, "analyzing response".to_string()); |
30 | match res.status() { | ||
31 | StatusCode::OK => { | ||
32 | let body = serde_json::from_str::<StartResponse>( | ||
33 | &res.text().await.map_err(CliError::Reqwest)?, | ||
34 | ) | ||
35 | .map_err(CliError::Serde)?; | ||
36 | |||
37 | if body.boot { | ||
38 | finish_pb(res_pb, "sent start packet".to_string(), DONE_STYLE); | ||
39 | } | ||
40 | 28 | ||
41 | if ping { | 29 | if res.status() == StatusCode::OK { |
42 | let status = status_socket(config, body.uuid, &send_start, &overview, id).await?; | 30 | let body = serde_json::from_str::<StartResponse>(&res.text().await?)?; |
43 | if status { | 31 | |
44 | finish_pb( | 32 | if body.boot { |
45 | overview, | 33 | finish_pb(&res_pb, "sent start packet".to_string(), DONE_STYLE); |
46 | format!("successfully started {}", body.id), | ||
47 | OVERVIEW_DONE, | ||
48 | ); | ||
49 | } else { | ||
50 | finish_pb( | ||
51 | overview, | ||
52 | format!("error while starting {}", body.id), | ||
53 | OVERVIEW_ERROR, | ||
54 | ); | ||
55 | } | ||
56 | } | ||
57 | } | 34 | } |
58 | _ => { | ||
59 | let body = serde_json::from_str::<ErrorResponse>( | ||
60 | &res.text().await.map_err(CliError::Reqwest)?, | ||
61 | ) | ||
62 | .map_err(CliError::Serde)?; | ||
63 | 35 | ||
64 | res_pb.finish_with_message(format!("✗ got error: {}", body.error)); | 36 | if ping { |
37 | let status = status_socket(config, body.uuid, &send_start, &overview, id).await?; | ||
38 | if status { | ||
39 | finish_pb( | ||
40 | &overview, | ||
41 | format!("successfully started {}", body.id), | ||
42 | OVERVIEW_DONE, | ||
43 | ); | ||
44 | } else { | ||
45 | finish_pb( | ||
46 | &overview, | ||
47 | format!("error while starting {}", body.id), | ||
48 | OVERVIEW_ERROR, | ||
49 | ); | ||
50 | } | ||
65 | } | 51 | } |
52 | } else { | ||
53 | let body = serde_json::from_str::<ErrorResponse>(&res.text().await?)?; | ||
54 | |||
55 | res_pb.finish_with_message(format!("✗ got error: {}", body.error)); | ||
66 | } | 56 | } |
67 | 57 | ||
68 | Ok(()) | 58 | Ok(()) |
@@ -74,60 +64,60 @@ async fn status_socket( | |||
74 | pb: &MultiProgress, | 64 | pb: &MultiProgress, |
75 | overview: &ProgressBar, | 65 | overview: &ProgressBar, |
76 | id: String, | 66 | id: String, |
77 | ) -> Result<bool, CliError> { | 67 | ) -> Result<bool, Error> { |
78 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); | 68 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); |
79 | let (mut ws_stream, _response) = | 69 | let (mut ws_stream, _response) = |
80 | connect_async(format_url(config, "status", Protocols::Websocket)?) | 70 | connect_async(format_url(config, "status", &Protocols::Websocket)) |
81 | .await | 71 | .await |
82 | .expect("Failed to connect"); | 72 | .expect("Failed to connect"); |
83 | finish_pb(ws_pb, "connected to websocket".to_string(), DONE_STYLE); | 73 | finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); |
84 | 74 | ||
85 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); | 75 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); |
86 | 76 | ||
87 | // Get ETA | 77 | // Get ETA |
88 | let eta_msg = ws_stream.next().await.unwrap().unwrap(); | 78 | let eta_msg = ws_stream.next().await.unwrap().unwrap(); |
89 | let eta = get_eta(eta_msg.into_text().unwrap(), uuid.clone())? + overview.elapsed().as_secs(); | 79 | let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)? + overview.elapsed().as_secs(); |
90 | overview.set_message(format!("/{}) start {}", eta, id)); | 80 | overview.set_message(format!("/{eta}) start {id}")); |
91 | 81 | ||
92 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); | 82 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); |
93 | let msg = ws_stream.next().await.unwrap(); | 83 | let msg = ws_stream.next().await.unwrap(); |
94 | finish_pb(msg_pb, "received message".to_string(), DONE_STYLE); | 84 | finish_pb(&msg_pb, "received message".to_string(), DONE_STYLE); |
95 | 85 | ||
96 | ws_stream.close(None).await.unwrap(); | 86 | ws_stream.close(None).await.unwrap(); |
97 | 87 | ||
98 | let v_pb = add_pb(pb, DEFAULT_STYLE, "verify response".to_string()); | 88 | let v_pb = add_pb(pb, DEFAULT_STYLE, "verify response".to_string()); |
99 | let res = verify_response(msg.unwrap().to_string(), uuid)?; | 89 | let res = verify_response(&msg.unwrap().to_string(), &uuid)?; |
100 | match res { | 90 | match res { |
101 | Verified::WrongUuid => { | 91 | Verified::WrongUuid => { |
102 | finish_pb(v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); | 92 | finish_pb(&v_pb, "returned wrong uuid".to_string(), ERROR_STYLE); |
103 | Ok(false) | 93 | Ok(false) |
104 | } | 94 | } |
105 | Verified::ResponseType(res_type) => match res_type { | 95 | Verified::ResponseType(res_type) => match res_type { |
106 | ResponseType::Start => { | 96 | ResponseType::Start => { |
107 | finish_pb(v_pb, "device started".to_string(), DONE_STYLE); | 97 | finish_pb(&v_pb, "device started".to_string(), DONE_STYLE); |
108 | Ok(true) | 98 | Ok(true) |
109 | } | 99 | } |
110 | ResponseType::Timeout => { | 100 | ResponseType::Timeout => { |
111 | finish_pb(v_pb, "ping timed out".to_string(), ERROR_STYLE); | 101 | finish_pb(&v_pb, "ping timed out".to_string(), ERROR_STYLE); |
112 | Ok(false) | 102 | Ok(false) |
113 | } | 103 | } |
114 | ResponseType::NotFound => { | 104 | ResponseType::NotFound => { |
115 | finish_pb(v_pb, "unknown uuid".to_string(), ERROR_STYLE); | 105 | finish_pb(&v_pb, "unknown uuid".to_string(), ERROR_STYLE); |
116 | Ok(false) | 106 | Ok(false) |
117 | } | 107 | } |
118 | }, | 108 | }, |
119 | } | 109 | } |
120 | } | 110 | } |
121 | 111 | ||
122 | fn get_eta(msg: String, uuid: String) -> Result<u64, CliError> { | 112 | fn get_eta(msg: &str, uuid: &str) -> Result<u64, Error> { |
123 | let spl: Vec<&str> = msg.split('_').collect(); | 113 | let spl: Vec<&str> = msg.split('_').collect(); |
124 | if (spl[0] != "eta") || (spl[2] != uuid) { | 114 | if (spl[0] != "eta") || (spl[2] != uuid) { |
125 | return Err(CliError::WsResponse); | 115 | return Err(Error::WsResponse); |
126 | }; | 116 | }; |
127 | Ok(u64::from_str_radix(spl[1], 10).map_err(CliError::Parse)?) | 117 | Ok(spl[1].parse()?) |
128 | } | 118 | } |
129 | 119 | ||
130 | fn verify_response(res: String, org_uuid: String) -> Result<Verified, CliError> { | 120 | fn verify_response(res: &str, org_uuid: &str) -> Result<Verified, Error> { |
131 | let spl: Vec<&str> = res.split('_').collect(); | 121 | let spl: Vec<&str> = res.split('_').collect(); |
132 | let res_type = spl[0]; | 122 | let res_type = spl[0]; |
133 | let uuid = spl[1]; | 123 | let uuid = spl[1]; |
@@ -158,12 +148,12 @@ enum ResponseType { | |||
158 | } | 148 | } |
159 | 149 | ||
160 | impl ResponseType { | 150 | impl ResponseType { |
161 | fn from(value: &str) -> Result<Self, CliError> { | 151 | fn from(value: &str) -> Result<Self, Error> { |
162 | match value { | 152 | match value { |
163 | "start" => Ok(ResponseType::Start), | 153 | "start" => Ok(ResponseType::Start), |
164 | "timeout" => Ok(ResponseType::Timeout), | 154 | "timeout" => Ok(ResponseType::Timeout), |
165 | "notfound" => Ok(ResponseType::NotFound), | 155 | "notfound" => Ok(ResponseType::NotFound), |
166 | _ => Err(CliError::WsResponse), | 156 | _ => Err(Error::WsResponse), |
167 | } | 157 | } |
168 | } | 158 | } |
169 | } | 159 | } |