use std::collections::HashMap;
use crate::core::{
Credentials, ExchangeResult, ExchangeError,
};
#[derive(Clone)]
pub struct TiingoAuth {
api_token: String,
}
impl TiingoAuth {
pub fn new(credentials: &Credentials) -> ExchangeResult<Self> {
if credentials.api_key.is_empty() {
return Err(ExchangeError::Auth("Tiingo requires API token".to_string()));
}
Ok(Self {
api_token: credentials.api_key.clone(),
})
}
pub fn get_auth_header(&self) -> HashMap<String, String> {
let mut headers = HashMap::new();
headers.insert(
"Authorization".to_string(),
format!("Token {}", self.api_token)
);
headers.insert(
"Content-Type".to_string(),
"application/json".to_string()
);
headers
}
pub fn add_to_params(&self, params: &mut HashMap<String, String>) {
params.insert("token".to_string(), self.api_token.clone());
}
pub fn api_token(&self) -> &str {
&self.api_token
}
pub fn ws_auth_token(&self) -> &str {
&self.api_token
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_auth_header() {
let credentials = Credentials::new("test_api_token", "");
let auth = TiingoAuth::new(&credentials).unwrap();
let headers = auth.get_auth_header();
assert_eq!(
headers.get("Authorization"),
Some(&"Token test_api_token".to_string())
);
assert_eq!(
headers.get("Content-Type"),
Some(&"application/json".to_string())
);
}
#[test]
fn test_auth_params() {
let credentials = Credentials::new("test_api_token", "");
let auth = TiingoAuth::new(&credentials).unwrap();
let mut params = HashMap::new();
auth.add_to_params(&mut params);
assert_eq!(params.get("token"), Some(&"test_api_token".to_string()));
}
#[test]
fn test_ws_auth_token() {
let credentials = Credentials::new("test_api_token", "");
let auth = TiingoAuth::new(&credentials).unwrap();
assert_eq!(auth.ws_auth_token(), "test_api_token");
}
#[test]
fn test_empty_api_key() {
let credentials = Credentials::new("", "");
let result = TiingoAuth::new(&credentials);
assert!(result.is_err());
}
}