upgrade to ureq 2.0

Signed-off-by: Christine Dodrill <me@christine.website>
This commit is contained in:
Cadey Ratio 2021-01-10 11:41:26 -05:00
parent 5ca86af0aa
commit 8afb9d2391
15 changed files with 137 additions and 227 deletions

114
backend/Cargo.lock generated
View File

@ -596,70 +596,6 @@ version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a68a4904193147e0a8dec3314640e6db742afd5f6e634f428a6af230d9b3591" 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]] [[package]]
name = "encoding_rs" name = "encoding_rs"
version = "0.8.26" version = "0.8.26"
@ -1275,7 +1211,7 @@ dependencies = [
"tracing-log", "tracing-log",
"tracing-subscriber", "tracing-subscriber",
"twapi-ureq", "twapi-ureq",
"ureq", "ureq 2.0.0",
"url 2.1.1", "url 2.1.1",
] ]
@ -2220,6 +2156,19 @@ dependencies = [
"webpki", "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]] [[package]]
name = "rusty_ulid" name = "rusty_ulid"
version = "0.10.0" version = "0.10.0"
@ -2876,7 +2825,7 @@ dependencies = [
"serde_json", "serde_json",
"serde_urlencoded", "serde_urlencoded",
"twapi-oauth", "twapi-oauth",
"ureq", "ureq 1.5.1",
] ]
[[package]] [[package]]
@ -2981,16 +2930,34 @@ dependencies = [
"chunked_transfer", "chunked_transfer",
"cookie 0.14.2", "cookie 0.14.2",
"cookie_store", "cookie_store",
"encoding",
"log 0.4.11", "log 0.4.11",
"once_cell", "once_cell",
"qstring", "qstring",
"rustls", "rustls 0.18.1",
"serde", "serde",
"serde_json", "serde_json",
"url 2.1.1", "url 2.1.1",
"webpki", "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]] [[package]]
@ -3145,6 +3112,15 @@ dependencies = [
"webpki", "webpki",
] ]
[[package]]
name = "webpki-roots"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82015b7e0b8bad8185994674a13a93306bea76cf5a16c5a181382fd3a5ec2376"
dependencies = [
"webpki",
]
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.2.8" version = "0.2.8"

View File

@ -36,7 +36,7 @@ tracing = "0.1"
tracing-log = "0.1" tracing-log = "0.1"
tracing-subscriber = "0.2" tracing-subscriber = "0.2"
twapi-ureq = "0.1.5" twapi-ureq = "0.1.5"
ureq = { version = "1", features = ["json", "charset"] } ureq = { version = "2", features = ["json", "charset"] }
url = "2" url = "2"
[dependencies.askama] [dependencies.askama]

View File

