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> {
let format = format.into();
let query = [("id", id), ("format", format.as_str())];
with_retry(self.client.retries(), || self.client.transport().get_redirect(DOWNLOAD, &query))
.await
}
pub async fn download(
&self,
id: &str,
format: impl Into<Format>,
path: impl AsRef<Path>,
) -> Result<u64, Error> {
let path = path.as_ref();
let partial = partial_path(path);
let response = self.fetch_file(id, format.into()).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> {
let mut response = self.fetch_file(id, format.into()).await?;
let declared = response.content_length();
let mut bytes = Vec::with_capacity(declared.unwrap_or(0) as usize);
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) -> Result<reqwest::Response, Error> {
let url = self.download_url(id, format).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> {
with_retry(self.client.retries(), || self.client.transport().get_json(path, query)).await
}
}
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(()),
}
}