From cc8bf154a33538c6754977a8cd7828167775ad51 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Sat, 1 Feb 2025 10:56:55 +0100 Subject: [PATCH] refactor(ureq): update to ureq v3 (#247) * refactor(ureq): update to ureq v3 * build: update Cargo.lock for ureq=3 * fix(ureq): handle json correctly --- Cargo.lock | 49 ++++++++++++++++++----------------- Cargo.toml | 4 ++- examples/custom_client.rs | 6 +++-- src/client_ureq.rs | 54 ++++++++++++++++++++++----------------- src/error.rs | 5 +--- 5 files changed, 64 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d7f8f9..1750697 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -226,15 +226,6 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" -[[package]] -name = "crc32fast" -version = "1.4.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a97769d94ddab943e4510d138150169a2758b5ef3eb191a9ee688de3e23ef7b3" -dependencies = [ - "cfg-if", -] - [[package]] name = "crossbeam-utils" version = "0.8.21" @@ -374,16 +365,6 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" -[[package]] -name = "flate2" -version = "1.0.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c936bfdafb507ebbf50b8074c54fa31c5be9a1e7e5f467dd659697041407d07c" -dependencies = [ - "crc32fast", - "miniz_oxide", -] - [[package]] name = "fnv" version = "1.0.7" @@ -1958,20 +1939,34 @@ checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" [[package]] name = "ureq" -version = "2.12.1" +version = "3.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d" +checksum = "217751151c53226090391713e533d9a5e904ba2570dabaaace29032687589c3e" dependencies = [ "base64", - "flate2", + "cc", "log", - "once_cell", + "percent-encoding", "rustls", + "rustls-pemfile", "rustls-pki-types", - "url", + "ureq-proto", + "utf-8", "webpki-roots", ] +[[package]] +name = "ureq-proto" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c51fe73e1d8c4e06bb2698286f7e7453c6fc90528d6d2e7fc36bb4e87fe09b1" +dependencies = [ + "base64", + "http 1.2.0", + "httparse", + "log", +] + [[package]] name = "url" version = "2.5.4" @@ -1983,6 +1978,12 @@ 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 = "utf16_iter" version = "1.0.5" diff --git a/Cargo.toml b/Cargo.toml index 1f4a368..45ed2e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -114,7 +114,9 @@ features = ["fs"] optional = true [dependencies.ureq] -version = "2" +version = "3.0.0" +default-features = false +features = ["rustls"] optional = true [dev-dependencies] diff --git a/examples/custom_client.rs b/examples/custom_client.rs index 009e91a..0a97e10 100644 --- a/examples/custom_client.rs +++ b/examples/custom_client.rs @@ -24,9 +24,11 @@ fn main() { } fn custom_client() -> Api { - let request_agent = frankenstein::ureq::builder() - .timeout(Duration::from_secs(100)) + let config = frankenstein::ureq::Agent::config_builder() + .http_status_as_error(false) + .timeout_global(Some(Duration::from_secs(100))) .build(); + let request_agent = frankenstein::ureq::Agent::new_with_config(config); let api_url = format!("{BASE_API_URL}{TOKEN}"); Api::builder() diff --git a/src/client_ureq.rs b/src/client_ureq.rs index 3ffb037..37ef6d6 100644 --- a/src/client_ureq.rs +++ b/src/client_ureq.rs @@ -15,10 +15,19 @@ pub struct Api { #[builder(into)] pub api_url: String, - #[builder(default = ureq::builder().timeout(Duration::from_secs(500)).build())] + #[builder(default = default_agent())] pub request_agent: ureq::Agent, } +fn default_agent() -> ureq::Agent { + ureq::Agent::new_with_config( + ureq::config::Config::builder() + .http_status_as_error(false) + .timeout_global(Some(Duration::from_secs(500))) + .build(), + ) +} + impl Api { /// Create a new `Api`. You can use [`Api::new_url`] or [`Api::builder`] for more options. pub fn new(api_key: &str) -> Self { @@ -31,21 +40,18 @@ impl Api { } fn decode_response( - response: Result, + response: ureq::http::response::Response, ) -> Result where Output: serde::de::DeserializeOwned, { - match response { - Ok(response) => { - let message = response.into_string().map_err(Error::DecodeUreqBody)?; - crate::json::decode(&message) - } - Err(ureq::Error::Status(_code, response)) => { - let message = response.into_string().map_err(Error::DecodeUreqBody)?; - Err(Error::Api(crate::json::decode(&message)?)) - } - Err(ureq::Error::Transport(error)) => Err(Error::HttpUreq(error)), + let success = response.status().is_success(); + let body = response.into_body().read_to_string()?; + if success { + crate::json::decode(&body) + } else { + let api_error = crate::json::decode(&body)?; + Err(Error::Api(api_error)) } } } @@ -59,15 +65,17 @@ impl TelegramApi for Api { Output: serde::de::DeserializeOwned, { let url = format!("{}/{method}", self.api_url); - let prepared_request = self - .request_agent - .post(&url) - .set("Content-Type", "application/json"); + let request = self.request_agent.post(&url); let response = match params { - None => prepared_request.call(), + None => request.send_empty()?, Some(data) => { let json = crate::json::encode(&data)?; - prepared_request.send_string(&json) + request + .header( + ureq::http::header::CONTENT_TYPE, + ureq::http::HeaderValue::from_static("application/json; charset=utf-8"), + ) + .send(&json)? } }; Self::decode_response(response) @@ -115,15 +123,15 @@ impl TelegramApi for Api { } let url = format!("{}/{method}", self.api_url); - let form_data = form.prepare().unwrap(); + let mut form_data = form.prepare().unwrap(); let response = self .request_agent .post(&url) - .set( - "Content-Type", - &format!("multipart/form-data; boundary={}", form_data.boundary()), + .header( + ureq::http::header::CONTENT_TYPE, + format!("multipart/form-data; boundary={}", form_data.boundary()), ) - .send(form_data); + .send(ureq::SendBody::from_reader(&mut form_data))?; Self::decode_response(response) } } diff --git a/src/error.rs b/src/error.rs index a59a702..04da3af 100644 --- a/src/error.rs +++ b/src/error.rs @@ -33,10 +33,7 @@ pub enum Error { #[cfg(feature = "client-ureq")] #[error("HTTP error: {0}")] - HttpUreq(#[source] ureq::Transport), - #[cfg(feature = "client-ureq")] - #[error("Decode Body Error: {0}")] - DecodeUreqBody(#[source] std::io::Error), + HttpUreq(#[from] ureq::Error), } impl Error {