dcspkg/commands/
list.rs

1use crate::Package;
2use anyhow::{bail, Context, Result};
3use reqwest::{blocking::get, IntoUrl, StatusCode};
4
5/// Returns a vector containing a list of packages that are available
6/// for installation from the dcspkg server.
7pub fn list_all_packages<U: IntoUrl>(url: U) -> Result<Vec<Package>> {
8    //craft URL
9    let url: reqwest::Url = url
10        .into_url()
11        .map_err(anyhow::Error::from)
12        .and_then(|url| url.join(crate::LIST_ENDPOINT).map_err(|e| e.into()))
13        .context("Could not parse URL")?;
14
15    log::info!("Downloading package list from {url}...");
16
17    //fetch the list
18    let response = get(url.as_ref()).context("Request failed")?;
19    log::info!("Got reponse from {url}");
20    if response.status() != StatusCode::OK {
21        bail!(
22            "Response was not okay (got code {})",
23            response.status().as_u16()
24        )
25    }
26    let list: Vec<Package> = response.json().context("Could not parse JSON response")?;
27
28    log::debug!("Package list: {list:?}");
29
30    Ok(list)
31}