@ -97,6 +97,12 @@ pub enum Error {
#[error("readability error: {0}")] #[error("readability error: {0}")]
Readability(#[from] readability_fork::error::Error), 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<T = ()> = std::result::Result<T, Error>; pub type Result<T = ()> = std::result::Result<T, Error>;

View File

@ -1,7 +1,7 @@
use super::Result; use super::Result;
use crate::{ use crate::{
models, paseto, schema, models, paseto, schema,
web::{DiscordWebhook, Error as WebError, Mastodon, Result as WebResult, Twitter}, web::{DiscordWebhook, Mastodon, Result as WebResult, Twitter},
MainDatabase, MainDatabase,
}; };
use diesel::prelude::*; use diesel::prelude::*;
@ -55,16 +55,9 @@ impl Into<models::Blogpost> for Item {
pub fn read_jsonfeed(url: String) -> WebResult<Jsonfeed> { pub fn read_jsonfeed(url: String) -> WebResult<Jsonfeed> {
let resp = ureq::get(&url) let resp = ureq::get(&url)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.call(); .call()?;
if resp.ok() { Ok(resp.into_json()?)
Ok(resp.into_json_deserialize()?)
} else {
Err(match resp.synthetic_error() {
Some(why) => WebError::UReq(why.to_string()),
None => WebError::HttpStatus(resp.status()),
})
}
} }
#[instrument(skip(dw, tw, ma), err)] #[instrument(skip(dw, tw, ma), err)]

View File

@ -1,7 +1,7 @@
use super::{Error, Result}; use super::{Error, Result};
use crate::{ use crate::{
models, paseto, schema, models, paseto, schema,
web::{self, discord_webhook::Client as DiscordWebhook}, web::{self, discord_webhook::Client as DiscordWebhook, Error as WebError},
MainDatabase, MainDatabase,
}; };
use diesel::prelude::*; use diesel::prelude::*;
@ -60,9 +60,8 @@ impl WebMention {
let resp = ureq::get(&self.source) let resp = ureq::get(&self.source)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.set("Mi-Mentioned-Url", &self.target) .set("Mi-Mentioned-Url", &self.target)
.call(); .call()?;
if resp.ok() {
let body = resp let body = resp
.into_string() .into_string()
.map_err(|why| Error::Web(web::Error::FuturesIO(why)))?; .map_err(|why| Error::Web(web::Error::FuturesIO(why)))?;
@ -70,12 +69,6 @@ impl WebMention {
&mut body.as_bytes(), &mut body.as_bytes(),
&url::Url::parse(&self.source)?, &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())),
})
}
} }
} }
@ -231,12 +224,11 @@ pub fn bridgy_expand(conn: MainDatabase, wm: models::WebMention) -> Result {
let resp = ureq::get(&wm.source_url) let resp = ureq::get(&wm.source_url)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.set("Mi-Mentioned-Url", &wm.target_url) .set("Mi-Mentioned-Url", &wm.target_url)
.call(); .call()
.map_err(WebError::UReq)?;
if resp.ok() { let body: String = resp.into_string()?;
let body = resp.into_string().unwrap();
let result = parse(&body).unwrap().unwrap(); let result = parse(&body).unwrap().unwrap();
debug!("{:?}", result);
diesel::update(webmentions.find(wm.id)) diesel::update(webmentions.find(wm.id))
.set(&models::UpdateWebMentionSource { .set(&models::UpdateWebMentionSource {
@ -246,10 +238,4 @@ pub fn bridgy_expand(conn: MainDatabase, wm: models::WebMention) -> Result {
.map_err(Error::Database) .map_err(Error::Database)
.unwrap(); .unwrap();
Ok(()) 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())),
})
}
} }

View File

@ -31,9 +31,13 @@ fn main() -> Result<()> {
let resp = ureq::get(&wm.source_url) let resp = ureq::get(&wm.source_url)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.set("Mi-Mentioned-Url", &wm.target_url) .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 body = resp.into_string().unwrap();
let result = parse(&body).unwrap().unwrap(); let result = parse(&body).unwrap().unwrap();
info!("{:?}", result); info!("{:?}", result);
@ -44,16 +48,6 @@ fn main() -> Result<()> {
}) })
.execute(&conn) .execute(&conn)
.unwrap(); .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()),
}
);
}
}); });
Ok(()) Ok(())

View File

@ -17,17 +17,13 @@ fn main() -> Result<()> {
info!("{} weather importer starting up", mi::APPLICATION_NAME); info!("{} weather importer starting up", mi::APPLICATION_NAME);
let resp = ureq::get(WEATHER_URL).set("User-Agent", WEATHER_URL).call(); let resp = ureq::get(WEATHER_URL)
.set("User-Agent", WEATHER_URL)
if !resp.ok() { .call()
panic!( .map_err(|why| {
"{}", panic!("{}", why.to_string());
match resp.synthetic_error() { why
Some(why) => why.to_string(), })?;
None => resp.status_line().to_string(),
}
);
}
let fin = DecodeReaderBytesBuilder::new() let fin = DecodeReaderBytesBuilder::new()
.encoding(Some(WINDOWS_1252)) .encoding(Some(WINDOWS_1252))

View File

@ -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. please contact me at me@christine.website.
This service is intended to act as a POSSE[1] syndication server for 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 Every effort is being taken to ensure that the data going through this
server is my own. server is my own.

View File

@ -45,17 +45,11 @@ impl Client {
#[instrument(skip(self), err)] #[instrument(skip(self), err)]
pub fn send(&self, body: String) -> Result<()> { 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) .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(()) Ok(())
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
} }
} }

View File

