#![allow(dead_code)]
use std::env;
use std::sync::Once;
use rust_okx::{Credentials, OkxClient};
fn load_dotenv() {
static INIT: Once = Once::new();
INIT.call_once(|| {
let _ = dotenvy::dotenv();
});
}
pub fn public_client() -> OkxClient {
OkxClient::builder().build()
}
pub fn live_client() -> Option<OkxClient> {
let creds = credentials("OKX_API_KEY", "OKX_API_SECRET", "OKX_PASSPHRASE")?;
Some(OkxClient::builder().credentials(creds).build())
}
pub fn demo_client() -> Option<OkxClient> {
let creds = credentials("OKX_DEMO_API_KEY", "OKX_DEMO_API_SECRET", "OKX_DEMO_PASSPHRASE")?;
Some(
OkxClient::builder()
.credentials(creds)
.demo_trading(true)
.build(),
)
}
fn credentials(key: &str, secret: &str, passphrase: &str) -> Option<Credentials> {
load_dotenv();
let key = non_empty(key)?;
let secret = non_empty(secret)?;
let passphrase = non_empty(passphrase)?;
Some(Credentials::new(key, secret, passphrase))
}
fn non_empty(var: &str) -> Option<String> {
env::var(var).ok().filter(|v| !v.is_empty())
}