Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add back S2Endpoints::from_env() #74

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fmt::Display, str::FromStr, time::Duration};
use std::{env::VarError, fmt::Display, str::FromStr, time::Duration};

use backon::{BackoffBuilder, ConstantBuilder, Retryable};
use futures::StreamExt;
Expand Down Expand Up @@ -109,7 +109,6 @@ impl S2Endpoints {
}

pub fn for_cell(
&self,
cloud: S2Cloud,
cell_id: impl Into<String>,
) -> Result<Self, http::uri::InvalidUri> {
Expand All @@ -119,6 +118,54 @@ impl S2Endpoints {
basin: BasinEndpoint::Direct(cell_endpoint),
})
}

pub fn from_env() -> Result<Self, String> {
let cloud: S2Cloud = std::env::var("S2_CLOUD")
.ok()
.as_deref()
.unwrap_or(S2Cloud::AWS)
.parse()
.map_err(|cloud| format!("Invalid S2_CLOUD: {cloud}"))?;

let mut endpoints = Self::for_cloud(cloud);

match std::env::var("S2_ACCOUNT_ENDPOINT") {
Ok(spec) => {
endpoints.account = spec
.as_str()
.try_into()
.map_err(|_| format!("Invalid S2_ACCOUNT_ENDPOINT: {spec}"))?;
}
Err(VarError::NotPresent) => {}
Err(VarError::NotUnicode(_)) => {
return Err("Invalid S2_ACCOUNT_ENDPOINT: not Unicode".to_owned());
}
}

match std::env::var("S2_BASIN_ENDPOINT") {
Ok(spec) => {
endpoints.basin = if let Some(parent_zone) = spec.strip_prefix("{basin}.") {
BasinEndpoint::ParentZone(
parent_zone
.try_into()
.map_err(|e| format!("Invalid S2_BASIN_ENDPOINT ({e}): {spec}"))?,
)
} else {
BasinEndpoint::Direct(
spec.as_str()
.try_into()
.map_err(|e| format!("Invalid S2_BASIN_ENDPOINT ({e}): {spec}"))?,
)
}
}
Err(VarError::NotPresent) => {}
Err(VarError::NotUnicode(_)) => {
return Err("Invalid S2_BASIN_ENDPOINT: not Unicode".to_owned());
}
}

Ok(endpoints)
}
}

/// Client configuration.
Expand Down