@ -33,18 +33,12 @@ impl Client {
pub fn toot(&self, body: String) -> Result { pub fn toot(&self, body: String) -> Result {
let url = &format!("{}/api/v1/statuses", self.instance_url); let url = &format!("{}/api/v1/statuses", self.instance_url);
let resp = ureq::post(url) ureq::post(url)
.set("Authorization", &format!("bearer {}", self.token)) .set("Authorization", &format!("bearer {}", self.token))
.set("User-Agent", crate::APPLICATION_NAME) .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(()) Ok(())
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
} }
} }

View File

@ -21,7 +21,10 @@ pub enum Error {
Serde(#[from] serde_json::Error), Serde(#[from] serde_json::Error),
#[error("ureq error: {0}")] #[error("ureq error: {0}")]
UReq(String), UReq(#[from] ureq::Error),
#[error("old ureq error: {0}")]
OldUReq(String),
#[error("http unsuccessful: {0}")] #[error("http unsuccessful: {0}")]
HttpStatus(u16), HttpStatus(u16),

View File

@ -99,14 +99,8 @@ pub fn get(tracking_number: String) -> Result<Info> {
.set("Cache-Control", "max-age=0") .set("Cache-Control", "max-age=0")
.send_json(serde_json::to_value(PostBody { .send_json(serde_json::to_value(PostBody {
tracking_numbers: vec![tracking_number], tracking_numbers: vec![tracking_number],
})?); })?)
.map_err(Error::UReq)?;
if resp.ok() { Ok(resp.into_json()?)
Ok(resp.into_json_deserialize()?)
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
} }

View File

@ -75,18 +75,10 @@ impl Client {
)) ))
.set("Authorization", &self.api_token) .set("Authorization", &self.api_token)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.call(); .call()
.map_err(Error::UReq)?;
debug!("headers: {:?}", resp.headers_names()); Ok(resp.into_json()?)
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()),
})
}
} }
#[instrument(err, skip(self))] #[instrument(err, skip(self))]
@ -97,20 +89,14 @@ impl Client {
.ok_or_else(|| Error::SystemmateMappingNotFound(member_name.clone()))? .ok_or_else(|| Error::SystemmateMappingNotFound(member_name.clone()))?
.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("Authorization", &self.api_token)
.set("User-Agent", crate::APPLICATION_NAME) .set("User-Agent", crate::APPLICATION_NAME)
.send_json(serde_json::to_value(SwitchRequest { .send_json(serde_json::to_value(SwitchRequest {
members: vec![member], members: vec![member],
})?); })?)
.map_err(Error::UReq)?;
if resp.ok() {
Ok(()) Ok(())
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
} }
} }

View File

@ -52,21 +52,13 @@ impl Client {
pub webhook: FrontAsk, pub webhook: FrontAsk,
} }
let resp = ureq::post(&self.webhook_url).send_json(serde_json::to_value(Wrapper { let resp = ureq::post(&self.webhook_url)
.send_json(serde_json::to_value(Wrapper {
webhook: FrontAsk::default(), webhook: FrontAsk::default(),
})?); })?)
.map_err(Error::UReq)?;
if resp.ok() { Ok(resp.into_json()?)
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()),
})
}
} }
#[instrument(err, skip(self))] #[instrument(err, skip(self))]
@ -76,20 +68,15 @@ impl Client {
pub webhook: SwitchCommand, pub webhook: SwitchCommand,
} }
let resp = ureq::post(&self.webhook_url).send_json(serde_json::to_value(Wrapper { let resp = ureq::post(&self.webhook_url)
.send_json(serde_json::to_value(Wrapper {
webhook: SwitchCommand { webhook: SwitchCommand {
command: "switch".to_string(), command: "switch".to_string(),
member_name: member_name, member_name: member_name,
}, },
})?); })?)
.map_err(Error::UReq)?;
if resp.ok() { Ok(resp.into_json()?)
Ok(resp.into_json_deserialize()?)
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
} }
} }

View File

@ -46,7 +46,7 @@ impl Client {
Ok(()) Ok(())
} else { } else {
Err(match resp.synthetic_error() { Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()), Some(why) => Error::OldUReq(why.to_string()),
None => Error::HttpStatus(resp.status()), None => Error::HttpStatus(resp.status()),
}) })
} }