use anyhow::{Context, Result};
pub fn read_bridge_credentials(path: Option<&str>) -> Result<(String, String)> {
let cred_path = path
.map(PathBuf::from)
.unwrap_or_else(super::default_local_credential_path);
let data = std::fs::read_to_string(&cred_path)
.with_context(|| format!("read {}", cred_path.display()))?;
let creds: serde_json::Value =
serde_json::from_str(&data).with_context(|| format!("parse {}", cred_path.display()))?;
let token = creds["token"]
.as_str()
.filter(|s| !s.is_empty())
.context("token field missing or empty in credentials")?;
let base_url = creds["base_url"]
.as_str()
.filter(|s| !s.is_empty())
.context("base_url field missing or empty in credentials")?;
Ok((base_url.to_string(), token.to_string()))
}
use std::path::PathBuf;
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires actual credentials file"]
fn test_read_credentials() {
let (url, token) = read_bridge_credentials(None).unwrap();
assert!(!url.is_empty());
assert!(!token.is_empty());
}
}