From bbc9c9037650b169abc6a65af6a21811604cef24 Mon Sep 17 00:00:00 2001 From: nyyu Date: Sat, 31 May 2025 20:17:14 +0200 Subject: [PATCH] feat: upgrade ureq v3 --- Cargo.lock | 169 +++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/kb_data_source.rs | 34 +++++---- src/main.rs | 16 ++-- src/ms_data_source.rs | 7 +- 5 files changed, 198 insertions(+), 30 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6683de7..3340033 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -50,6 +50,12 @@ version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" +[[package]] +name = "bytes" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" + [[package]] name = "cc" version = "1.2.25" @@ -93,6 +99,35 @@ dependencies = [ "windows-link", ] +[[package]] +name = "cookie" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ddef33a339a91ea89fb53151bd0a4689cfce27055c291dfa69945475d22c747" +dependencies = [ + "percent-encoding", + "time", + "version_check", +] + +[[package]] +name = "cookie_store" +version = "0.21.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eac901828f88a5241ee0600950ab981148a18f2f756900ffba1b125ca6a3ef9" +dependencies = [ + "cookie", + "document-features", + "idna", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "time", + "url", +] + [[package]] name = "core-foundation-sys" version = "0.8.7" @@ -133,6 +168,15 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "deranged" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e" +dependencies = [ + "powerfmt", +] + [[package]] name = "displaydoc" version = "0.2.5" @@ -144,6 +188,15 @@ dependencies = [ "syn", ] +[[package]] +name = "document-features" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95249b50c6c185bee49034bcb378a49dc2b5dff0be90ff6616d31d64febab05d" +dependencies = [ + "litrs", +] + [[package]] name = "either" version = "1.15.0" @@ -166,6 +219,12 @@ dependencies = [ "miniz_oxide", ] +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "form_urlencoded" version = "1.2.1" @@ -192,6 +251,23 @@ version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" +[[package]] +name = "http" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + [[package]] name = "iana-time-zone" version = "0.1.63" @@ -363,6 +439,12 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "241eaef5fd12c88705a01fc1066c48c4b36e0dd4377dcdc7ec3942cea7a69956" +[[package]] +name = "litrs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" + [[package]] name = "log" version = "0.4.27" @@ -395,6 +477,12 @@ dependencies = [ "adler2", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-traits" version = "0.2.19" @@ -425,6 +513,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + [[package]] name = "proc-macro2" version = "1.0.95" @@ -521,6 +615,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.12.0" @@ -641,6 +744,37 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "time" +version = "0.3.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c" + +[[package]] +name = "time-macros" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tinystr" version = "0.8.1" @@ -665,22 +799,37 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "ureq" -version = "2.12.1" +version = "3.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" +checksum = "b7a3e9af6113ecd57b8c63d3cd76a385b2e3881365f1f489e54f49801d0c83ea" dependencies = [ "base64", + "cookie_store", "flate2", "log", - "once_cell", + "percent-encoding", "rustls", + "rustls-pemfile", "rustls-pki-types", "serde", "serde_json", - "url", + "ureq-proto", + "utf-8", "webpki-roots 0.26.11", ] +[[package]] +name = "ureq-proto" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fadf18427d33828c311234884b7ba2afb57143e6e7e69fda7ee883b624661e36" +dependencies = [ + "base64", + "http", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.4" @@ -692,12 +841,24 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8_iter" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index b8a2119..d9f683e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ regex = "1.11.1" serde = "1.0" serde_derive = "1.0" serde_json = {version = "1.0", features = ["preserve_order"]} -ureq = {version = "2.9", features = ["json"]} +ureq = {version = "3.0", features = ["json"]} [target.'cfg(windows)'.dependencies] winreg = { version = "0.55" } diff --git a/src/kb_data_source.rs b/src/kb_data_source.rs index d13668c..3a8cd75 100644 --- a/src/kb_data_source.rs +++ b/src/kb_data_source.rs @@ -7,6 +7,7 @@ use log::error; use serde_derive::Deserialize; use serde_json::{json, Value}; use std::time::Duration; +use ureq::Agent; pub struct KBDataSource { token: Option, @@ -118,12 +119,13 @@ struct Summoner { // It will be better to use Result... fn get_auth_token() -> Option { - let client = ureq::AgentBuilder::new() + let client: Agent = Agent::config_builder() .user_agent(USER_AGENT_VALUE) - .timeout(Duration::from_secs(10)) - .build(); + .timeout_global(Some(Duration::from_secs(10))) + .build() + .into(); let mut bundle = match client.get("https://koreanbuilds.net/bundle.js").call() { - Ok(resp) => match resp.into_string() { + Ok(mut resp) => match resp.body_mut().read_to_string() { Ok(val) => val, Err(_) => return None, }, @@ -149,11 +151,11 @@ impl KBDataSource { if let Some(token) = &self.token { return match client .get("https://api.koreanbuilds.net/champions?patchid=-1") - .set("Accept", "application/json") - .set("Authorization", token.as_str()) + .header("Accept", "application/json") + .header("Authorization", token.as_str()) .call() { - Ok(resp) => resp.into_json().unwrap_or_default(), + Ok(mut resp) => resp.body_mut().read_json().unwrap_or_default(), Err(_) => None, }; } @@ -336,11 +338,11 @@ impl DataSource for KBDataSource { "https://api.koreanbuilds.net/builds?chmpname={}&patchid=-2&position=COMPOSITE", champ.name )) - .set("Accept", "application/json") - .set("Authorization", token.as_str()) + .header("Accept", "application/json") + .header("Authorization", token.as_str()) .call() { - Ok(resp) => match resp.into_json() { + Ok(mut resp) => match resp.body_mut().read_json() { Ok(val) => val, Err(x) => { error!("Cant json: {}", x); @@ -380,10 +382,10 @@ mod tests { #[test] fn test_get_champs_with_positions_and_patch() { - let client = ureq::AgentBuilder::new() + let client = ureq::Agent::config_builder() .user_agent(USER_AGENT_VALUE) - .timeout(Duration::from_secs(10)) - .build(); + .timeout_global(Some(Duration::from_secs(10))) + .build().into(); let datasource = KBDataSource::new(); let champion = ChampionLoL { data: IndexMap::new(), @@ -394,10 +396,10 @@ mod tests { #[test] fn test_get_champ_data_with_win_pourcentage() { - let client = ureq::AgentBuilder::new() + let client = ureq::Agent::config_builder() .user_agent(USER_AGENT_VALUE) - .timeout(Duration::from_secs(10)) - .build(); + .timeout_global(Some(Duration::from_secs(10))) + .build().into(); let datasource = KBDataSource::new(); let champ = ChampInfo { id: String::from("Annie"), diff --git a/src/main.rs b/src/main.rs index ca95e61..c0dca42 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,7 @@ use std::path::{Path, PathBuf}; use std::time::Instant; use std::{fs, thread, time}; use time::Duration; +use ureq::Agent; #[cfg(target_os = "windows")] use winreg::RegKey; @@ -44,7 +45,7 @@ pub struct ChampInfo { } const USER_AGENT_VALUE: &str = - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0"; + "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:139.0) Gecko/20100101 Firefox/139.0"; const DEFAULT_LOL_CHAMPS_DIR: &str = ".\\champs"; #[cfg(target_os = "windows")] const REG_KEY_LOL_RADS: &str = r"SOFTWARE\WOW6432Node\Riot Games\RADS"; @@ -75,15 +76,17 @@ fn main() -> Result<(), Box> { }; info!("LoL Champs Folder: {}", lol_champs_dir.to_str().unwrap()); - let client: ureq::Agent = ureq::AgentBuilder::new() + let client: Agent = Agent::config_builder() .user_agent(USER_AGENT_VALUE) - .timeout(Duration::from_secs(10)) - .build(); + .timeout_global(Some(Duration::from_secs(10))) + .build() + .into(); let realm: Realm = client .get("https://ddragon.leagueoflegends.com/realms/euw.json") .call()? - .into_json()?; + .body_mut() + .read_json()?; info!("LoL version: {}", realm.v); let champion: Champion = client .get(&format!( @@ -91,7 +94,8 @@ fn main() -> Result<(), Box> { realm.v )) .call()? - .into_json()?; + .body_mut() + .read_json()?; info!("LoL numbers of champs: {}", champion.data.len()); let data_sources: Vec> = diff --git a/src/ms_data_source.rs b/src/ms_data_source.rs index 9ef0e7e..3e0e44a 100644 --- a/src/ms_data_source.rs +++ b/src/ms_data_source.rs @@ -55,7 +55,8 @@ impl DataSource for MSDataSource { .get("https://www.metasrc.com/lol/search/lol") .call() .unwrap() - .into_json() + .body_mut() + .read_json() .unwrap(); for champ in champions { @@ -91,8 +92,8 @@ impl DataSource for MSDataSource { .as_str(), ) .call(); - if let Ok(p) = rep { - let page = p.into_string().unwrap(); + if let Ok(mut p) = rep { + let page = p.body_mut().read_to_string().unwrap(); let mut pos = page.find("Patch ").unwrap(); let patch = find_next_number(&page[pos..]).to_string();