mcapi 0.2.0

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, Java Runtimes, and SLP)
Documentation
#![cfg_attr(docsrs, feature(doc_cfg))]
//! Unified async Rust client for the Minecraft ecosystem.
//!
//! All HTTP platform clients can share a single `Arc`-backed [`HttpCore`] (and its connection
//! pool, [`Mirror`] routing, and [`ResiliencePolicy`]) either directly via `::from_http(http)`
//! or through the lightweight [`McApi`] entry point.
//!
//! Enable only the modules you need via Cargo features (`mojang`, `modrinth`, `curseforge`,
//! `yggdrasil`, `yggdrasil-authlib`, `yggdrasil-verify`, `blessing`, `loaders`, `java`,
//! `slp`, `slp-srv`, `stream`) or keep the default `full` feature set. Enabling only `slp`
//! compiles with zero HTTP (`reqwest`) dependencies.

pub mod error;
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub mod http;
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub mod mirror;
#[cfg(feature = "profile")]
#[cfg_attr(docsrs, doc(cfg(feature = "profile")))]
pub mod profile;
pub mod util;
pub mod uuid;

#[cfg(feature = "curseforge")]
#[cfg_attr(docsrs, doc(cfg(feature = "curseforge")))]
pub mod curseforge;

#[cfg(feature = "modrinth")]
#[cfg_attr(docsrs, doc(cfg(feature = "modrinth")))]
pub mod modrinth;

#[cfg(feature = "mojang")]
#[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
pub mod mojang;

#[cfg(feature = "yggdrasil")]
#[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
pub mod yggdrasil;

#[cfg(feature = "blessing")]
#[cfg_attr(docsrs, doc(cfg(feature = "blessing")))]
pub mod blessing;

#[cfg(feature = "loaders")]
#[cfg_attr(docsrs, doc(cfg(feature = "loaders")))]
pub mod loaders;

#[cfg(feature = "java")]
#[cfg_attr(docsrs, doc(cfg(feature = "java")))]
pub mod java;

#[cfg(feature = "slp")]
#[cfg_attr(docsrs, doc(cfg(feature = "slp")))]
pub mod slp;

pub use error::{Error, Result};
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub use error::{RateLimitInfo, parse_rate_limit_headers};
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub use http::{HttpConfig, HttpCore, HttpCoreBuilder, ResiliencePolicy};
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub use mirror::{Mirror, RewriteRule};
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
pub use util::deserialize_optional_url;
pub use util::{encode_path_segment, trim_trailing_slash};
pub use uuid::McUuid;

/// Lightweight shared entry point holding a single [`HttpCore`] (one `reqwest::Client`
/// connection pool, [`Mirror`], and [`ResiliencePolicy`]) and vending zero-cost platform clients.
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[derive(Clone, Debug)]
pub struct McApi {
    http: HttpCore,
}

#[cfg(feature = "http")]
impl McApi {
    /// Creates an [`McApi`] instance with default HTTP settings.
    pub fn new() -> Result<Self> {
        Ok(Self {
            http: HttpCore::new()?,
        })
    }

    /// Creates an [`McApi`] wrapping an existing [`HttpCore`].
    pub const fn from_http(http: HttpCore) -> Self {
        Self { http }
    }

    /// Returns an [`McApiBuilder`] to configure shared HTTP, mirror, and retry settings.
    pub fn builder() -> McApiBuilder {
        McApiBuilder {
            http: HttpCore::builder(),
        }
    }

    /// Returns the shared [`HttpCore`].
    pub fn http(&self) -> &HttpCore {
        &self.http
    }

    /// Returns a [`mojang::MojangClient`] sharing this instance's [`HttpCore`].
    #[cfg(feature = "mojang")]
    #[cfg_attr(docsrs, doc(cfg(feature = "mojang")))]
    pub fn mojang(&self) -> mojang::MojangClient {
        mojang::MojangClient::from_http(self.http.clone())
    }

    /// Returns a [`modrinth::ModrinthClient`] sharing this instance's [`HttpCore`].
    #[cfg(feature = "modrinth")]
    #[cfg_attr(docsrs, doc(cfg(feature = "modrinth")))]
    pub fn modrinth(&self) -> modrinth::ModrinthClient {
        modrinth::ModrinthClient::from_http(self.http.clone())
    }

