#[cfg(feature = "polars")]
use polars::prelude::{DataFrame, LazyFrame};
use crate::base::{
PreparedRequest, datasets_request, download_request, parse_retry_after, partitions_request,
should_retry,
};
use crate::config::{ClientConfig, backoff_delay};
use crate::error::{TesseraError, error_from_response};
use crate::models::{
DatasetsResponse, DownloadBody, IntoCoins, IntoMonths, PartitionRef, PartitionsResponse,
download_urls,
};
#[cfg(any(feature = "polars", feature = "duckdb"))]
use crate::readers::ResolvedPartition;
use crate::readers::expand_refs;
#[cfg(any(feature = "polars", feature = "duckdb"))]
use crate::resolver::resolve_sync;
pub struct TesseraClient {
config: ClientConfig,
http: reqwest::blocking::Client,
#[cfg(feature = "polars")]
runtime: tokio::runtime::Runtime,
}
impl TesseraClient {
pub fn new(api_key: Option<&str>) -> Result<Self, TesseraError> {
Self::from_config(ClientConfig::new(api_key)?)
}
pub fn from_config(config: ClientConfig) -> Result<Self, TesseraError> {
let http = reqwest::blocking::Client::builder()
.redirect(reqwest::redirect::Policy::none())
.default_headers(config.auth_headers())
.timeout(config.timeout)
.build()
.map_err(|err| TesseraError::Network(err.to_string()))?;
#[cfg(feature = "polars")]
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.map_err(|err| TesseraError::Network(err.to_string()))?;
Ok(Self {
config,
http,
#[cfg(feature = "polars")]
runtime,
})
}
#[must_use]
pub fn config(&self) -> &ClientConfig {
&self.config
}
pub fn close(&mut self) {
}
fn request(
&self,
prepared: &PreparedRequest,
) -> Result<reqwest::blocking::Response, TesseraError> {
for attempt in 0..=self.config.max_retries {
let last = attempt == self.config.max_retries;
let url = format!("{}{}", self.config.base_url, prepared.path);
let sent = self.http.get(url).query(&prepared.params).send();
match sent {
Err(err) if err.is_connect() || err.is_timeout() || err.is_request() => {
if last {
return Err(TesseraError::Network(format!(
"network error contacting Tessera: {err}"
)));
}
std::thread::sleep(backoff_delay(attempt, None));
}
Ok(response) if !last && should_retry(response.status().as_u16()) => {
let retry_after = parse_retry_after(response.headers());
std::thread::sleep(backoff_delay(attempt, retry_after));
}
Ok(response) => {
if response.status().is_success() {
return Ok(response);
}
let status = response.status().as_u16();
let body = response
.bytes()
.map_err(|err| TesseraError::Network(err.to_string()))?;
return Err(error_from_response(status, &body));
}
Err(err) => {
return Err(TesseraError::Network(format!(
"network error contacting Tessera: {err}"
)));
}
}
}
unreachable!("retry loop always returns")
}
fn json<T: serde::de::DeserializeOwned>(
response: reqwest::blocking::Response,
) -> Result<T, TesseraError> {
let body = response
.bytes()
.map_err(|err| TesseraError::Network(err.to_string()))?;
serde_json::from_slice(&body).map_err(|err| TesseraError::Network(err.to_string()))
}
pub fn datasets(&self) -> Result<DatasetsResponse, TesseraError> {
let response = self.request(&datasets_request())?;
Self::json(response)
}
pub fn partitions(
&self,
asset: &str,
coin: Option<&str>,
month: Option<&str>,
) -> Result<PartitionsResponse, TesseraError> {
let response = self.request(&partitions_request(asset, coin, month))?;
Self::json(response)
}
pub fn download_url(
&self,
asset: &str,
coin: &str,
month: &str,
) -> Result<DownloadBody, TesseraError> {
let response = self.request(&download_request(asset, coin, month))?;
Self::json(response)
}
pub fn partition_refs(
&self,
asset: &str,
coin: impl IntoCoins,
month: impl IntoMonths,
) -> Result<Vec<PartitionRef>, TesseraError> {
let _ = self;
expand_refs(asset, coin, month)
}
#[cfg(any(feature = "polars", feature = "duckdb"))]
fn resolve(&self, refs: &[PartitionRef]) -> Result<Vec<ResolvedPartition>, TesseraError> {
resolve_sync(
|partition| {
download_urls(self.download_url(
&partition.asset,
&partition.coin,
&partition.month,
)?)
},
refs,
)
}
#[cfg(feature = "polars")]
pub fn scan(
&self,
asset: &str,
coin: impl IntoCoins,
month: impl IntoMonths,
columns: Option<&[&str]>,
) -> Result<LazyFrame, TesseraError> {
let parts = self.resolve(&expand_refs(asset, coin, month)?)?;
crate::readers::polars::build_lazyframe(&parts, columns)
}
#[cfg(feature = "polars")]
pub fn read(
&self,
asset: &str,
coin: impl IntoCoins,
month: impl IntoMonths,
columns: Option<&[&str]>,
) -> Result<DataFrame, TesseraError> {
let parts = self.resolve(&expand_refs(asset, coin, month)?)?;
let lazy = crate::readers::polars::build_lazyframe(&parts, columns)?;
self.runtime
.block_on(async { crate::readers::polars::collect(lazy) })
}
#[cfg(feature = "duckdb")]
pub fn to_duckdb(
&self,
asset: &str,
coin: impl IntoCoins,
month: impl IntoMonths,
columns: Option<&[&str]>,
) -> Result<duckdb::Connection, TesseraError> {
let parts = self.resolve(&expand_refs(asset, coin, month)?)?;
crate::readers::duckdb::build_relation(&parts, columns)
}
}
impl Drop for TesseraClient {
fn drop(&mut self) {
self.close();
}
}
impl std::fmt::Debug for TesseraClient {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TesseraClient")
.field("base_url", &self.config.base_url)
.field("timeout", &self.config.timeout)
.field("max_retries", &self.config.max_retries)
.finish_non_exhaustive()
}
}