lix/nix-rust/src/store/binary_cache_store.rs

45 lines
1.2 KiB
Rust
Raw Normal View History

2019-12-04 14:32:28 +00:00
use super::{PathInfo, Store, StorePath};
use crate::Error;
pub struct BinaryCacheStore {
base_uri: String,
client: reqwest::r#async::Client,
}
impl BinaryCacheStore {
pub fn new(base_uri: String) -> Self {
Self {
base_uri,
client: reqwest::r#async::Client::new(),
}
}
}
impl Store for BinaryCacheStore {
fn query_path_info(
&self,
path: &StorePath,
) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<PathInfo, Error>> + Send>> {
let uri = format!("{}/{}.narinfo", self.base_uri.clone(), path.hash);
let path = path.clone();
let client = self.client.clone();
let store_dir = self.store_dir().to_string();
Box::pin(async move {
2019-12-04 14:32:28 +00:00
let response = client.get(&uri).send().await?;
2019-12-04 14:32:28 +00:00
if response.status() == reqwest::StatusCode::NOT_FOUND
|| response.status() == reqwest::StatusCode::FORBIDDEN
{
return Err(Error::InvalidPath(path));
}
2019-12-04 14:32:28 +00:00
let response = response.error_for_status()?;
2019-12-04 14:32:28 +00:00
let body = response.text().await?;
PathInfo::parse_nar_info(&body, &store_dir)
})
}
}