mcapi 0.3.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
//! Dedicated client for BMCLAPI-specific loader and OptiFine index endpoints
//! (<https://bmclapidoc.bangbang93.com/>).
//!
//! Unlike [`crate::loaders::LoadersClient`], which queries official upstream APIs and
//! applies [`crate::Mirror`] rewriting when configured, [`BmclapiClient`] targets
//! BMCLAPI's proprietary index endpoints directly.

/// Data models for BMCLAPI loader and OptiFine index responses.
pub mod models;

pub use models::{ForgeEntry, ForgeFile, LiteLoaderEntry, NeoForgeEntry, OptiFineEntry};

use url::Url;

use crate::error::Result;
use crate::http::HttpClient;
use crate::util::{encode_path_segment, trim_trailing_slash};

/// Default BMCLAPI base URL.
pub const DEFAULT_BASE_URL: &str = "https://bmclapi2.bangbang93.com";

/// Async client for BMCLAPI's proprietary Forge, NeoForge, LiteLoader, and OptiFine endpoints.
#[derive(Clone, Debug)]
pub struct BmclapiClient {
    http: HttpClient,
    base_url: String,
}

impl BmclapiClient {
    /// Creates a new [`BmclapiClient`] with default [`HttpClient`] settings.
    pub fn new() -> Result<Self> {
        Ok(Self::from_http(HttpClient::new()?))
    }

    /// Creates a new [`BmclapiClient`] sharing an existing [`HttpClient`].
    #[must_use]
    pub fn from_http(http: HttpClient) -> Self {
        Self {
            http,
            base_url: DEFAULT_BASE_URL.to_string(),
        }
    }

    /// Overrides the BMCLAPI base URL (for mock servers or custom BMCLAPI-compatible gateways).
    #[must_use]
    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
        self.base_url = trim_trailing_slash(base_url);
        self
    }

    /// Returns the configured BMCLAPI base URL.
    #[must_use]
    pub fn base_url(&self) -> &str {
        &self.base_url
    }

    /// Returns a reference to the underlying shared [`HttpClient`].
    #[must_use]
    pub fn http(&self) -> &HttpClient {
        &self.http
    }

    fn endpoint(&self, path: &str) -> String {
        format!("{}{path}", self.base_url)
    }

    /// Lists Forge builds for `mc_version` via `GET /forge/minecraft/{mcversion}`.
    pub async fn forge_versions(&self, mc_version: &str) -> Result<Vec<ForgeEntry>> {
        let mc = encode_path_segment(mc_version)?;
        let url = self.endpoint(&format!("/forge/minecraft/{mc}"));
        self.http.get_json(&url).await
    }

    /// Lists Minecraft versions supported by BMCLAPI Forge index via `GET /forge/minecraft`.
    pub async fn forge_supported_mc_versions(&self) -> Result<Vec<String>> {
        let url = self.endpoint("/forge/minecraft");
        self.http.get_json(&url).await
    }

    /// Lists NeoForge builds for `mc_version` via `GET /neoforge/list/{mcversion}`.
    pub async fn neoforge_versions(&self, mc_version: &str) -> Result<Vec<NeoForgeEntry>> {
        let mc = encode_path_segment(mc_version)?;
        let url = self.endpoint(&format!("/neoforge/list/{mc}"));
        self.http.get_json(&url).await
    }

    /// Lists LiteLoader builds for `mc_version` via `GET /liteloader/list`.
    pub async fn liteloader_versions(&self, mc_version: &str) -> Result<Vec<LiteLoaderEntry>> {
        let url = self.endpoint("/liteloader/list");
        self.http
            .get_json_query(&url, &[("mcversion", mc_version)])
            .await
    }

    /// Lists OptiFine versions for `mc_version` via `GET /optifine/{mcversion}`.
    pub async fn optifine_versions(&self, mc_version: &str) -> Result<Vec<OptiFineEntry>> {
        let mc = encode_path_segment(mc_version)?;
        let url = self.endpoint(&format!("/optifine/{mc}"));
        self.http.get_json(&url).await
    }

    /// Lists all OptiFine versions indexed by BMCLAPI via `GET /optifine/versionlist`.
    pub async fn optifine_all_versions(&self) -> Result<Vec<OptiFineEntry>> {
        let url = self.endpoint("/optifine/versionlist");
        self.http.get_json(&url).await
    }

    /// Builds the download URL for `entry` against this client's configured `base_url`.
    pub fn optifine_download_url(&self, entry: &OptiFineEntry) -> Result<Url> {
        entry.download_url(&self.base_url)
    }
}