diff options
author | FxQnLr <[email protected]> | 2024-02-25 20:00:38 +0100 |
---|---|---|
committer | FxQnLr <[email protected]> | 2024-02-25 20:00:38 +0100 |
commit | a192e9baca9a14beaa9f87c27a63cff96aa41c94 (patch) | |
tree | 1051968707fc43405ea31fc1878c448c71f6fe34 /src/main.rs | |
parent | 465a71b6780921fb7ec19682702cbe864decd212 (diff) | |
download | webol-cli-a192e9baca9a14beaa9f87c27a63cff96aa41c94.tar webol-cli-a192e9baca9a14beaa9f87c27a63cff96aa41c94.tar.gz webol-cli-a192e9baca9a14beaa9f87c27a63cff96aa41c94.zip |
Closes #4. Auth on Websocket. Small stuff
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 33 |
1 files changed, 19 insertions, 14 deletions
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}; | |||
6 | use error::Error; | 6 | use error::Error; |
7 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; | 7 | use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; |
8 | use requests::{device, start::start}; | 8 | use requests::{device, start::start}; |
9 | use reqwest::header::{HeaderMap, HeaderValue}; | 9 | use reqwest::{ |
10 | header::{HeaderMap, HeaderValue}, | ||
11 | Response, | ||
12 | }; | ||
10 | use serde::Deserialize; | 13 | use serde::Deserialize; |
11 | 14 | ||
12 | mod config; | 15 | mod config; |
@@ -66,7 +69,7 @@ enum DeviceCmd { | |||
66 | } | 69 | } |
67 | 70 | ||
68 | #[tokio::main] | 71 | #[tokio::main] |
69 | async fn main() -> Result<(), Error> { | 72 | async fn main() -> Result<(), anyhow::Error> { |
70 | let config = Config::load()?; | 73 | let config = Config::load()?; |
71 | 74 | ||
72 | let cli = Args::parse(); | 75 | let cli = Args::parse(); |
@@ -112,18 +115,9 @@ fn print_completions<G: Generator>(gen: G, cmd: &mut Command) { | |||
112 | 115 | ||
113 | fn default_headers(config: &Config) -> Result<HeaderMap, Error> { | 116 | fn default_headers(config: &Config) -> Result<HeaderMap, Error> { |
114 | let mut map = HeaderMap::new(); | 117 | let mut map = HeaderMap::new(); |
115 | map.append( | 118 | map.append("Accept-Content", HeaderValue::from_str("application/json")?); |
116 | "Accept-Content", | 119 | map.append("Content-Type", HeaderValue::from_str("application/json")?); |
117 | HeaderValue::from_str("application/json")? | 120 | map.append("Authorization", HeaderValue::from_str(&config.apikey)?); |
118 | ); | ||
119 | map.append( | ||
120 | "Content-Type", | ||
121 | HeaderValue::from_str("application/json")? | ||
122 | ); | ||
123 | map.append( | ||
124 | "Authorization", | ||
125 | HeaderValue::from_str(&config.apikey)? | ||
126 | ); | ||
127 | 121 | ||
128 | Ok(map) | 122 | Ok(map) |
129 | } | 123 | } |
@@ -132,6 +126,17 @@ fn format_url(config: &Config, path: &str, protocol: &Protocols) -> String { | |||
132 | format!("{}://{}/{}", protocol, config.server, path) | 126 | format!("{}://{}/{}", protocol, config.server, path) |
133 | } | 127 | } |
134 | 128 | ||
129 | async fn check_success(res: Response) -> Result<String, Error> { | ||
130 | let status = res.status(); | ||
131 | if status.is_success() { | ||
132 | Ok(res.text().await?) | ||
133 | } else if status.as_u16() == 401 { | ||
134 | Err(Error::Authorization) | ||
135 | } else { | ||
136 | Err(Error::HttpStatus(status.as_u16())) | ||
137 | } | ||
138 | } | ||
139 | |||
135 | fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { | 140 | fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { |
136 | let pb = mp.add(ProgressBar::new(1)); | 141 | let pb = mp.add(ProgressBar::new(1)); |
137 | pb.set_style(ProgressStyle::with_template(template).unwrap()); | 142 | pb.set_style(ProgressStyle::with_template(template).unwrap()); |