use std::time::Duration;
pub(super) async fn wait_for_daemon_ready(
client: &reqwest::Client,
base: &str,
timeout: Duration,
) -> bool {
let url = format!("{base}/health");
let deadline = std::time::Instant::now() + timeout;
loop {
if client
.get(&url)
.send()
.await
.ok()
.map(|r| r.status().is_success())
.unwrap_or(false)
{
return true;
}
if std::time::Instant::now() >= deadline {
return false;
}
tokio::time::sleep(Duration::from_millis(200)).await;
}
}
pub(super) async fn fetch_known_index_ids(
client: &reqwest::Client,
base: &str,
) -> anyhow::Result<std::collections::HashSet<String>> {
let url = format!("{base}/indexes");
let resp = client.get(&url).send().await?;
if !resp.status().is_success() {
anyhow::bail!("daemon returned {} for {url}", resp.status());
}
let body: serde_json::Value = resp.json().await?;
let empty: Vec<serde_json::Value> = Vec::new();
let set = body
.get("indexes")
.and_then(|v| v.as_array())
.unwrap_or(&empty)
.iter()
.filter_map(|v| v.as_str().map(|s| s.to_string()))
.collect();
Ok(set)
}