diff options
Diffstat (limited to 'src/requests')
-rw-r--r-- | src/requests/device.rs | 23 | ||||
-rw-r--r-- | src/requests/start.rs | 24 |
2 files changed, 28 insertions, 19 deletions
diff --git a/src/requests/device.rs b/src/requests/device.rs index a612978..7583406 100644 --- a/src/requests/device.rs +++ b/src/requests/device.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use crate::{config::Config, default_headers, error::Error, format_url, Protocols}; | 1 | use crate::{check_success, config::Config, default_headers, error::Error, format_url, Protocols}; |
2 | 2 | ||
3 | pub async fn put( | 3 | pub async fn put( |
4 | config: &Config, | 4 | config: &Config, |
@@ -16,11 +16,10 @@ pub async fn put( | |||
16 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, | 16 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, |
17 | )) | 17 | )) |
18 | .send() | 18 | .send() |
19 | .await? | 19 | .await?; |
20 | .text() | ||
21 | .await; | ||
22 | 20 | ||
23 | println!("{res:?}"); | 21 | let body = check_success(res).await?; |
22 | println!("{body}"); | ||
24 | Ok(()) | 23 | Ok(()) |
25 | } | 24 | } |
26 | 25 | ||
@@ -30,11 +29,10 @@ pub async fn get(config: &Config, id: String) -> Result<(), Error> { | |||
30 | .headers(default_headers(config)?) | 29 | .headers(default_headers(config)?) |
31 | .body(format!(r#"{{"id": "{id}"}}"#)) | 30 | .body(format!(r#"{{"id": "{id}"}}"#)) |
32 | .send() | 31 | .send() |
33 | .await? | 32 | .await?; |
34 | .text() | ||
35 | .await; | ||
36 | 33 | ||
37 | println!("{res:?}"); | 34 | let body = check_success(res).await?; |
35 | println!("{body}"); | ||
38 | Ok(()) | 36 | Ok(()) |
39 | } | 37 | } |
40 | 38 | ||
@@ -52,10 +50,9 @@ pub async fn post( | |||
52 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, | 50 | r#"{{"id": "{id}", "mac": "{mac}", "broadcast_addr": "{broadcast_addr}", "ip": "{ip}"}}"#, |
53 | )) | 51 | )) |
54 | .send() | 52 | .send() |
55 | .await? | 53 | .await?; |
56 | .text() | ||
57 | .await; | ||
58 | 54 | ||
59 | println!("{res:?}"); | 55 | let body = check_success(res).await?; |
56 | println!("{body}"); | ||
60 | Ok(()) | 57 | Ok(()) |
61 | } | 58 | } |
diff --git a/src/requests/start.rs b/src/requests/start.rs index 7abbbe0..d07177e 100644 --- a/src/requests/start.rs +++ b/src/requests/start.rs | |||
@@ -2,7 +2,10 @@ 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::{ |
6 | connect_async, | ||
7 | tungstenite::{http::Request, Message}, | ||
8 | }; | ||
6 | 9 | ||
7 | use crate::{ | 10 | use crate::{ |
8 | add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, | 11 | add_pb, config::Config, default_headers, error::Error, finish_pb, format_url, ErrorResponse, |
@@ -66,17 +69,26 @@ async fn status_socket( | |||
66 | id: String, | 69 | id: String, |
67 | ) -> Result<bool, Error> { | 70 | ) -> Result<bool, Error> { |
68 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); | 71 | let ws_pb = add_pb(pb, DEFAULT_STYLE, "connect to websocket".to_string()); |
69 | let (mut ws_stream, _response) = | 72 | |
70 | connect_async(format_url(config, "status", &Protocols::Websocket)) | 73 | let request = Request::builder() |
71 | .await | 74 | .uri(format_url(config, "status", &Protocols::Websocket)) |
72 | .expect("Failed to connect"); | 75 | .header("Authorization", &config.apikey) |
76 | .header("sec-websocket-key", "") | ||
77 | .header("host", &config.server) | ||
78 | .header("upgrade", "websocket") | ||
79 | .header("connection", "upgrade") | ||
80 | .header("sec-websocket-version", 13) | ||
81 | .body(()) | ||
82 | .unwrap(); | ||
83 | |||
84 | let (mut ws_stream, _response) = connect_async(request).await?; | ||
73 | finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); | 85 | finish_pb(&ws_pb, "connected to websocket".to_string(), DONE_STYLE); |
74 | 86 | ||
75 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); | 87 | ws_stream.send(Message::Text(uuid.clone())).await.unwrap(); |
76 | 88 | ||
77 | // Get ETA | 89 | // Get ETA |
78 | let eta_msg = ws_stream.next().await.unwrap().unwrap(); | 90 | let eta_msg = ws_stream.next().await.unwrap().unwrap(); |
79 | let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)? + overview.elapsed().as_secs(); | 91 | let eta = get_eta(&eta_msg.into_text().unwrap(), &uuid)?; |
80 | overview.set_message(format!("/{eta}) start {id}")); | 92 | overview.set_message(format!("/{eta}) start {id}")); |
81 | 93 | ||
82 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); | 94 | let msg_pb = add_pb(pb, DEFAULT_STYLE, "await message".to_string()); |