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 serde::Serialize;

use super::models::{BlessingPlayer, BlessingUser, SignInResult};
use super::{Authed, NameBody};
use crate::error::Result;

#[derive(Serialize)]
struct SetTexturesBody {
    #[serde(skip_serializing_if = "Option::is_none")]
    skin: Option<u64>,
    #[serde(skip_serializing_if = "Option::is_none")]
    cape: Option<u64>,
}

#[derive(Serialize)]
struct ClearTexturesBody<'a> {
    #[serde(rename = "type")]
    types: &'a [&'a str],
}

impl<'a> Authed<'a> {
    /// Logs out and revokes the active token via `POST /api/auth/logout`.
    pub async fn logout(&self) -> Result<()> {
        self.post_unit("/auth/logout", &serde_json::json!({})).await
    }

    /// Fetches the authenticated user's profile via `GET /api/user`.
    pub async fn current_user(&self) -> Result<BlessingUser> {
        self.get_envelope("/user").await
    }

    /// Performs the daily check-in reward via `POST /api/user/sign`.
    pub async fn daily_sign_in(&self) -> Result<SignInResult> {
        self.post_envelope("/user/sign", &serde_json::json!({}))
            .await
    }

    /// Lists all game profiles belonging to the user via `GET /api/players`.
    pub async fn list_players(&self) -> Result<Vec<BlessingPlayer>> {
        self.get_envelope("/players").await
    }

    /// Creates a new player character via `POST /api/players`.
    pub async fn create_player(&self, name: &str) -> Result<BlessingPlayer> {
        self.post_envelope("/players", &NameBody { name }).await
    }

    /// Renames a player character via `PUT /api/players/{pid}/name`.
    pub async fn rename_player(&self, pid: u64, name: &str) -> Result<()> {
        self.put_unit(&format!("/players/{pid}/name"), &NameBody { name })
            .await
    }

    /// Assigns skin and/or cape texture IDs to a player via `PUT /api/players/{pid}/textures`.
    pub async fn set_player_textures(
        &self,
        pid: u64,
        skin_tid: Option<u64>,
        cape_tid: Option<u64>,
    ) -> Result<()> {
        self.put_unit(
            &format!("/players/{pid}/textures"),
            &SetTexturesBody {
                skin: skin_tid,
                cape: cape_tid,
            },
        )
        .await
    }

    /// Clears selected texture slots (`"skin"` and/or `"cape"`) via
    /// `DELETE /api/players/{pid}/textures`.
    pub async fn clear_player_textures(&self, pid: u64, clear_types: &[&str]) -> Result<()> {
        let body = ClearTexturesBody { types: clear_types };
        self.send_unit(&format!("/players/{pid}/textures"), |c, t| {
            c.delete(t).json(&body)
        })
        .await
    }

    /// Deletes a player character via `DELETE /api/players/{pid}`.
    pub async fn delete_player(&self, pid: u64) -> Result<()> {
        self.delete_unit(&format!("/players/{pid}")).await
    }
}