use crate::{
error::ApiError,
fabric::{FabricClient, models::InstallerQuery},
};
impl FabricClient {
pub async fn download_installer(
&self,
params: InstallerQuery<'_>,
) -> Result<Vec<u8>, ApiError> {
let url = format!(
"https://maven.fabricmc.net/net/fabricmc/fabric-installer/{version}/fabric-installer-{version}.jar",
version = params.version
);
let resp = self.client.get(&url).send().await?.error_for_status()?;
Ok(resp.bytes().await?.to_vec())
}
pub async fn download_installer_to<F: FnMut(&[u8])>(
&self,
params: InstallerQuery<'_>,
mut on_chunk: F,
) -> Result<(Option<u64>, u64), ApiError> {
let url = format!(
"https://maven.fabricmc.net/net/fabricmc/fabric-installer/{version}/fabric-installer-{version}.jar",
version = params.version
);
let mut resp = self.client.get(&url).send().await?.error_for_status()?;
let total = resp.content_length();
let mut downloaded = 0u64;
while let Some(chunk) = resp.chunk().await? {
on_chunk(&chunk);
downloaded += chunk.len() as u64;
}
Ok((total, downloaded))
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::fabric::models::InstallerQuery;
use reqwest::Client;
#[tokio::test]
async fn test_download_installer() {
let client = FabricClient::new(Client::new());
let jar = client
.download_installer(InstallerQuery { version: "1.1.1" })
.await
.unwrap();
assert!(jar.len() > 100_000);
}
}