mcapi 0.3.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
use url::Url;

use super::models::{
    FabricAllVersions, FabricGameVersion, FabricInstallerVersion, FabricIntermediaryVersion,
    FabricLoaderEntry, FabricLoaderVersion, FabricYarnVersion, LoaderLaunchProfile,
};
use super::{DEFAULT_FABRIC_META_URL, LoadersClient};
use crate::error::Result;
use crate::util::encode_path_segment;

/// Builds the canonical Fabric server launcher JAR URL
/// (`https://meta.fabricmc.net/v2/versions/loader/{game}/{loader}/{installer}/server/jar`).
///
/// Apply [`crate::Mirror::rewrite_url`] / [`crate::Mirror::candidate_urls`] or pass directly
/// to [`crate::HttpClient::stream`] for mirror routing and fallback.
pub fn fabric_server_jar_url(
    game_version: &str,
    loader_version: &str,
    installer_version: &str,
) -> Result<Url> {
    let gv = encode_path_segment(game_version)?;
    let lv = encode_path_segment(loader_version)?;
    let iv = encode_path_segment(installer_version)?;
    Ok(Url::parse(&format!(
        "{DEFAULT_FABRIC_META_URL}/v2/versions/loader/{gv}/{lv}/{iv}/server/jar"
    ))?)
}

impl LoadersClient {
    /// Fetches all Fabric Meta version lists via `GET /v2/versions`.
    pub async fn fabric_versions(&self) -> Result<FabricAllVersions> {
        let url = self.url(DEFAULT_FABRIC_META_URL, "/v2/versions");
        self.http.get_json(&url).await
    }

    /// Lists Minecraft versions supported by Fabric via `GET /v2/versions/game`.
    pub async fn fabric_game_versions(&self) -> Result<Vec<FabricGameVersion>> {
        let url = self.url(DEFAULT_FABRIC_META_URL, "/v2/versions/game");
        self.http.get_json(&url).await
    }

    /// Lists Fabric Loader versions via `GET /v2/versions/loader`.
    pub async fn fabric_loader_versions(&self) -> Result<Vec<FabricLoaderVersion>> {
        let url = self.url(DEFAULT_FABRIC_META_URL, "/v2/versions/loader");
        self.http.get_json(&url).await
    }

    /// Lists compatible Fabric Loaders for `mc_version` via
    /// `GET /v2/versions/loader/{mc_version}`.
    pub async fn fabric_loaders_for_mc(&self, mc_version: &str) -> Result<Vec<FabricLoaderEntry>> {
        let gv = encode_path_segment(mc_version)?;
        let url = self.url(
            DEFAULT_FABRIC_META_URL,
            &format!("/v2/versions/loader/{gv}"),
        );
        self.http.get_json(&url).await
    }

    /// Fetches a specific Fabric Loader entry via
    /// `GET /v2/versions/loader/{game_version}/{loader_version}`.
    pub async fn fabric_loader_entry(
        &self,
        game_version: &str,
        loader_version: &str,
    ) -> Result<FabricLoaderEntry> {
        self.get_meta_loader_path(
            DEFAULT_FABRIC_META_URL,
            "v2",
            game_version,
            loader_version,
            "",
        )
        .await
    }

    /// Lists Fabric Installer versions via `GET /v2/versions/installer`.
    pub async fn fabric_installer_versions(&self) -> Result<Vec<FabricInstallerVersion>> {
        let url = self.url(DEFAULT_FABRIC_META_URL, "/v2/versions/installer");
        self.http.get_json(&url).await
    }

    /// Lists Fabric Yarn mappings via `GET /v2/versions/yarn` (optionally scoped by `game_version`).
    pub async fn fabric_yarn_versions(
        &self,
        game_version: Option<&str>,
    ) -> Result<Vec<FabricYarnVersion>> {
        self.get_meta_opt_segment(DEFAULT_FABRIC_META_URL, "/v2/versions/yarn", game_version)
            .await
    }

    /// Lists Fabric Intermediary mappings via `GET /v2/versions/intermediary` (optionally scoped).
    pub async fn fabric_intermediary_versions(
        &self,
        game_version: Option<&str>,
    ) -> Result<Vec<FabricIntermediaryVersion>> {
        self.get_meta_opt_segment(
            DEFAULT_FABRIC_META_URL,
            "/v2/versions/intermediary",
            game_version,
        )
        .await
    }

    /// Fetches a Fabric client launch profile via
    /// `GET /v2/versions/loader/{game_version}/{loader_version}/profile/json`.
    pub async fn fabric_client_profile(
        &self,
        game_version: &str,
        loader_version: &str,
    ) -> Result<LoaderLaunchProfile> {
        self.get_meta_loader_path(
            DEFAULT_FABRIC_META_URL,
            "v2",
            game_version,
            loader_version,
            "/profile/json",
        )
        .await
    }

    /// Fetches a Fabric server launch profile via
    /// `GET /v2/versions/loader/{game_version}/{loader_version}/server/json`.
    pub async fn fabric_server_profile(
        &self,
        game_version: &str,
        loader_version: &str,
    ) -> Result<LoaderLaunchProfile> {
        self.get_meta_loader_path(
            DEFAULT_FABRIC_META_URL,
            "v2",
            game_version,
            loader_version,
            "/server/json",
        )
        .await
    }
}