use crate::USER_AGENT as KD_USER_AGENT;
use anyhow::{Context, Result};
use reqwest::{Client, header::USER_AGENT};
use semver::Version;
use serde::Deserialize;
const BASE: &str = "https://crates.io/api/v1/crates/knowledge";
pub struct CratesApi {
client: Client,
}
impl CratesApi {
pub fn new() -> Self {
Self {
client: Client::new(),
}
}
pub async fn fetch_info(&mut self) -> Result<CratesInfo> {
let url = format!("{BASE}");
let info = self
.client
.get(&url)
.header(USER_AGENT, KD_USER_AGENT)
.send()
.await?
.json()
.await?;
Ok(info)
}
pub async fn latest_version(&mut self) -> Result<Version> {
self.fetch_info()
.await?
.versions
.into_iter()
.next()
.map(|remote| remote.num)
.context("No versions available")
}
}
#[derive(Debug, Deserialize)]
pub struct CratesInfo {
pub versions: Vec<CratesVersion>,
}
#[derive(Debug, Deserialize)]
pub struct CratesVersion {
pub num: Version,
}