From a192e9baca9a14beaa9f87c27a63cff96aa41c94 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Sun, 25 Feb 2024 20:00:38 +0100 Subject: Closes #4. Auth on Websocket. Small stuff --- src/main.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index cdca6cb..d76341f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,7 +6,10 @@ use clap_complete::{generate, Generator, Shell}; use error::Error; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use requests::{device, start::start}; -use reqwest::header::{HeaderMap, HeaderValue}; +use reqwest::{ + header::{HeaderMap, HeaderValue}, + Response, +}; use serde::Deserialize; mod config; @@ -66,7 +69,7 @@ enum DeviceCmd { } #[tokio::main] -async fn main() -> Result<(), Error> { +async fn main() -> Result<(), anyhow::Error> { let config = Config::load()?; let cli = Args::parse(); @@ -112,18 +115,9 @@ fn print_completions(gen: G, cmd: &mut Command) { fn default_headers(config: &Config) -> Result { let mut map = HeaderMap::new(); - map.append( - "Accept-Content", - HeaderValue::from_str("application/json")? - ); - map.append( - "Content-Type", - HeaderValue::from_str("application/json")? - ); - map.append( - "Authorization", - HeaderValue::from_str(&config.apikey)? - ); + map.append("Accept-Content", HeaderValue::from_str("application/json")?); + map.append("Content-Type", HeaderValue::from_str("application/json")?); + map.append("Authorization", HeaderValue::from_str(&config.apikey)?); Ok(map) } @@ -132,6 +126,17 @@ fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { format!("{}://{}/{}", protocol, config.server, path) } +async fn check_success(res: Response) -> Result { + let status = res.status(); + if status.is_success() { + Ok(res.text().await?) + } else if status.as_u16() == 401 { + Err(Error::Authorization) + } else { + Err(Error::HttpStatus(status.as_u16())) + } +} + fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { let pb = mp.add(ProgressBar::new(1)); pb.set_style(ProgressStyle::with_template(template).unwrap()); -- cgit v1.2.3