forked from mirror/Riven
1
0
Fork 0

naive swap reqwest::Client with reqwest_middleware::ClientWithMiddleware

feat-middleware
Zynh0722 2024-02-08 23:57:38 -08:00
parent 886ed4032d
commit 595553dcaf
4 changed files with 35 additions and 13 deletions

View File

@ -44,6 +44,7 @@ log = "0.4"
num_enum = "0.5" num_enum = "0.5"
parking_lot = "0.12" parking_lot = "0.12"
reqwest = { version = "0.11", default-features = false, features = [ "gzip", "json" ] } reqwest = { version = "0.11", default-features = false, features = [ "gzip", "json" ] }
reqwest-middleware = "0.2.4"
scan_fmt = { version = "0.2", default-features = false } scan_fmt = { version = "0.2", default-features = false }
serde = { version = "1.0", features = [ "derive" ] } serde = { version = "1.0", features = [ "derive" ] }
serde_json = "1.0" serde_json = "1.0"

View File

@ -1,6 +1,7 @@
use std::fmt; use std::fmt;
use reqwest::{Error, Response, StatusCode}; use reqwest::{Response, StatusCode};
use reqwest_middleware::Error;
/// Result containing RiotApiError on failure. /// Result containing RiotApiError on failure.
pub type Result<T> = std::result::Result<T, RiotApiError>; pub type Result<T> = std::result::Result<T, RiotApiError>;

View File

@ -1,7 +1,8 @@
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::Arc;
use reqwest::{RequestBuilder, StatusCode}; use reqwest::StatusCode;
use reqwest_middleware::RequestBuilder;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
use tracing as log; use tracing as log;
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
@ -102,7 +103,7 @@ impl RegionalRequester {
retries retries
); );
break Err(RiotApiError::new( break Err(RiotApiError::new(
err, reqwest_middleware::Error::Reqwest(err),
retries, retries,
Some(response), Some(response),
Some(status), Some(status),

View File

@ -1,7 +1,8 @@
use std::future::Future; use std::future::Future;
use std::sync::Arc; use std::sync::Arc;
use reqwest::{Client, Method, RequestBuilder}; use reqwest::Method;
use reqwest_middleware::{ClientWithMiddleware, RequestBuilder};
#[cfg(feature = "tracing")] #[cfg(feature = "tracing")]
use tracing as log; use tracing as log;
@ -43,7 +44,7 @@ pub struct RiotApi {
/// Configuration settings. /// Configuration settings.
config: RiotApiConfig, config: RiotApiConfig,
/// Client for making requests. /// Client for making requests.
client: Client, client: ClientWithMiddleware,
/// Per-region requesters. /// Per-region requesters.
regional_requesters: InsertOnlyCHashMap<&'static str, RegionalRequester>, regional_requesters: InsertOnlyCHashMap<&'static str, RegionalRequester>,
@ -61,7 +62,8 @@ impl RiotApi {
config, config,
client: client_builder client: client_builder
.build() .build()
.expect("Failed to create client from builder."), .expect("Failed to create client from builder.")
.into(),
regional_requesters: InsertOnlyCHashMap::new(), regional_requesters: InsertOnlyCHashMap::new(),
} }
} }
@ -103,7 +105,14 @@ impl RiotApi {
let retries = rinfo.retries; let retries = rinfo.retries;
let status = rinfo.response.status(); let status = rinfo.response.status();
let value = rinfo.response.json::<T>().await; let value = rinfo.response.json::<T>().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. /// This method should generally not be used directly. Consider using endpoint wrappers instead.
@ -132,7 +141,14 @@ impl RiotApi {
let retries = rinfo.retries; let retries = rinfo.retries;
let status = rinfo.response.status(); let status = rinfo.response.status();
let value = rinfo.response.json::<Option<T>>().await; let value = rinfo.response.json::<Option<T>>().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. /// This method should generally not be used directly. Consider using endpoint wrappers instead.
@ -157,11 +173,14 @@ impl RiotApi {
.await?; .await?;
let retries = rinfo.retries; let retries = rinfo.retries;
let status = rinfo.response.status(); let status = rinfo.response.status();
rinfo rinfo.response.error_for_status().map(|_| ()).map_err(|e| {
.response RiotApiError::new(
.error_for_status() reqwest_middleware::Error::Reqwest(e),
.map(|_| ()) retries,
.map_err(|e| RiotApiError::new(e, retries, None, Some(status))) None,
Some(status),
)
})
} }
/// This method should generally not be used directly. Consider using endpoint wrappers instead. /// This method should generally not be used directly. Consider using endpoint wrappers instead.