    /// Returns a [`curseforge::CurseForgeClient`] sharing this instance's [`HttpCore`].
    ///
    /// - When `api_key` is `Some(key)` (non-blank), attaches `x-api-key` only for official
    ///   requests (never forwarded to third-party mirrors).
    /// - When `api_key` is `None` (or blank) and the active [`Mirror`] rewrites
    ///   `https://api.curseforge.com`, constructs a keyless mirror client.
    /// - Otherwise returns [`Error::InvalidInput`].
    #[cfg(feature = "curseforge")]
    #[cfg_attr(docsrs, doc(cfg(feature = "curseforge")))]
    pub fn curseforge(&self, api_key: Option<&str>) -> Result<curseforge::CurseForgeClient> {
        curseforge::CurseForgeClient::from_http(self.http.clone(), api_key)
    }

    /// Returns a [`yggdrasil::YggdrasilClient`] targeting `api_root` and sharing this [`HttpCore`].
    #[cfg(feature = "yggdrasil")]
    #[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil")))]
    pub fn yggdrasil(&self, api_root: impl Into<String>) -> yggdrasil::YggdrasilClient {
        yggdrasil::YggdrasilClient::from_http(self.http.clone(), api_root)
    }

    /// Returns an [`yggdrasil::authlib_injector::AuthlibInjectorClient`] sharing this [`HttpCore`].
    #[cfg(feature = "yggdrasil-authlib")]
    #[cfg_attr(docsrs, doc(cfg(feature = "yggdrasil-authlib")))]
    pub fn authlib_injector(&self) -> yggdrasil::authlib_injector::AuthlibInjectorClient {
        yggdrasil::authlib_injector::AuthlibInjectorClient::from_http(self.http.clone())
    }

    /// Returns a [`blessing::BlessingClient`] targeting `site_url` and sharing this [`HttpCore`].
    #[cfg(feature = "blessing")]
    #[cfg_attr(docsrs, doc(cfg(feature = "blessing")))]
    pub fn blessing(&self, site_url: impl Into<String>) -> blessing::BlessingClient {
        blessing::BlessingClient::from_http(self.http.clone(), site_url)
    }

    /// Returns a [`loaders::LoadersClient`] sharing this instance's [`HttpCore`].
    #[cfg(feature = "loaders")]
    #[cfg_attr(docsrs, doc(cfg(feature = "loaders")))]
    pub fn loaders(&self) -> loaders::LoadersClient {
        loaders::LoadersClient::from_http(self.http.clone())
    }

    /// Returns a [`java::JavaClient`] sharing this instance's [`HttpCore`].
    #[cfg(feature = "java")]
    #[cfg_attr(docsrs, doc(cfg(feature = "java")))]
    pub fn java(&self) -> java::JavaClient {
        java::JavaClient::from_http(self.http.clone())
    }

    /// Returns a default [`slp::SlpClient`].
    #[cfg(feature = "slp")]
    #[cfg_attr(docsrs, doc(cfg(feature = "slp")))]
    pub fn slp(&self) -> slp::SlpClient {
        slp::SlpClient::new()
    }
}

/// Builder for [`McApi`].
#[cfg(feature = "http")]
#[cfg_attr(docsrs, doc(cfg(feature = "http")))]
#[derive(Clone, Debug, Default)]
pub struct McApiBuilder {
    http: HttpCoreBuilder,
}

#[cfg(feature = "http")]
impl McApiBuilder {
    /// Sets the shared `User-Agent` header.
    pub fn user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.http = self.http.user_agent(user_agent);
        self
    }

    /// Configures shared mirror URL rewriting (e.g. `Mirror::China`, `Mirror::Bmclapi`, `Mirror::Mcim`).
    pub fn mirror(mut self, mirror: Mirror) -> Self {
        self.http = self.http.mirror(mirror);
        self
    }

    /// Configures shared retry and mirror-fallback policies.
    pub fn resilience(mut self, resilience: ResiliencePolicy) -> Self {
        self.http = self.http.resilience(resilience);
        self
    }

    /// Sets the shared HTTP request timeout.
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.http = self.http.timeout(timeout);
        self
    }

    /// Injects a pre-configured `reqwest::Client`.
    pub fn with_client(mut self, client: reqwest::Client) -> Self {
        self.http = self.http.with_client(client);
        self
    }

    /// Builds [`McApi`].
    pub fn build(self) -> Result<McApi> {
        Ok(McApi {
            http: self.http.build()?,
        })
    }
}