diff --git a/Cargo.lock b/Cargo.lock index 0d567fd..12364b7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -148,7 +148,7 @@ dependencies = [ [[package]] name = "ethereum-rpc" -version = "0.3.0" +version = "0.3.1" dependencies = [ "reqwest", "reqwest-enum", @@ -749,7 +749,7 @@ dependencies = [ [[package]] name = "reqwest-enum" -version = "0.3.0" +version = "0.3.1" dependencies = [ "futures", "reqwest", diff --git a/Cargo.toml b/Cargo.toml index 6dcbf9a..7e394e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -package.version = "0.3.0" +package.version = "0.3.1" package.edition = "2021" package.documentation = "https://docs.rs/reqwest_enum" package.authors = ["Tao Xu "] diff --git a/reqwest-enum/src/provider.rs b/reqwest-enum/src/provider.rs index ac58fcb..3fdf979 100644 --- a/reqwest-enum/src/provider.rs +++ b/reqwest-enum/src/provider.rs @@ -43,12 +43,12 @@ pub trait JsonRpcProviderType: ProviderType { } pub type EndpointFn = fn(target: &T) -> String; -pub type RequestBuilderFn = - fn(request_builder: &reqwest::RequestBuilder) -> reqwest::RequestBuilder; +pub type RequestBuilderFn = + fn(request_builder: &reqwest::RequestBuilder, target: &T) -> reqwest::RequestBuilder; pub struct Provider { /// endpoint closure to customize the endpoint (url / path) endpoint_fn: Option>, - request_fn: Option, + request_fn: Option>, client: Client, } @@ -166,7 +166,10 @@ impl Provider where T: Target, { - pub fn new(endpoint_fn: Option>, request_fn: Option) -> Self { + pub fn new( + endpoint_fn: Option>, + request_fn: Option>, + ) -> Self { let client = reqwest::Client::new(); Self { client, @@ -206,7 +209,7 @@ where } } if let Some(request_fn) = &self.request_fn { - request = request_fn(&mut request); + request = request_fn(&mut request, target); } request } @@ -233,7 +236,9 @@ mod tests { target::Target, }; use serde::{Deserialize, Serialize}; + use std::collections::hash_map::DefaultHasher; use std::collections::HashMap; + use std::hash::{Hash, Hasher}; use tokio_test::block_on; #[derive(Serialize, Deserialize)] struct Person { @@ -270,7 +275,7 @@ mod tests { } fn query(&self) -> HashMap<&'static str, &'static str> { - HashMap::default() + HashMap::from([("foo", "bar")]) } fn headers(&self) -> HashMap<&'static str, &'static str> { @@ -313,11 +318,15 @@ mod tests { fn test_request_fn() { let provider = Provider::::new( None, - Some(|builder: &reqwest::RequestBuilder| { - builder - .try_clone() - .expect("trying to clone request") - .header("X-test", "test") + Some(|builder: &reqwest::RequestBuilder, target: &HttpBin| { + let mut hasher = DefaultHasher::new(); + target.query_string().hash(&mut hasher); + let hash = hasher.finish(); + + let mut req = builder.try_clone().expect("trying to clone request"); + req = req.header("X-test", "test"); + req = req.header("X-hash", format!("{}", hash)); + req }), ); @@ -326,6 +335,7 @@ mod tests { assert_eq!(request.method().to_string(), "GET"); assert_eq!(headers.get("X-test").unwrap(), "test"); + assert_eq!(headers.get("X-hash").unwrap(), "3270317559611782182"); } #[test]