[wip] impl Client for ureq::Agent
- minor refactorings
This commit is contained in:
parent
592e7bf76e
commit
9b1f7f872c
2 changed files with 48 additions and 28 deletions
38
src/main.rs
38
src/main.rs
|
|
@ -4,7 +4,7 @@ mod file;
|
||||||
mod sharry;
|
mod sharry;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
process::exit,
|
process::{self, exit},
|
||||||
sync::{
|
sync::{
|
||||||
Arc,
|
Arc,
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
|
|
@ -22,22 +22,30 @@ use cli::Cli;
|
||||||
use sharry::ClientError;
|
use sharry::ClientError;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
env_logger::init();
|
||||||
|
|
||||||
println!(
|
println!(
|
||||||
"{} to {}!",
|
"{} to {}!",
|
||||||
style("Welcome").magenta().bold(),
|
style("Welcome").magenta().bold(),
|
||||||
style("ShrUpl").yellow().bold(),
|
style("ShrUpl").yellow().bold(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let stop = Arc::new(AtomicBool::new(false));
|
let check_ctrlc = {
|
||||||
|
let stop = Arc::new(AtomicBool::new(false));
|
||||||
|
|
||||||
let stop_ctrlc = stop.clone();
|
let stop_ctrlc = stop.clone();
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(move || {
|
||||||
stop_ctrlc.store(true, Ordering::SeqCst);
|
stop_ctrlc.store(true, Ordering::SeqCst);
|
||||||
info!("stopping as soon as possible ...");
|
info!("stopping as soon as possible ...");
|
||||||
})
|
})
|
||||||
.expect("Error setting Ctrl-C handler");
|
.expect("Error setting Ctrl-C handler");
|
||||||
|
|
||||||
env_logger::init();
|
move || {
|
||||||
|
if stop.load(Ordering::SeqCst) {
|
||||||
|
process::exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let args = Cli::parse();
|
let args = Cli::parse();
|
||||||
info!("args: {args:?}");
|
info!("args: {args:?}");
|
||||||
|
|
@ -57,7 +65,7 @@ fn main() {
|
||||||
.map_or(None, |b| b.then_some(state))
|
.map_or(None, |b| b.then_some(state))
|
||||||
})
|
})
|
||||||
.unwrap_or_else(|| {
|
.unwrap_or_else(|| {
|
||||||
stop.load(Ordering::SeqCst).then(|| exit(0));
|
check_ctrlc();
|
||||||
|
|
||||||
match AppState::from_args(&args, &agent) {
|
match AppState::from_args(&args, &agent) {
|
||||||
Ok(state) => {
|
Ok(state) => {
|
||||||
|
|
@ -67,8 +75,8 @@ fn main() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if let Some(cause) = match e {
|
if let Some(cause) = match e {
|
||||||
ClientError::ResponseStatus {
|
ClientError::ResponseStatus {
|
||||||
actual: _,
|
actual: 403,
|
||||||
expected: 403,
|
expected: _,
|
||||||
} => Some("Alias ID"),
|
} => Some("Alias ID"),
|
||||||
// ClientError::FileIO(_) => Some("URL"),
|
// ClientError::FileIO(_) => Some("URL"),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
@ -77,8 +85,8 @@ fn main() {
|
||||||
println!(
|
println!(
|
||||||
"{} probably wrong: {} – {:?}",
|
"{} probably wrong: {} – {:?}",
|
||||||
style("Error!").red().bold(),
|
style("Error!").red().bold(),
|
||||||
style(cause).cyan().italic(),
|
style(cause).cyan(),
|
||||||
style(e.to_string()).yellow()
|
style(e.to_string()).yellow().italic()
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
error!("unknown error: {e} – {e:?}");
|
error!("unknown error: {e} – {e:?}");
|
||||||
|
|
@ -110,6 +118,6 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
state.save().unwrap(); // HACK unwrap
|
state.save().unwrap(); // HACK unwrap
|
||||||
stop.load(Ordering::SeqCst).then(|| exit(0));
|
check_ctrlc();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,8 +32,8 @@ pub enum ClientError {
|
||||||
#[error("response parsing failed: {0}")]
|
#[error("response parsing failed: {0}")]
|
||||||
ResponseParsing(String),
|
ResponseParsing(String),
|
||||||
|
|
||||||
#[error("unexpected response status: {actual} (expected {expected})")]
|
#[error("unexpected response status: {actual} (expected {expected:?})")]
|
||||||
ResponseStatus { actual: u16, expected: u16 },
|
ResponseStatus { actual: u16, expected: Option<u16> },
|
||||||
|
|
||||||
#[error("unexpected response content: {0}")]
|
#[error("unexpected response content: {0}")]
|
||||||
ResponseContent(String),
|
ResponseContent(String),
|
||||||
|
|
@ -48,21 +48,33 @@ impl ClientError {
|
||||||
Self::ResponseParsing(msg.to_string())
|
Self::ResponseParsing(msg.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn res_check_status<T>(actual: T, expected: T) -> Result<()>
|
pub fn res_status_check<T>(actual: T, expected: T) -> Result<()>
|
||||||
where
|
where
|
||||||
T: Into<u16> + Eq,
|
T: PartialEq + Into<u16> + Copy,
|
||||||
{
|
{
|
||||||
if actual == expected {
|
if actual == expected {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(Self::ResponseStatus {
|
Err(Self::ResponseStatus {
|
||||||
actual: actual.into(),
|
actual: actual.into(),
|
||||||
expected: expected.into(),
|
expected: Some(expected.into()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<ureq::Error> for ClientError {
|
||||||
|
fn from(value: ureq::Error) -> Self {
|
||||||
|
match value {
|
||||||
|
ureq::Error::StatusCode(status) => ClientError::ResponseStatus {
|
||||||
|
actual: status,
|
||||||
|
expected: None,
|
||||||
|
},
|
||||||
|
error => Self::Request(error.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Client for ureq::Agent {
|
impl Client for ureq::Agent {
|
||||||
fn share_create(
|
fn share_create(
|
||||||
&self,
|
&self,
|
||||||
|
|
@ -76,10 +88,10 @@ impl Client for ureq::Agent {
|
||||||
.post(endpoint)
|
.post(endpoint)
|
||||||
.header("Sharry-Alias", alias_id)
|
.header("Sharry-Alias", alias_id)
|
||||||
.send_json(data)
|
.send_json(data)
|
||||||
.map_err(ClientError::req_err)?;
|
.map_err(ClientError::from)?;
|
||||||
|
|
||||||
trace!("{endpoint:?} response: {res:?}");
|
trace!("{endpoint:?} response: {res:?}");
|
||||||
ClientError::res_check_status(res.status(), ureq::http::StatusCode::OK)?;
|
ClientError::res_status_check(res.status(), ureq::http::StatusCode::OK)?;
|
||||||
|
|
||||||
let res = res
|
let res = res
|
||||||
.body_mut()
|
.body_mut()
|
||||||
|
|
@ -102,10 +114,10 @@ impl Client for ureq::Agent {
|
||||||
.post(endpoint)
|
.post(endpoint)
|
||||||
.header("Sharry-Alias", alias_id)
|
.header("Sharry-Alias", alias_id)
|
||||||
.send_empty()
|
.send_empty()
|
||||||
.map_err(ClientError::req_err)?;
|
.map_err(ClientError::from)?;
|
||||||
|
|
||||||
trace!("{endpoint:?} response: {res:?}");
|
trace!("{endpoint:?} response: {res:?}");
|
||||||
ClientError::res_check_status(res.status(), ureq::http::StatusCode::OK)?;
|
ClientError::res_status_check(res.status(), ureq::http::StatusCode::OK)?;
|
||||||
|
|
||||||
let res = res
|
let res = res
|
||||||
.body_mut()
|
.body_mut()
|
||||||
|
|
@ -132,10 +144,10 @@ impl Client for ureq::Agent {
|
||||||
.header("Sharry-File-Name", file_name)
|
.header("Sharry-File-Name", file_name)
|
||||||
.header("Upload-Length", file_size)
|
.header("Upload-Length", file_size)
|
||||||
.send_empty()
|
.send_empty()
|
||||||
.map_err(ClientError::req_err)?;
|
.map_err(ClientError::from)?;
|
||||||
|
|
||||||
trace!("{endpoint:?} response: {res:?}");
|
trace!("{endpoint:?} response: {res:?}");
|
||||||
ClientError::res_check_status(res.status(), ureq::http::StatusCode::CREATED)?;
|
ClientError::res_status_check(res.status(), ureq::http::StatusCode::CREATED)?;
|
||||||
|
|
||||||
let location = (res.headers().get("Location"))
|
let location = (res.headers().get("Location"))
|
||||||
.ok_or_else(|| ClientError::res_parse_err("Location header not found"))?
|
.ok_or_else(|| ClientError::res_parse_err("Location header not found"))?
|
||||||
|
|
@ -154,10 +166,10 @@ impl Client for ureq::Agent {
|
||||||
.header("Sharry-Alias", alias_id)
|
.header("Sharry-Alias", alias_id)
|
||||||
.header("Upload-Offset", offset)
|
.header("Upload-Offset", offset)
|
||||||
.send(chunk)
|
.send(chunk)
|
||||||
.map_err(ClientError::req_err)?;
|
.map_err(ClientError::from)?;
|
||||||
|
|
||||||
trace!("{patch_uri:?} response: {res:?}");
|
trace!("{patch_uri:?} response: {res:?}");
|
||||||
ClientError::res_check_status(res.status(), ureq::http::StatusCode::NO_CONTENT)?;
|
ClientError::res_status_check(res.status(), ureq::http::StatusCode::NO_CONTENT)?;
|
||||||
|
|
||||||
let res_offset = (res.headers().get("Upload-Offset"))
|
let res_offset = (res.headers().get("Upload-Offset"))
|
||||||
.ok_or_else(|| ClientError::res_parse_err("Upload-Offset header not found"))?
|
.ok_or_else(|| ClientError::res_parse_err("Upload-Offset header not found"))?
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue