use std::collections::HashMap;
#[derive(Clone)]
pub struct CryptoCompareAuth {
pub api_key: Option<String>,
}
impl CryptoCompareAuth {
pub fn from_env() -> Self {
Self {
api_key: std::env::var("CRYPTOCOMPARE_API_KEY").ok(),
}
}
pub fn new(api_key: impl Into<String>) -> Self {
Self {
api_key: Some(api_key.into()),
}
}
pub fn public() -> Self {
Self { api_key: None }
}
pub fn sign_query(&self, params: &mut HashMap<String, String>) {
if let Some(key) = &self.api_key {
params.insert("api_key".to_string(), key.clone());
}
}
pub fn sign_headers(&self, headers: &mut HashMap<String, String>) {
if let Some(key) = &self.api_key {
headers.insert("Authorization".to_string(), format!("Apikey {}", key));
}
}
pub fn has_key(&self) -> bool {
self.api_key.is_some()
}
}
impl Default for CryptoCompareAuth {
fn default() -> Self {
Self::from_env()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sign_query() {
let auth = CryptoCompareAuth::new("test_key_123");
let mut params = HashMap::new();
params.insert("fsym".to_string(), "BTC".to_string());
auth.sign_query(&mut params);
assert_eq!(params.get("api_key"), Some(&"test_key_123".to_string()));
assert_eq!(params.get("fsym"), Some(&"BTC".to_string()));
}
#[test]
fn test_sign_headers() {
let auth = CryptoCompareAuth::new("test_key_123");
let mut headers = HashMap::new();
auth.sign_headers(&mut headers);
assert_eq!(
headers.get("Authorization"),
Some(&"Apikey test_key_123".to_string())
);
}
#[test]
fn test_public_auth() {
let auth = CryptoCompareAuth::public();
assert!(!auth.has_key());
let mut params = HashMap::new();
auth.sign_query(&mut params);
assert!(!params.contains_key("api_key"));
}
}