Skip to content

Commit

Permalink
refactor(services/aws): Load region in blocking (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Xuanwo <github@xuanwo.io>
  • Loading branch information
Xuanwo authored May 14, 2022
1 parent 5026f51 commit 4f21d6c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 7 additions & 10 deletions src/services/aws/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<String>>;
fn load_region(&self) -> Result<Option<String>>;
}

#[derive(Default)]
Expand All @@ -44,9 +43,9 @@ impl RegionLoadChain {

#[async_trait]
impl RegionLoad for RegionLoadChain {
async fn load_region(&self) -> Result<Option<String>> {
fn load_region(&self) -> Result<Option<String>> {
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));
}
}
Expand Down Expand Up @@ -77,6 +76,7 @@ impl CredentialLoadChain {
self
}
}

#[async_trait]
impl CredentialLoad for CredentialLoadChain {
async fn load_credential(&self) -> Result<Option<Credential>> {
Expand Down Expand Up @@ -112,9 +112,8 @@ impl CredentialLoad for EnvLoader {
}
}

#[async_trait]
impl RegionLoad for EnvLoader {
async fn load_region(&self) -> Result<Option<String>> {
fn load_region(&self) -> Result<Option<String>> {
if let Ok(region) = env::var(super::constants::AWS_REGION) {
Ok(Some(region))
} else {
Expand Down Expand Up @@ -187,7 +186,7 @@ impl CredentialLoad for ProfileLoader {

#[async_trait]
impl RegionLoad for ProfileLoader {
async fn load_region(&self) -> Result<Option<String>> {
fn load_region(&self) -> Result<Option<String>> {
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) {
Expand Down Expand Up @@ -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());
});
});
Expand All @@ -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);
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/services/aws/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ impl Builder {
}

self.region_load
.load_region()
.await?
.load_region()?
.ok_or_else(|| anyhow!("region is required"))?
}
};
Expand Down

0 comments on commit 4f21d6c

Please sign in to comment.