From 8afb9d23910cbdb6ea2ac760c1d3e79b1389fc4d Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 10 Jan 2021 11:41:26 -0500 Subject: [PATCH] upgrade to ureq 2.0 Signed-off-by: Christine Dodrill --- backend/Cargo.lock | 114 ++++++++++++----------------- backend/Cargo.toml | 2 +- backend/src/api/mod.rs | 6 ++ backend/src/api/posse.rs | 13 +--- backend/src/api/webmention.rs | 54 +++++--------- backend/src/bin/bridgy_migrate.rs | 36 ++++----- backend/src/bin/weatherscrape.rs | 18 ++--- backend/src/botinfo.txt | 3 +- backend/src/web/discord_webhook.rs | 14 +--- backend/src/web/mastodon.rs | 14 +--- backend/src/web/mod.rs | 5 +- backend/src/web/orange_connex.rs | 12 +-- backend/src/web/pluralkit.rs | 28 ++----- backend/src/web/switchcounter.rs | 43 ++++------- backend/src/web/twitter.rs | 2 +- 15 files changed, 137 insertions(+), 227 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 2df5970..0f10c2a 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -596,70 +596,6 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" -[[package]] -name = "encoding" -version = "0.2.33" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec" -dependencies = [ - "encoding-index-japanese", - "encoding-index-korean", - "encoding-index-simpchinese", - "encoding-index-singlebyte", - "encoding-index-tradchinese", -] - -[[package]] -name = "encoding-index-japanese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-korean" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-simpchinese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-singlebyte" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding-index-tradchinese" -version = "1.20141219.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18" -dependencies = [ - "encoding_index_tests", -] - -[[package]] -name = "encoding_index_tests" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569" - [[package]] name = "encoding_rs" version = "0.8.26" @@ -1275,7 +1211,7 @@ dependencies = [ "tracing-log", "tracing-subscriber", "twapi-ureq", - "ureq", + "ureq 2.0.0", "url 2.1.1", ] @@ -2220,6 +2156,19 @@ dependencies = [ "webpki", ] +[[package]] +name = "rustls" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "064fd21ff87c6e87ed4506e68beb42459caa4a0e2eb144932e6776768556980b" +dependencies = [ + "base64 0.13.0", + "log 0.4.11", + "ring", + "sct", + "webpki", +] + [[package]] name = "rusty_ulid" version = "0.10.0" @@ -2876,7 +2825,7 @@ dependencies = [ "serde_json", "serde_urlencoded", "twapi-oauth", - "ureq", + "ureq 1.5.1", ] [[package]] @@ -2981,16 +2930,34 @@ dependencies = [ "chunked_transfer", "cookie 0.14.2", "cookie_store", - "encoding", "log 0.4.11", "once_cell", "qstring", - "rustls", + "rustls 0.18.1", "serde", "serde_json", "url 2.1.1", "webpki", - "webpki-roots", + "webpki-roots 0.20.0", +] + +[[package]] +name = "ureq" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e562326b32e3766fee6a4890b62489b4b095e94ac07232cfea631668002c19d7" +dependencies = [ + "base64 0.13.0", + "chunked_transfer", + "encoding_rs", + "log 0.4.11", + "once_cell", + "rustls 0.19.0", + "serde", + "serde_json", + "url 2.1.1", + "webpki", + "webpki-roots 0.21.0", ] [[package]] @@ -3145,6 +3112,15 @@ dependencies = [ "webpki", ] +[[package]] +name = "webpki-roots" +version = "0.21.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82015b7e0b8bad8185994674a13a93306bea76cf5a16c5a181382fd3a5ec2376" +dependencies = [ + "webpki", +] + [[package]] name = "winapi" version = "0.2.8" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 0f2380c..3097965 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -36,7 +36,7 @@ tracing = "0.1" tracing-log = "0.1" tracing-subscriber = "0.2" twapi-ureq = "0.1.5" -ureq = { version = "1", features = ["json", "charset"] } +ureq = { version = "2", features = ["json", "charset"] } url = "2" [dependencies.askama] diff --git a/backend/src/api/mod.rs b/backend/src/api/mod.rs index 7c20572..a0052a1 100644 --- a/backend/src/api/mod.rs +++ b/backend/src/api/mod.rs @@ -97,6 +97,12 @@ pub enum Error { #[error("readability error: {0}")] Readability(#[from] readability_fork::error::Error), + + #[error("ureq error: {0}")] + UReq(#[from] ureq::Error), + + #[error("futures error: {0}")] + Futures(#[from] futures_io::Error), } pub type Result = std::result::Result; diff --git a/backend/src/api/posse.rs b/backend/src/api/posse.rs index 76e804a..0fb84ba 100644 --- a/backend/src/api/posse.rs +++ b/backend/src/api/posse.rs @@ -1,7 +1,7 @@ use super::Result; use crate::{ models, paseto, schema, - web::{DiscordWebhook, Error as WebError, Mastodon, Result as WebResult, Twitter}, + web::{DiscordWebhook, Mastodon, Result as WebResult, Twitter}, MainDatabase, }; use diesel::prelude::*; @@ -55,16 +55,9 @@ impl Into for Item { pub fn read_jsonfeed(url: String) -> WebResult { let resp = ureq::get(&url) .set("User-Agent", crate::APPLICATION_NAME) - .call(); + .call()?; - if resp.ok() { - Ok(resp.into_json_deserialize()?) - } else { - Err(match resp.synthetic_error() { - Some(why) => WebError::UReq(why.to_string()), - None => WebError::HttpStatus(resp.status()), - }) - } + Ok(resp.into_json()?) } #[instrument(skip(dw, tw, ma), err)] diff --git a/backend/src/api/webmention.rs b/backend/src/api/webmention.rs index 93cc0e8..59e8a7d 100644 --- a/backend/src/api/webmention.rs +++ b/backend/src/api/webmention.rs @@ -1,7 +1,7 @@ use super::{Error, Result}; use crate::{ models, paseto, schema, - web::{self, discord_webhook::Client as DiscordWebhook}, + web::{self, discord_webhook::Client as DiscordWebhook, Error as WebError}, MainDatabase, }; use diesel::prelude::*; @@ -60,22 +60,15 @@ impl WebMention { let resp = ureq::get(&self.source) .set("User-Agent", crate::APPLICATION_NAME) .set("Mi-Mentioned-Url", &self.target) - .call(); + .call()?; - if resp.ok() { - let body = resp - .into_string() - .map_err(|why| Error::Web(web::Error::FuturesIO(why)))?; - Ok(extractor::extract( - &mut body.as_bytes(), - &url::Url::parse(&self.source)?, - )?) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::Web(web::Error::UReq(why.to_string())), - None => Error::Web(web::Error::HttpStatus(resp.status())), - }) - } + let body = resp + .into_string() + .map_err(|why| Error::Web(web::Error::FuturesIO(why)))?; + Ok(extractor::extract( + &mut body.as_bytes(), + &url::Url::parse(&self.source)?, + )?) } } @@ -231,25 +224,18 @@ pub fn bridgy_expand(conn: MainDatabase, wm: models::WebMention) -> Result { let resp = ureq::get(&wm.source_url) .set("User-Agent", crate::APPLICATION_NAME) .set("Mi-Mentioned-Url", &wm.target_url) - .call(); + .call() + .map_err(WebError::UReq)?; - if resp.ok() { - let body = resp.into_string().unwrap(); - let result = parse(&body).unwrap().unwrap(); - debug!("{:?}", result); + let body: String = resp.into_string()?; + let result = parse(&body).unwrap().unwrap(); - diesel::update(webmentions.find(wm.id)) - .set(&models::UpdateWebMentionSource { - source_url: result.target, - }) - .execute(&*conn) - .map_err(Error::Database) - .unwrap(); - Ok(()) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::Web(web::Error::UReq(why.to_string())), - None => Error::Web(web::Error::HttpStatus(resp.status())), + diesel::update(webmentions.find(wm.id)) + .set(&models::UpdateWebMentionSource { + source_url: result.target, }) - } + .execute(&*conn) + .map_err(Error::Database) + .unwrap(); + Ok(()) } diff --git a/backend/src/bin/bridgy_migrate.rs b/backend/src/bin/bridgy_migrate.rs index 22fb549..2052997 100644 --- a/backend/src/bin/bridgy_migrate.rs +++ b/backend/src/bin/bridgy_migrate.rs @@ -31,29 +31,23 @@ fn main() -> Result<()> { let resp = ureq::get(&wm.source_url) .set("User-Agent", crate::APPLICATION_NAME) .set("Mi-Mentioned-Url", &wm.target_url) - .call(); + .call() + .map_err(|why| { + error!("can't fetch {}: {}", wm.source_url, why); + why + }) + .unwrap(); - if resp.ok() { - let body = resp.into_string().unwrap(); - let result = parse(&body).unwrap().unwrap(); - info!("{:?}", result); + let body = resp.into_string().unwrap(); + let result = parse(&body).unwrap().unwrap(); + info!("{:?}", result); - diesel::update(webmentions.find(wm.id)) - .set(&models::UpdateWebMentionSource { - source_url: result.target, - }) - .execute(&conn) - .unwrap(); - } else { - error!( - "can't fetch {}: {}", - wm.source_url, - match resp.synthetic_error() { - Some(why) => web::Error::UReq(why.to_string()), - None => web::Error::HttpStatus(resp.status()), - } - ); - } + diesel::update(webmentions.find(wm.id)) + .set(&models::UpdateWebMentionSource { + source_url: result.target, + }) + .execute(&conn) + .unwrap(); }); Ok(()) diff --git a/backend/src/bin/weatherscrape.rs b/backend/src/bin/weatherscrape.rs index 26a1f3d..8e62ddb 100644 --- a/backend/src/bin/weatherscrape.rs +++ b/backend/src/bin/weatherscrape.rs @@ -17,17 +17,13 @@ fn main() -> Result<()> { info!("{} weather importer starting up", mi::APPLICATION_NAME); - let resp = ureq::get(WEATHER_URL).set("User-Agent", WEATHER_URL).call(); - - if !resp.ok() { - panic!( - "{}", - match resp.synthetic_error() { - Some(why) => why.to_string(), - None => resp.status_line().to_string(), - } - ); - } + let resp = ureq::get(WEATHER_URL) + .set("User-Agent", WEATHER_URL) + .call() + .map_err(|why| { + panic!("{}", why.to_string()); + why + })?; let fin = DecodeReaderBytesBuilder::new() .encoding(Some(WINDOWS_1252)) diff --git a/backend/src/botinfo.txt b/backend/src/botinfo.txt index 0753402..193d545 100644 --- a/backend/src/botinfo.txt +++ b/backend/src/botinfo.txt @@ -3,7 +3,8 @@ access logs. If this program is doing something you don't want it to do, please contact me at me@christine.website. This service is intended to act as a POSSE[1] syndication server for -various services to various other services. +various services to various other services. It also performs a lot of +personal integrations between my data and cloud services. Every effort is being taken to ensure that the data going through this server is my own. diff --git a/backend/src/web/discord_webhook.rs b/backend/src/web/discord_webhook.rs index 0e5249a..f1b6a04 100644 --- a/backend/src/web/discord_webhook.rs +++ b/backend/src/web/discord_webhook.rs @@ -45,17 +45,11 @@ impl Client { #[instrument(skip(self), err)] pub fn send(&self, body: String) -> Result<()> { - let resp = ureq::post(&self.webhook_url) + ureq::post(&self.webhook_url) .set("User-Agent", crate::APPLICATION_NAME) - .send_json(serde_json::to_value(Body::new(body))?); + .send_json(serde_json::to_value(Body::new(body))?) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(()) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(()) } } diff --git a/backend/src/web/mastodon.rs b/backend/src/web/mastodon.rs index 3c1fe15..604246c 100644 --- a/backend/src/web/mastodon.rs +++ b/backend/src/web/mastodon.rs @@ -33,18 +33,12 @@ impl Client { pub fn toot(&self, body: String) -> Result { let url = &format!("{}/api/v1/statuses", self.instance_url); - let resp = ureq::post(url) + ureq::post(url) .set("Authorization", &format!("bearer {}", self.token)) .set("User-Agent", crate::APPLICATION_NAME) - .send_form(&[("status", body.as_str())]); + .send_form(&[("status", body.as_str())]) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(()) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(()) } } diff --git a/backend/src/web/mod.rs b/backend/src/web/mod.rs index 6a8bcc8..a300695 100644 --- a/backend/src/web/mod.rs +++ b/backend/src/web/mod.rs @@ -21,7 +21,10 @@ pub enum Error { Serde(#[from] serde_json::Error), #[error("ureq error: {0}")] - UReq(String), + UReq(#[from] ureq::Error), + + #[error("old ureq error: {0}")] + OldUReq(String), #[error("http unsuccessful: {0}")] HttpStatus(u16), diff --git a/backend/src/web/orange_connex.rs b/backend/src/web/orange_connex.rs index 713bf5c..400932d 100644 --- a/backend/src/web/orange_connex.rs +++ b/backend/src/web/orange_connex.rs @@ -99,14 +99,8 @@ pub fn get(tracking_number: String) -> Result { .set("Cache-Control", "max-age=0") .send_json(serde_json::to_value(PostBody { tracking_numbers: vec![tracking_number], - })?); + })?) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(resp.into_json_deserialize()?) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(resp.into_json()?) } diff --git a/backend/src/web/pluralkit.rs b/backend/src/web/pluralkit.rs index 10c8d6b..21ec5f2 100644 --- a/backend/src/web/pluralkit.rs +++ b/backend/src/web/pluralkit.rs @@ -75,18 +75,10 @@ impl Client { )) .set("Authorization", &self.api_token) .set("User-Agent", crate::APPLICATION_NAME) - .call(); + .call() + .map_err(Error::UReq)?; - debug!("headers: {:?}", resp.headers_names()); - - if resp.ok() { - Ok(resp.into_json_deserialize()?) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(resp.into_json()?) } #[instrument(err, skip(self))] @@ -97,20 +89,14 @@ impl Client { .ok_or_else(|| Error::SystemmateMappingNotFound(member_name.clone()))? .clone(); - let resp = ureq::post("https://api.pluralkit.me/v1/s/switches") + ureq::post("https://api.pluralkit.me/v1/s/switches") .set("Authorization", &self.api_token) .set("User-Agent", crate::APPLICATION_NAME) .send_json(serde_json::to_value(SwitchRequest { members: vec![member], - })?); + })?) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(()) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(()) } } diff --git a/backend/src/web/switchcounter.rs b/backend/src/web/switchcounter.rs index df70616..a655ff8 100644 --- a/backend/src/web/switchcounter.rs +++ b/backend/src/web/switchcounter.rs @@ -52,21 +52,13 @@ impl Client { pub webhook: FrontAsk, } - let resp = ureq::post(&self.webhook_url).send_json(serde_json::to_value(Wrapper { - webhook: FrontAsk::default(), - })?); + let resp = ureq::post(&self.webhook_url) + .send_json(serde_json::to_value(Wrapper { + webhook: FrontAsk::default(), + })?) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(resp.into_json_deserialize()?) - } else { - Err(match resp.synthetic_error() { - Some(why) => { - error!("ureq error: {}", why); - Error::UReq(why.to_string()) - } - None => Error::HttpStatus(resp.status()), - }) - } + Ok(resp.into_json()?) } #[instrument(err, skip(self))] @@ -76,20 +68,15 @@ impl Client { pub webhook: SwitchCommand, } - let resp = ureq::post(&self.webhook_url).send_json(serde_json::to_value(Wrapper { - webhook: SwitchCommand { - command: "switch".to_string(), - member_name: member_name, - }, - })?); + let resp = ureq::post(&self.webhook_url) + .send_json(serde_json::to_value(Wrapper { + webhook: SwitchCommand { + command: "switch".to_string(), + member_name: member_name, + }, + })?) + .map_err(Error::UReq)?; - if resp.ok() { - Ok(resp.into_json_deserialize()?) - } else { - Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), - None => Error::HttpStatus(resp.status()), - }) - } + Ok(resp.into_json()?) } } diff --git a/backend/src/web/twitter.rs b/backend/src/web/twitter.rs index 4d09ae6..808a12f 100644 --- a/backend/src/web/twitter.rs +++ b/backend/src/web/twitter.rs @@ -46,7 +46,7 @@ impl Client { Ok(()) } else { Err(match resp.synthetic_error() { - Some(why) => Error::UReq(why.to_string()), + Some(why) => Error::OldUReq(why.to_string()), None => Error::HttpStatus(resp.status()), }) }