#![allow(dead_code)]
use std::io;
use scopedb_client::Client;
use scopedb_client::Table;
pub fn endpoint() -> String {
std::env::var("SCOPEDB_ENDPOINT").unwrap_or_else(|_| "http://127.0.0.1:6543".to_string())
}
pub fn database() -> String {
std::env::var("SCOPEDB_DATABASE").unwrap_or_else(|_| "scopedb".to_string())
}
pub fn schema() -> String {
std::env::var("SCOPEDB_SCHEMA").unwrap_or_else(|_| "public".to_string())
}
pub fn client() -> Result<Client, Box<dyn std::error::Error>> {
let api_key = std::env::var("SCOPEDB_API_KEY")
.ok()
.filter(|value| !value.is_empty())
.or_else(|| {
std::env::var("SCOPEDB_TOKEN")
.ok()
.filter(|value| !value.is_empty())
});
let mut builder = Client::builder(endpoint());
if let Some(api_key) = api_key {
builder = builder.api_key(api_key);
}
Ok(builder.build()?)
}
pub fn write_table(client: &Client) -> Result<Table, io::Error> {
let table = std::env::var("SCOPEDB_TABLE")
.ok()
.filter(|value| !value.is_empty())
.ok_or_else(|| {
io::Error::new(
io::ErrorKind::InvalidInput,
"set SCOPEDB_TABLE to a disposable table before running this write example",
)
})?;
Ok(client
.table(table)
.with_database(database())
.with_schema(schema()))
}