mcapi 0.3.1

Unified async Rust client for the Minecraft ecosystem (Mojang, Modrinth, CurseForge, Yggdrasil, Blessing Skin, Loaders, BMCLAPI, and Java Runtimes)
Documentation
use super::BlessingClient;
use super::models::CslProfile;
use crate::error::Result;
use crate::util::encode_path_segment;

impl BlessingClient {
    /// Fetches a CustomSkinLoader profile via `GET /{player}.json`.
    pub async fn csl_profile(&self, player_name: &str) -> Result<Option<CslProfile>> {
        let encoded = encode_path_segment(player_name)?;
        let url = format!("{}/{encoded}.json", self.site_url);
        self.http
            .send_optional_json(&url, |client, target| client.get(target))
            .await
    }

    /// Fetches a CustomSkinLoader profile via `GET /csl/{player}.json`.
    pub async fn csl_profile_prefixed(&self, player_name: &str) -> Result<Option<CslProfile>> {
        let encoded = encode_path_segment(player_name)?;
        let url = format!("{}/csl/{encoded}.json", self.site_url);
        self.http
            .send_optional_json(&url, |client, target| client.get(target))
            .await
    }

    /// Downloads raw texture PNG bytes via `GET /textures/{hash}`.
    pub async fn texture_bytes(&self, hash: &str) -> Result<Vec<u8>> {
        let encoded = encode_path_segment(hash)?;
        let url = format!("{}/textures/{encoded}", self.site_url);
        let resp = self
            .http
            .send_with(&url, |client, target| client.get(target))
            .await?;
        Ok(Vec::from(resp.bytes().await?))
    }

    /// Downloads a rendered avatar PNG via `GET /avatar/{tid}` (with optional pixel `size`).
    pub async fn avatar_bytes(&self, tid: u64, size: Option<u32>) -> Result<Vec<u8>> {
        let url = format!("{}/avatar/{tid}", self.site_url);
        let resp = self
            .http
            .send_with(&url, |client, target| {
                let req = client.get(target);
                match size {
                    Some(s) => req.query(&[("size", s)]),
                    None => req,
                }
            })
            .await?;
        Ok(Vec::from(resp.bytes().await?))
    }
}