From 4f21d6c56efe0f0f285c5a641280a4e28bb1f1fe Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 14 May 2022 21:06:36 +0800 Subject: [PATCH] refactor(services/aws): Load region in blocking (#53) Signed-off-by: Xuanwo --- README.md | 1 + src/services/aws/loader.rs | 17 +++++++---------- src/services/aws/v4.rs | 3 +-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1a9f7f68..80f51d2f 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ async fn main() -> Result<()>{ - Supported services - AWS services (SigV4): `reqsign::services::aws::v4::Signer` - Azure Storage services: `reqsign::services::azure::storage::Signer` + - Google services: `reqsign::services::google::Signer` ## Contributing diff --git a/src/services/aws/loader.rs b/src/services/aws/loader.rs index d8dc8046..c672fa1d 100644 --- a/src/services/aws/loader.rs +++ b/src/services/aws/loader.rs @@ -20,9 +20,8 @@ use crate::dirs::expand_homedir; use crate::time::parse_rfc3339; /// Loader trait will try to load credential and region from different sources. -#[async_trait] pub trait RegionLoad: Send + Sync { - async fn load_region(&self) -> Result>; + fn load_region(&self) -> Result>; } #[derive(Default)] @@ -44,9 +43,9 @@ impl RegionLoadChain { #[async_trait] impl RegionLoad for RegionLoadChain { - async fn load_region(&self) -> Result> { + fn load_region(&self) -> Result> { for l in self.loaders.iter() { - if let Some(r) = l.load_region().await? { + if let Some(r) = l.load_region()? { return Ok(Some(r)); } } @@ -77,6 +76,7 @@ impl CredentialLoadChain { self } } + #[async_trait] impl CredentialLoad for CredentialLoadChain { async fn load_credential(&self) -> Result> { @@ -112,9 +112,8 @@ impl CredentialLoad for EnvLoader { } } -#[async_trait] impl RegionLoad for EnvLoader { - async fn load_region(&self) -> Result> { + fn load_region(&self) -> Result> { if let Ok(region) = env::var(super::constants::AWS_REGION) { Ok(Some(region)) } else { @@ -187,7 +186,7 @@ impl CredentialLoad for ProfileLoader { #[async_trait] impl RegionLoad for ProfileLoader { - async fn load_region(&self) -> Result> { + fn load_region(&self) -> Result> { let cfg_path = env::var(super::constants::AWS_CONFIG_FILE) .unwrap_or_else(|_| "~/.aws/config".to_string()); if let Some(cfg_path) = expand_homedir(&cfg_path) { @@ -477,7 +476,7 @@ mod tests { temp_env::with_vars_unset(vec![AWS_REGION], || { TOKIO.block_on(async { let l = EnvLoader {}; - let x = l.load_region().await.expect("load_region must success"); + let x = l.load_region().expect("load_region must success"); assert!(x.is_none()); }); }); @@ -490,7 +489,6 @@ mod tests { let l = EnvLoader {}; let x = l .load_region() - .await .expect("load_credential must success") .expect("region must be valid"); assert_eq!("test", x); @@ -515,7 +513,6 @@ mod tests { let l = ProfileLoader {}; let x = l .load_region() - .await .expect("load_credential must success") .expect("region must be valid"); assert_eq!("test", x); diff --git a/src/services/aws/v4.rs b/src/services/aws/v4.rs index 6a5fbc4f..1b7c5680 100644 --- a/src/services/aws/v4.rs +++ b/src/services/aws/v4.rs @@ -145,8 +145,7 @@ impl Builder { } self.region_load - .load_region() - .await? + .load_region()? .ok_or_else(|| anyhow!("region is required"))? } };