mcapi 0.2.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, Java Runtimes, and SLP)
Documentation
use super::models::{
    CreateVersionData, ModifyVersionRequest, ScheduleVersionRequest, UploadFile, Version,
    VersionListFilter,
};
use super::{Authed, ModrinthClient, encode_id_or_slug};
use crate::error::Result;
use crate::util::encode_path_segment;

impl ModrinthClient {
    /// Lists versions for a project via `GET /v2/project/{id|slug}/version`.
    pub async fn list_project_versions(
        &self,
        id_or_slug: &str,
        filter: Option<&VersionListFilter>,
    ) -> Result<Vec<Version>> {
        let seg = encode_id_or_slug(id_or_slug)?;
        let path = format!("/project/{seg}/version");
        match filter {
            Some(f) => self.get_json_query(&path, f).await,
            None => self.get_json(&path).await,
        }
    }

    /// Fetches a single version via `GET /v2/version/{id}`.
    pub async fn get_version(&self, version_id: &str) -> Result<Version> {
        let seg = encode_id_or_slug(version_id)?;
        self.get_json(&format!("/version/{seg}")).await
    }

    /// Fetches a project's version by version ID or version number via
    /// `GET /v2/project/{id|slug}/version/{id|number}`.
    pub async fn get_project_version(
        &self,
        id_or_slug: &str,
        version_id_or_number: &str,
    ) -> Result<Version> {
        let proj = encode_id_or_slug(id_or_slug)?;
        let ver = encode_path_segment(version_id_or_number)?;
        self.get_json(&format!("/project/{proj}/version/{ver}"))
            .await
    }

    /// Batch-fetches versions via `GET /v2/versions?ids=[...]`.
    pub async fn get_versions(&self, ids: &[&str]) -> Result<Vec<Version>> {
        self.get_by_ids("/versions", ids).await
    }

    /// Batch-fetches arbitrarily many versions by splitting `ids` into chunks of `chunk_size`.
    pub async fn get_versions_chunked(
        &self,
        ids: &[&str],
        chunk_size: usize,
    ) -> Result<Vec<Version>> {
        self.get_by_ids_chunked("/versions", ids, chunk_size).await
    }
}

impl<'a> Authed<'a> {
    /// Creates a new version with attached files via `POST /v2/version`.
    pub async fn create_version(
        &self,
        data: &CreateVersionData,
        files: Vec<(String, UploadFile)>,
    ) -> Result<Version> {
        let data_json = serde_json::to_string(data)?;
        let resp = self.send_multipart("/version", data_json, files).await?;
        Ok(resp.json().await?)
    }

    /// Modifies a version via `PATCH /v2/version/{id}`.
    pub async fn modify_version(&self, version_id: &str, req: &ModifyVersionRequest) -> Result<()> {
        let seg = encode_id_or_slug(version_id)?;
        self.patch_authed_json(&format!("/version/{seg}"), req)
            .await
    }

    /// Deletes a version via `DELETE /v2/version/{id}`.
    pub async fn delete_version(&self, version_id: &str) -> Result<()> {
        let seg = encode_id_or_slug(version_id)?;
        self.delete_authed(&format!("/version/{seg}")).await
    }

    /// Appends additional files to an existing version via `POST /v2/version/{id}/file`.
    pub async fn add_files_to_version(
        &self,
        version_id: &str,
        files: Vec<(String, UploadFile)>,
    ) -> Result<()> {
        let seg = encode_id_or_slug(version_id)?;
        self.send_multipart(&format!("/version/{seg}/file"), "{}".to_string(), files)
            .await?;
        Ok(())
    }

    /// Schedules a version publication via `POST /v2/version/{id}/schedule`.
    pub async fn schedule_version(
        &self,
        version_id: &str,
        req: &ScheduleVersionRequest,
    ) -> Result<()> {
        let seg = encode_id_or_slug(version_id)?;
        self.post_authed_empty(&format!("/version/{seg}/schedule"), req)
            .await
    }
}