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 super::models::{ModifyUserRequest, PayoutHistory, Project, User};
use super::{Authed, ModrinthClient, encode_id_or_slug};
use crate::error::Result;

impl ModrinthClient {
    /// Fetches a user by ID or username via `GET /v2/user/{id|username}`.
    pub async fn get_user(&self, id_or_username: &str) -> Result<User> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.get_json(&format!("/user/{seg}")).await
    }

    /// Batch-fetches users via `GET /v2/users?ids=[...]`.
    pub async fn get_users(&self, ids: &[&str]) -> Result<Vec<User>> {
        self.get_by_ids("/users", ids).await
    }

    /// Lists a user's projects via `GET /v2/user/{id|username}/projects`.
    pub async fn get_user_projects(&self, id_or_username: &str) -> Result<Vec<Project>> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.get_json(&format!("/user/{seg}/projects")).await
    }
}

impl<'a> Authed<'a> {
    /// Fetches the currently authenticated user via `GET /v2/user`.
    pub async fn get_current_user(&self) -> Result<User> {
        self.get_authed_json("/user").await
    }

    /// Modifies a user profile via `PATCH /v2/user/{id|username}`.
    pub async fn modify_user(&self, id_or_username: &str, req: &ModifyUserRequest) -> Result<()> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.patch_authed_json(&format!("/user/{seg}"), req).await
    }

    /// Lists projects followed by a user via `GET /v2/user/{id|username}/follows`.
    pub async fn get_followed_projects(&self, id_or_username: &str) -> Result<Vec<Project>> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.get_authed_json(&format!("/user/{seg}/follows")).await
    }

    /// Fetches a user's payout history via `GET /v2/user/{id|username}/payouts`.
    pub async fn get_user_payouts(&self, id_or_username: &str) -> Result<PayoutHistory> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.get_authed_json(&format!("/user/{seg}/payouts")).await
    }

    /// Changes a user's avatar icon via `PATCH /v2/user/{id|username}/icon`.
    pub async fn change_user_icon(
        &self,
        id_or_username: &str,
        ext: &str,
        bytes: Vec<u8>,
        content_type: &str,
    ) -> Result<()> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.upload_icon(&format!("/user/{seg}/icon"), ext, bytes, content_type)
            .await
    }

    /// Requests a payout withdrawal via `POST /v2/user/{id|username}/payouts`.
    pub async fn withdraw_payout(&self, id_or_username: &str, amount: f64) -> Result<()> {
        let seg = encode_id_or_slug(id_or_username)?;
        self.send_authed(&format!("/user/{seg}/payouts"), |c, t| {
            c.post(t).query(&[("amount", amount)])
        })
        .await?;
        Ok(())
    }
}