#![allow(dead_code)]
use std::io;
use scopedb_client::Client;
use scopedb_client::Table;
use scopedb_client::reqwest;
use scopedb_client::reqwest::header::AUTHORIZATION;
use scopedb_client::reqwest::header::HeaderMap;
use scopedb_client::reqwest::header::HeaderValue;
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 mut headers = HeaderMap::new();
if let Ok(token) = std::env::var("SCOPEDB_TOKEN") {
if !token.is_empty() {
headers.insert(
AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {token}"))?,
);
}
}
let http_client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
Ok(Client::new(endpoint(), http_client)?)
}
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()))
}