use std::collections::HashMap;
#[derive(Clone, Debug)]
pub struct IBAuth {
pub account_id: String,
pub _access_token: Option<String>,
}
impl IBAuth {
pub fn new(account_id: impl Into<String>) -> Self {
Self {
account_id: account_id.into(),
_access_token: None,
}
}
#[allow(dead_code)]
pub fn from_env() -> Self {
let account_id = std::env::var("IB_ACCOUNT_ID").unwrap_or_default();
Self::new(account_id)
}
#[allow(dead_code)]
pub fn sign_headers(&self, headers: &mut HashMap<String, String>) {
if let Some(token) = &self._access_token {
headers.insert("Authorization".to_string(), format!("Bearer {}", token));
}
}
pub fn account_id(&self) -> &str {
&self.account_id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_creation() {
let auth = IBAuth::new("DU12345");
assert_eq!(auth.account_id(), "DU12345");
assert!(auth._access_token.is_none());
}
#[test]
fn test_sign_headers_without_token() {
let auth = IBAuth::new("DU12345");
let mut headers = HashMap::new();
auth.sign_headers(&mut headers);
assert!(headers.is_empty()); }
#[test]
fn test_sign_headers_with_token() {
let mut auth = IBAuth::new("DU12345");
auth._access_token = Some("test_token".to_string());
let mut headers = HashMap::new();
auth.sign_headers(&mut headers);
assert_eq!(headers.get("Authorization"), Some(&"Bearer test_token".to_string()));
}
}