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 reqwest::header::{ACCEPT, AUTHORIZATION};
use reqwest::multipart::{Form, Part};
use serde::Serialize;

use super::models::{BlessingEnvelope, TextureModel, UploadTextureRequest, UploadedTexture};
use super::{ACCEPT_JSON, Authed, NameBody, unwrap_envelope};
use crate::error::Result;
use crate::http::bearer_header;

#[derive(Serialize)]
struct ModelBody {
    #[serde(rename = "type")]
    texture_type: TextureModel,
}

#[derive(Serialize)]
struct PrivacyBody {
    public: bool,
}

impl<'a> Authed<'a> {
    /// Uploads a new skin or cape PNG texture via multipart `POST /api/skinlib`.
    ///
    /// Laravel's `boolean` validation rule accepts `"1"` / `"0"` for multipart form strings.
    pub async fn upload_texture(&self, req: UploadTextureRequest) -> Result<UploadedTexture> {
        let url = self.client.api("/skinlib");
        let auth = bearer_header(self.token)?;
        let texture_type = req.texture_type.as_str();
        let public = if req.public { "1" } else { "0" };

        let resp = self
            .client
            .http
            .send_direct_oneshot(&url, move |client, target| {
                let part = Part::bytes(req.png_bytes)
                    .file_name(req.file_name)
                    .mime_str("image/png")
                    .expect("static mime type is valid");
                let form = Form::new()
                    .text("name", req.name)
                    .text("type", texture_type)
                    .text("public", public)
                    .part("file", part);
                client
                    .post(target)
                    .header(ACCEPT, ACCEPT_JSON)
                    .header(AUTHORIZATION, auth)
                    .multipart(form)
            })
            .await?;

        let env: BlessingEnvelope<UploadedTexture> = resp.json().await?;
        unwrap_envelope(env)
    }

    /// Renames a texture via `PUT /api/skinlib/{tid}/name`.
    pub async fn rename_texture(&self, tid: u64, name: &str) -> Result<()> {
        self.put_unit(&format!("/skinlib/{tid}/name"), &NameBody { name })
            .await
    }

    /// Changes a texture's model (`steve`, `alex`, or `cape`) via `PUT /api/skinlib/{tid}/type`.
    pub async fn change_texture_model(&self, tid: u64, model: TextureModel) -> Result<()> {
        self.put_unit(
            &format!("/skinlib/{tid}/type"),
            &ModelBody {
                texture_type: model,
            },
        )
        .await
    }

    /// Sets a texture's public/private visibility via `PUT /api/skinlib/{tid}/privacy`.
    pub async fn set_texture_public(&self, tid: u64, public: bool) -> Result<()> {
        self.put_unit(&format!("/skinlib/{tid}/privacy"), &PrivacyBody { public })
            .await
    }

    /// Deletes a texture via `DELETE /api/skinlib/{tid}`.
    pub async fn delete_texture(&self, tid: u64) -> Result<()> {
        self.delete_unit(&format!("/skinlib/{tid}")).await
    }
}