Switch to ureq

This commit is contained in:
spikecodes 2021-01-11 10:33:48 -08:00
parent bb8273bab4
commit 91746908a1
4 changed files with 103 additions and 196 deletions

223
Cargo.lock generated
View File

@ -31,7 +31,7 @@ dependencies = [
"futures-util",
"http",
"log",
"rustls",
"rustls 0.18.1",
"tokio-rustls",
"trust-dns-proto",
"trust-dns-resolver",
@ -193,10 +193,10 @@ dependencies = [
"actix-service",
"actix-utils",
"futures-util",
"rustls",
"rustls 0.18.1",
"tokio-rustls",
"webpki",
"webpki-roots",
"webpki-roots 0.20.0",
]
[[package]]
@ -249,7 +249,7 @@ dependencies = [
"mime",
"pin-project 1.0.4",
"regex",
"rustls",
"rustls 0.18.1",
"serde",
"serde_json",
"serde_urlencoded",
@ -393,7 +393,7 @@ dependencies = [
"mime",
"percent-encoding",
"rand",
"rustls",
"rustls 0.18.1",
"serde",
"serde_json",
"serde_urlencoded",
@ -529,6 +529,12 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chunked_transfer"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7477065d45a8fe57167bf3cf8bcd3729b54cfcb81cca49bda2d038ea89ae82ca"
[[package]]
name = "const_fn"
version = "0.4.5"
@ -855,74 +861,18 @@ dependencies = [
"itoa",
]
[[package]]
name = "http-body"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b"
dependencies = [
"bytes 0.5.6",
"http",
]
[[package]]
name = "httparse"
version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
[[package]]
name = "httpdate"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47"
[[package]]
name = "humansize"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6cab2627acfc432780848602f3f558f7e9dd427352224b0d9324025796d2a5e"
[[package]]
name = "hyper"
version = "0.13.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6ad767baac13b44d4529fcf58ba2cd0995e36e7b435bc5b039de6f47e880dbf"
dependencies = [
"bytes 0.5.6",
"futures-channel",
"futures-core",
"futures-util",
"h2",
"http",
"http-body",
"httparse",
"httpdate",
"itoa",
"pin-project 1.0.4",
"socket2",
"tokio",
"tower-service",
"tracing",
"want",
]
[[package]]
name = "hyper-rustls"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37743cc83e8ee85eacfce90f2f4102030d9ff0a95244098d781e9bee4a90abb6"
dependencies = [
"bytes 0.5.6",
"futures-util",
"hyper",
"log",
"rustls",
"tokio",
"tokio-rustls",
"webpki",
]
[[package]]
name = "idna"
version = "0.2.0"
@ -971,15 +921,9 @@ dependencies = [
"socket2",
"widestring",
"winapi 0.3.9",
"winreg 0.6.2",
"winreg",
]
[[package]]
name = "ipnet"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47be2f14c678be2fdcab04ab1171db51b2762ce6f0a8ee87c8dd4a04ed216135"
[[package]]
name = "itoa"
version = "0.4.7"
@ -1045,10 +989,10 @@ dependencies = [
"async-recursion",
"base64 0.13.0",
"regex",
"reqwest",
"serde",
"serde_json",
"time",
"ureq",
"url",
]
@ -1109,16 +1053,6 @@ version = "0.3.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "mime_guess"
version = "2.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212"
dependencies = [
"mime",
"unicase",
]
[[package]]
name = "miniz_oxide"
version = "0.4.3"
@ -1433,42 +1367,6 @@ version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581"
[[package]]
name = "reqwest"
version = "0.10.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0718f81a8e14c4dbb3b34cf23dc6aaf9ab8a0dfec160c534b3dbca1aaa21f47c"
dependencies = [
"base64 0.13.0",
"bytes 0.5.6",
"encoding_rs",
"futures-core",
"futures-util",
"http",
"http-body",
"hyper",
"hyper-rustls",
"ipnet",
"js-sys",
"lazy_static",
"log",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite 0.2.3",
"rustls",
"serde",
"serde_urlencoded",
"tokio",
"tokio-rustls",
"url",
"wasm-bindgen",
"wasm-bindgen-futures",
"web-sys",
"webpki-roots",
"winreg 0.7.0",
]
[[package]]
name = "resolv-conf"
version = "0.7.0"
@ -1522,6 +1420,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",
"ring",
"sct",
"webpki",
]
[[package]]
name = "ryu"
version = "1.0.5"
@ -1838,7 +1749,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "099837d3464c16a808060bb3f02263b412f6fafcb5d01c533d309985fbeebe48"
dependencies = [
"bytes 0.5.6",
"fnv",
"futures-core",
"iovec",
"lazy_static",
@ -1859,7 +1769,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12831b255bcfa39dc0436b01e19fea231a37db570686c06ee72c423479f889a"
dependencies = [
"futures-core",
"rustls",
"rustls 0.18.1",
"tokio",
"webpki",
]
@ -1887,12 +1797,6 @@ dependencies = [
"serde",
]
[[package]]
name = "tower-service"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860"
[[package]]
name = "tracing"
version = "0.1.22"
@ -1964,27 +1868,12 @@ dependencies = [
"trust-dns-proto",
]
[[package]]
name = "try-lock"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "typenum"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33"
[[package]]
name = "unicase"
version = "2.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6"
dependencies = [
"version_check",
]
[[package]]
name = "unicode-bidi"
version = "0.3.4"
@ -2021,6 +1910,22 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a"
[[package]]
name = "ureq"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96014ded8c85822677daee4f909d18acccca744810fd4f8ffc492c284f2324bc"
dependencies = [
"base64 0.13.0",
"chunked_transfer",
"log",
"once_cell",
"rustls 0.19.0",
"url",
"webpki",
"webpki-roots 0.21.0",
]
[[package]]
name = "url"
version = "2.2.0"
@ -2039,16 +1944,6 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "want"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0"
dependencies = [
"log",
"try-lock",
]
[[package]]
name = "wasi"
version = "0.9.0+wasi-snapshot-preview1"
@ -2062,8 +1957,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cd364751395ca0f68cafb17666eee36b63077fb5ecd972bbcd74c90c4bf736e"
dependencies = [
"cfg-if 1.0.0",
"serde",
"serde_json",
"wasm-bindgen-macro",
]
@ -2082,18 +1975,6 @@ dependencies = [
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fe9756085a84584ee9457a002b7cdfe0bfff169f45d2591d8be1345a6780e35"
dependencies = [
"cfg-if 1.0.0",
"js-sys",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.69"
@ -2152,6 +2033,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 = "widestring"
version = "0.4.3"
@ -2201,15 +2091,6 @@ dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "winreg"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0120db82e8a1e0b9fb3345a539c478767c0048d842860994d96113d5b667bd69"
dependencies = [
"winapi 0.3.9",
]
[[package]]
name = "ws2_32-sys"
version = "0.2.1"

View File

@ -10,7 +10,7 @@ edition = "2018"
[dependencies]
base64 = "0.13.0"
actix-web = { version = "3.3.2", features = ["rustls"] }
reqwest = { version = "0.10", default_features = false, features = ["rustls-tls"] }
ureq = "2.0.1"
askama = "0.10.5"
serde = { version = "1.0.118", default_features = false, features = ["derive"] }
serde_json = "1.0"

View File

@ -49,7 +49,7 @@ pub async fn profile(req: HttpRequest) -> HttpResponse {
// USER
async fn user(name: &str) -> Result<User, &'static str> {
// Build the Reddit JSON API path
let path: String = format!("user/{}/about.json", name);
let path: String = format!("/user/{}/about.json", name);
// Send a request to the url
match request(&path).await {

View File

@ -277,36 +277,62 @@ pub async fn error(msg: String) -> HttpResponse {
// Make a request to a Reddit API and parse the JSON response
pub async fn request(path: &str) -> Result<serde_json::Value, &'static str> {
let url = format!("https://www.reddit.com/{}", path);
let url = format!("https://www.reddit.com{}", path);
// match reqwest::get(&url).await {
// Ok(res) => {
// // Read the status from the response
// match res.status().is_success() {
// true => {
// // Parse the response from Reddit as JSON
// match from_str(res.text().await.unwrap_or_default().as_str()) {
// Ok(json) => Ok(json),
// Err(_) => {
// #[cfg(debug_assertions)]
// dbg!(format!("{} - Failed to parse page JSON data", url));
// Err("Failed to parse page JSON data")
// }
// }
// }
// // If Reddit returns error, tell user Page Not Found
// false => {
// #[cfg(debug_assertions)]
// dbg!(format!("{} - Page not found", url));
// Err("Page not found")
// }
// }
// }
// // If can't send request to Reddit, return this to user
// Err(_e) => {
// #[cfg(debug_assertions)]
// dbg!(format!("{} - {}", url, _e));
// Err("Couldn't send request to Reddit")
// }
// }
// Send request using reqwest
match reqwest::get(&url).await {
Ok(res) => {
// Read the status from the response
match res.status().is_success() {
true => {
// Parse the response from Reddit as JSON
match from_str(res.text().await.unwrap_or_default().as_str()) {
Ok(json) => Ok(json),
Err(_) => {
#[cfg(debug_assertions)]
dbg!(format!("{} - Failed to parse page JSON data", url));
Err("Failed to parse page JSON data")
}
}
}
// If Reddit returns error, tell user Page Not Found
false => {
match ureq::get(&url).call() {
// If response is success
Ok(response) => {
// Parse the response from Reddit as JSON
match from_str(&response.into_string().unwrap()) {
Ok(json) => Ok(json),
Err(_) => {
#[cfg(debug_assertions)]
dbg!(format!("{} - Page not found", url));
Err("Page not found")
dbg!(format!("{} - Failed to parse page JSON data", url));
Err("Failed to parse page JSON data")
}
}
}
// If can't send request to Reddit, return this to user
Err(_e) => {
// If response is error
Err(ureq::Error::Status(_, _)) => {
#[cfg(debug_assertions)]
dbg!(format!("{} - {}", url, _e));
dbg!(format!("{} - Page not found", url));
Err("Page not found")
}
// If failed to send request
Err(e) => {
#[cfg(debug_assertions)]
dbg!(e);
Err("Couldn't send request to Reddit")
}
}