diff options
author | stuebinm | 2022-01-25 23:59:01 +0100 |
---|---|---|
committer | stuebinm | 2022-01-25 23:59:01 +0100 |
commit | 2e44478f4e6aa29c42bf4d416c6e37be99c03114 (patch) | |
tree | 937100290a3e10fca9f59e3dfd6bc65712c8f6b4 /src/main.rs | |
parent | 130d8907335c93e48017c13e3202816d552683c9 (diff) |
some code deduplications
Diffstat (limited to '')
-rw-r--r-- | src/main.rs | 157 |
1 files changed, 88 insertions, 69 deletions
diff --git a/src/main.rs b/src/main.rs index 12c67a8..dc8ac75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,20 +3,19 @@ use clap::{Parser, Subcommand}; use colored::*; use traveltext::types::*; -use traveltext::{travelynx::*, iceportal::*}; - +use traveltext::{iceportal::*, travelynx::*}; #[allow(non_upper_case_globals)] const token: &str = "1387-d942ee22-1d34-4dc2-89b6-5e7ef229fb5e"; #[allow(non_upper_case_globals)] const baseurl: &str = "https://travelynx.de"; - - #[derive(Parser)] struct Cli { #[clap(subcommand)] command: Command, + #[clap(long)] + debug: bool, } #[derive(Subcommand)] @@ -29,7 +28,7 @@ enum Command { to: String, // TODO: make this optional and guess which train if not given #[clap(flatten)] - train: TrainRef + train: TrainRef, }, /// If iceportal.de is available, ask it which train we're in and /// check in @@ -37,11 +36,9 @@ enum Command { /// Undo the last checkin (if any). Undo, /// Query iceportal.de (for testing) - ICEPortal + ICEPortal, } - - fn main() -> Result<(), ureq::Error> { let cli = Cli::parse(); @@ -55,83 +52,65 @@ fn main() -> Result<(), ureq::Error> { match cli.command { Command::Status => { - let body: Status = ureq::get(&format!("{}/api/v1/status/{}", baseurl, token)) - .call() - // TODO: this prints the token! - .unwrap_or_else(|err| exit_err(&err.to_string())) - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); - - println!("{}: {}", traveltext, body); + let status: Status = + exiting_get_request(&format!("{}/api/v1/status/{}", baseurl, token), cli.debug); + + println!("{}: {}", traveltext, status); } - Command::Checkin {from, to, train} => { - let request = Action::CheckIn { - train, - from_station: from, - to_station: Some(to), - comment: None, - token: format!("{}", token) - }; - - // println!("{}", serde_json::to_string(&request).unwrap()); - - let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl)) - .send_json(request) - .unwrap_or_else(|err| exit_err(&err.to_string())) - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); + Command::Checkin { from, to, train } => { + let resp: Response = exiting_post_request( + &format!("{}/api/v1/travel", baseurl), + Action::CheckIn { + train, + from_station: from, + to_station: Some(to), + comment: None, + token: format!("{}", token), + }, + cli.debug, + ); - // eprintln!("{:?}", resp); println!("{}: {}", traveltext, resp); - }, + } Command::Undo => { - let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl)) - .send_json(Action::Undo {token: token.to_owned()}) - .unwrap_or_else(|err| exit_err(&err.to_string())) - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); + let resp: Response = exiting_post_request( + &format!("{}/api/v1/travel", baseurl), + Action::Undo { + token: token.to_owned(), + }, + cli.debug, + ); println!("{}: {}", traveltext, resp); - }, + } Command::Autocheckin => { - let iceportal: TripInfo = ureq::get("https://iceportal.de/api1/rs/tripInfo/trip") - .call()? - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); + let iceportal: TripInfo = + exiting_get_request("https://iceportal.de/api1/rs/tripInfo/trip", cli.debug); + let last_stop = iceportal.guess_last_station().unwrap(); let train = iceportal.get_train_ref(); println!( "{}: guessing you got onto {} {} in {}, checking in …", - traveltext, - train._type, - train.no, - last_stop + traveltext, train._type, train.no, last_stop + ); + let resp: Response = exiting_post_request( + &format!("{}/api/v1/travel", baseurl), + Action::CheckIn { + train, + from_station: last_stop, + to_station: None, + comment: None, + token: format!("{}", token), + }, + cli.debug, ); - let request = Action::CheckIn { - train, - from_station: last_stop, - to_station: None, - comment: None, - token: format!("{}", token) - }; - - // println!("{}", serde_json::to_string(&request).unwrap()); - - let resp: Response = ureq::post(&format!("{}/api/v1/travel", baseurl)) - .send_json(request) - .unwrap_or_else(|err| exit_err(&err.to_string())) - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); // eprintln!("{:?}", resp); println!("{}: {}", traveltext, resp); - - }, + } Command::ICEPortal => { - let resp: TripInfo = ureq::get("https://iceportal.de/api1/rs/tripInfo/trip") - .call()? - .into_json() - .unwrap_or_else(|err| exit_err(&err.to_string())); + let resp: TripInfo = + exiting_get_request("https://iceportal.de/api1/rs/tripInfo/trip", cli.debug); println!("{:?}", resp); println!("guessing last stop was: {:?}", resp.guess_last_station()); } @@ -139,6 +118,46 @@ fn main() -> Result<(), ureq::Error> { Ok(()) } +fn exiting_get_request<R: serde::de::DeserializeOwned>(uri: &str, debug: bool) -> R { + let resp: String = ureq::get(uri) + .call() + .unwrap_or_else(|err| exit_err(&err.to_string())) + .into_string() + .unwrap_or_else(|err| exit_err(&err.to_string())); + + match serde_json::from_str::<R>(&resp) { + Ok(obj) => obj, + Err(err) => { + if debug { + eprintln!("DEBUG: {}", resp); + } + exit_err(&err.to_string()) + } + } +} + +fn exiting_post_request<R, P>(uri: &str, payload: P, debug: bool) -> R +where + P: serde::Serialize, + R: serde::de::DeserializeOwned, +{ + let resp: String = ureq::post(uri) + .send_json(payload) + .unwrap_or_else(|err| exit_err(&err.to_string())) + .into_string() + .unwrap_or_else(|err| exit_err(&err.to_string())); + + match serde_json::from_str::<R>(&resp) { + Ok(obj) => obj, + Err(err) => { + if debug { + eprintln!("DEBUG: {}", resp); + } + exit_err(&err.to_string()) + } + } +} + fn exit_err(msg: &str) -> ! { eprintln!("{}", msg); std::process::exit(1) |