use crate::error::Result;
use crate::error::SmartIdClientError;
use crate::models::common::SchemeName;
use std::env;
use std::str::FromStr;
#[derive(Debug, Clone)]
pub struct SmartIDConfig {
pub root_url: String,
pub api_path: String,
pub scheme_name: SchemeName,
pub relying_party_uuid: String,
pub relying_party_name: String,
pub client_request_timeout: Option<u64>,
pub long_polling_timeout: u64,
}
impl SmartIDConfig {
pub fn load_from_env() -> Result<SmartIDConfig> {
Ok(SmartIDConfig {
root_url: get_env("SMART_ID_ROOT_URL")?,
api_path: get_env("SMART_ID_V3_API_PATH")?,
scheme_name: SchemeName::from_str(&get_env("SMART_ID_SCHEME_NAME")?)
.map_err(|_| SmartIdClientError::ConfigMissingException("SMART_ID_SCHEME_NAME"))?,
relying_party_uuid: get_env("RELYING_PARTY_UUID")?,
relying_party_name: get_env("RELYING_PARTY_NAME")?,
client_request_timeout: get_env_u64("CLIENT_REQ_NETWORK_TIMEOUT_MILLIS").ok(),
long_polling_timeout: get_env_u64("CLIENT_LONG_POLLING_TIMEOUT_MILLIS")
.unwrap_or(120000),
})
}
pub fn api_url(&self) -> String {
format!("{}{}", self.root_url, self.api_path)
}
pub(crate) fn is_demo(&self) -> bool {
self.root_url == "https://sid.demo.sk.ee"
}
}
fn get_env(name: &'static str) -> Result<String> {
env::var(name).map_err(|_| SmartIdClientError::ConfigMissingException(name))
}
fn get_env_u64(name: &'static str) -> Result<u64> {
env::var(name)
.map_err(|_| SmartIdClientError::ConfigMissingException(name))
.and_then(|val| {
val.parse()
.map_err(|_| SmartIdClientError::ConfigMissingException(name))
})
}