use std::time::Duration;
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Environment {
Demo,
Live,
Custom(Url),
}
impl Environment {
pub fn base_url(&self) -> Url {
match self {
Environment::Demo => {
Url::parse("https://demo-api.ig.com/gateway/deal/").expect("static URL is valid")
}
Environment::Live => {
Url::parse("https://api.ig.com/gateway/deal/").expect("static URL is valid")
}
Environment::Custom(u) => {
let mut u = u.clone();
if !u.path().ends_with('/') {
let new_path = format!("{}/", u.path());
u.set_path(&new_path);
}
u
}
}
}
}
#[derive(Debug, Clone)]
pub struct IgConfig {
pub environment: Environment,
pub api_key: String,
pub user_agent: String,
pub request_timeout: Duration,
pub token_refresh_skew: Duration,
}
impl IgConfig {
pub fn new(environment: Environment, api_key: impl Into<String>) -> Self {
Self {
environment,
api_key: api_key.into(),
user_agent: format!("trading-ig-rust/{}", env!("CARGO_PKG_VERSION")),
request_timeout: Duration::from_secs(30),
token_refresh_skew: Duration::from_secs(10),
}
}
}