use std::path::{Path, PathBuf};
use tokio::io::AsyncWriteExt;
use crate::client::{Client, with_retry};
use crate::error::Error;
use crate::models::DatabaseFormat as Format;
use crate::models::{
Checksums, ChecksumsResponse, Database, DatabaseList, DatabaseMetadata, Download, DownloadList,
};
const LIST: &str = "/api/v2/database/list";
const METADATA: &str = "/api/v2/database/metadata";
const CHECKSUM: &str = "/api/v2/database/checksum";
const DOWNLOADS: &str = "/api/v2/database/downloads";
const DOWNLOAD: &str = "/api/v2/database/download";
#[derive(Debug, Clone, Copy)]
pub struct DatabaseApi<'a> {
client: &'a Client,
}
impl<'a> DatabaseApi<'a> {
pub(crate) fn new(client: &'a Client) -> Self {
Self { client }
}
pub async fn list(&self) -> Result<Vec<Database>, Error> {
let response: DatabaseList = self.get(LIST, &[]).await?;
Ok(response.databases)
}
pub async fn metadata(&self, id: &str) -> Result<DatabaseMetadata, Error> {
self.get(METADATA, &[("id", id)]).await
}
pub async fn checksums(&self, id: &str, format: impl Into<Format>) -> Result<Checksums, Error> {
let format = format.into();
let response: ChecksumsResponse =
self.get(CHECKSUM, &[("id", id), ("format", format.as_str())]).await?;
Ok(*response.checksums)
}
pub async fn downloads(&self, limit: Option<u32>) -> Result<Vec<Download>, Error> {
let limit = limit.map(|n| n.to_string());
let query: Vec<(&str, &str)> = match &limit {
Some(n) => vec![("limit", n.as_str())],
None => vec![],
};
let response: DownloadList = self.get(DOWNLOADS, &query).await?;
Ok(response.downloads)
}
pub async fn download_url(&self, id: &str, format: impl Into<Format>) -> Result<String, Error> {
self.download_url_with(id, format, &DownloadOptions::default()).await
}
pub async fn download_url_with(
&self,
id: &str,
format: impl Into<Format>,
options: &DownloadOptions,
) -> Result<String, Error> {
let format = format.into();
let mut query = vec![("id", id), ("format", format.as_str())];
if let Some(build) = options.build.as_deref().filter(|build| !build.is_empty()) {
query.push(("build", build));
}
let timeout = self.client.timeout();
with_retry(self.client.retries(), || {
self.client.transport().get_redirect(DOWNLOAD, &query, timeout)
})
.await
}
pub async fn download(
&self,
id: &str,
format: impl Into<Format>,
path: impl AsRef<Path>,
) -> Result<u64, Error> {
self.download_with(id, format, path, &DownloadOptions::default()).await
}
pub async fn download_with(
&self,
id: &str,
format: impl Into<Format>,
path: impl AsRef<Path>,
options: &DownloadOptions,
) -> Result<u64, Error> {
let path = path.as_ref();
let partial = partial_path(path);
let response = self.fetch_file(id, format.into(), options).await?;
let outcome = match stream_to_file(response, &partial).await {
Ok(written) => {
tokio::fs::rename(&partial, path).await.map(|()| written).map_err(Error::from)
}
Err(err) => Err(err),
};
if outcome.is_err() {
let _ = tokio::fs::remove_file(&partial).await;
}
outcome
}
pub async fn download_bytes(
&self,
id: &str,
format: impl Into<Format>,
) -> Result<Vec<u8>, Error> {
self.download_bytes_with(id, format, &DownloadOptions::default()).await
}
pub async fn download_bytes_with(
&self,
id: &str,
format: impl Into<Format>,
options: &DownloadOptions,
) -> Result<Vec<u8>, Error> {
let mut response = self.fetch_file(id, format.into(), options).await?;
let declared = response.content_length();
let mut bytes = Vec::new();
if let Some(length) = declared {
bytes.try_reserve_exact(usize::try_from(length).unwrap_or(usize::MAX)).map_err(
|_| {
Error::Io(std::io::Error::new(
std::io::ErrorKind::OutOfMemory,
format!(
"the file declares {length} bytes, more than this process can hold: \
use DatabaseApi::download to write it to disk"
),
))
},
)?;
}
while let Some(chunk) = response.chunk().await? {
bytes.extend_from_slice(&chunk);
}
assert_whole_transfer(declared, bytes.len() as u64)?;
Ok(bytes)
}
async fn fetch_file(
&self,
id: &str,
format: Format,
options: &DownloadOptions,
) -> Result<reqwest::Response, Error> {
let url = self.download_url_with(id, format, options).await?;
with_retry(self.client.retries(), || self.client.transport().get_file(&url)).await
}
async fn get<T: serde::de::DeserializeOwned>(
&self,
path: &str,
query: &[(&str, &str)],
) -> Result<T, Error> {
let timeout = self.client.timeout();
with_retry(self.client.retries(), || self.client.transport().get_json(path, query, timeout))
.await
}
}
#[derive(Debug, Clone, Default)]
pub struct DownloadOptions {
build: Option<String>,
}
impl DownloadOptions {
pub fn new() -> Self {
Self::default()
}
pub fn build(mut self, build: impl Into<String>) -> Self {
self.build = Some(build.into());
self
}
}
async fn stream_to_file(mut response: reqwest::Response, partial: &Path) -> Result<u64, Error> {
let declared = response.content_length();
let mut file = tokio::fs::File::create(partial).await?;
let mut written = 0u64;
while let Some(chunk) = response.chunk().await? {
file.write_all(&chunk).await?;
written += chunk.len() as u64;
}
file.flush().await?;
drop(file);
assert_whole_transfer(declared, written)?;
Ok(written)
}
fn partial_path(path: &Path) -> PathBuf {
let mut partial = path.as_os_str().to_owned();
partial.push(".part");
PathBuf::from(partial)
}
fn assert_whole_transfer(declared: Option<u64>, written: u64) -> Result<(), Error> {
match declared {
Some(want) if want != written => Err(Error::Io(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
format!("the transfer ended after {written} of {want} bytes"),
))),
_ => Ok(()),
}
}