summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorFxQnLr <[email protected]>2024-02-25 20:00:38 +0100
committerFxQnLr <[email protected]>2024-02-25 20:00:38 +0100
commita192e9baca9a14beaa9f87c27a63cff96aa41c94 (patch)
tree1051968707fc43405ea31fc1878c448c71f6fe34 /src/main.rs
parent465a71b6780921fb7ec19682702cbe864decd212 (diff)
downloadwebol-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.rs33
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};
6use error::Error; 6use error::Error;
7use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; 7use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
8use requests::{device, start::start}; 8use requests::{device, start::start};
9use reqwest::header::{HeaderMap, HeaderValue}; 9use reqwest::{
10 header::{HeaderMap, HeaderValue},
11 Response,
12};
10use serde::Deserialize; 13use serde::Deserialize;
11 14
12mod config; 15mod config;
@@ -66,7 +69,7 @@ enum DeviceCmd {
66} 69}
67 70
68#[tokio::main] 71#[tokio::main]
69async fn main() -> Result<(), Error> { 72async 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
113fn default_headers(config: &Config) -> Result<HeaderMap, Error> { 116fn 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
129async 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
135fn add_pb(mp: &MultiProgress, template: &str, message: String) -> ProgressBar { 140fn 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());