From 595553dcaf86e0ce3e4a8899b0d79caeff7f0dd4 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Thu, 8 Feb 2024 23:57:38 -0800 Subject: [PATCH] naive swap reqwest::Client with reqwest_middleware::ClientWithMiddleware --- riven/Cargo.toml | 1 + riven/src/error.rs | 3 ++- riven/src/req/regional_requester.rs | 5 ++-- riven/src/riot_api.rs | 39 +++++++++++++++++++++-------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/riven/Cargo.toml b/riven/Cargo.toml index 24ef3f2..7d7084c 100644 --- a/riven/Cargo.toml +++ b/riven/Cargo.toml @@ -44,6 +44,7 @@ log = "0.4" num_enum = "0.5" parking_lot = "0.12" reqwest = { version = "0.11", default-features = false, features = [ "gzip", "json" ] } +reqwest-middleware = "0.2.4" scan_fmt = { version = "0.2", default-features = false } serde = { version = "1.0", features = [ "derive" ] } serde_json = "1.0" diff --git a/riven/src/error.rs b/riven/src/error.rs index 1253fa4..f56ce5d 100644 --- a/riven/src/error.rs +++ b/riven/src/error.rs @@ -1,6 +1,7 @@ use std::fmt; -use reqwest::{Error, Response, StatusCode}; +use reqwest::{Response, StatusCode}; +use reqwest_middleware::Error; /// Result containing RiotApiError on failure. pub type Result = std::result::Result; diff --git a/riven/src/req/regional_requester.rs b/riven/src/req/regional_requester.rs index 7dab098..71f9850 100644 --- a/riven/src/req/regional_requester.rs +++ b/riven/src/req/regional_requester.rs @@ -1,7 +1,8 @@ use std::future::Future; use std::sync::Arc; -use reqwest::{RequestBuilder, StatusCode}; +use reqwest::StatusCode; +use reqwest_middleware::RequestBuilder; #[cfg(feature = "tracing")] use tracing as log; #[cfg(feature = "tracing")] @@ -102,7 +103,7 @@ impl RegionalRequester { retries ); break Err(RiotApiError::new( - err, + reqwest_middleware::Error::Reqwest(err), retries, Some(response), Some(status), diff --git a/riven/src/riot_api.rs b/riven/src/riot_api.rs index f09a600..c705f57 100644 --- a/riven/src/riot_api.rs +++ b/riven/src/riot_api.rs @@ -1,7 +1,8 @@ use std::future::Future; use std::sync::Arc; -use reqwest::{Client, Method, RequestBuilder}; +use reqwest::Method; +use reqwest_middleware::{ClientWithMiddleware, RequestBuilder}; #[cfg(feature = "tracing")] use tracing as log; @@ -43,7 +44,7 @@ pub struct RiotApi { /// Configuration settings. config: RiotApiConfig, /// Client for making requests. - client: Client, + client: ClientWithMiddleware, /// Per-region requesters. regional_requesters: InsertOnlyCHashMap<&'static str, RegionalRequester>, @@ -61,7 +62,8 @@ impl RiotApi { config, client: client_builder .build() - .expect("Failed to create client from builder."), + .expect("Failed to create client from builder.") + .into(), regional_requesters: InsertOnlyCHashMap::new(), } } @@ -103,7 +105,14 @@ impl RiotApi { let retries = rinfo.retries; let status = rinfo.response.status(); let value = rinfo.response.json::().await; - value.map_err(|e| RiotApiError::new(e, retries, None, Some(status))) + value.map_err(|e| { + RiotApiError::new( + reqwest_middleware::Error::Reqwest(e), + retries, + None, + Some(status), + ) + }) } /// This method should generally not be used directly. Consider using endpoint wrappers instead. @@ -132,7 +141,14 @@ impl RiotApi { let retries = rinfo.retries; let status = rinfo.response.status(); let value = rinfo.response.json::>().await; - value.map_err(|e| RiotApiError::new(e, retries, None, Some(status))) + value.map_err(|e| { + RiotApiError::new( + reqwest_middleware::Error::Reqwest(e), + retries, + None, + Some(status), + ) + }) } /// This method should generally not be used directly. Consider using endpoint wrappers instead. @@ -157,11 +173,14 @@ impl RiotApi { .await?; let retries = rinfo.retries; let status = rinfo.response.status(); - rinfo - .response - .error_for_status() - .map(|_| ()) - .map_err(|e| RiotApiError::new(e, retries, None, Some(status))) + rinfo.response.error_for_status().map(|_| ()).map_err(|e| { + RiotApiError::new( + reqwest_middleware::Error::Reqwest(e), + retries, + None, + Some(status), + ) + }) } /// This method should generally not be used directly. Consider using endpoint wrappers instead.