use nitro_auth::mc::{Keypair, call_mc_api};
use nitro_shared::minecraft::{MinecraftUserProfile, SkinVariant};
use reqwest::{
Client,
multipart::{Form, Part},
};
use serde::{Deserialize, Serialize};
pub async fn get_user_profile(
access_token: &str,
client: &Client,
) -> anyhow::Result<MinecraftUserProfile> {
call_mc_api(
"https://api.minecraftservices.com/minecraft/profile",
access_token,
client,
)
.await
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MinecraftUserCertificate {
pub key_pair: Keypair,
}
pub async fn get_user_certificate(
access_token: &str,
client: &Client,
) -> anyhow::Result<MinecraftUserCertificate> {
let response = client
.post("https://api.minecraftservices.com/player/certificates")
.header("Authorization", format!("Bearer {access_token}"))
.send()
.await?
.error_for_status()?
.json()
.await?;
Ok(response)
}
pub async fn upload_skin(
variant: SkinVariant,
skin: &[u8],
access_token: &str,
client: &Client,
) -> anyhow::Result<()> {
let variant = match variant {
SkinVariant::Classic => "classic",
SkinVariant::Slim => "slim",
};
let form = Form::new().text("variant", variant).part(
"file",
Part::bytes(skin.to_vec())
.file_name("skin.png")
.mime_str("image/png")?,
);
client
.post("https://api.minecraftservices.com/minecraft/profile/skins")
.header("Authorization", format!("Bearer {access_token}"))
.multipart(form)
.send()
.await?
.error_for_status()?;
Ok(())
}
pub async fn activate_cape(
cape_id: &str,
access_token: &str,
client: &Client,
) -> anyhow::Result<()> {
let body = ActivateCapeRequest {
cape_id: cape_id.to_string(),
};
client
.put("https://api.minecraftservices.com/minecraft/profile/capes/active")
.header("Authorization", format!("Bearer {access_token}"))
.json(&body)
.send()
.await?
.error_for_status()?;
Ok(())
}
#[derive(Serialize)]
struct ActivateCapeRequest {
#[serde(rename = "capeId")]
cape_id: String,
}
pub async fn deactivate_cape(access_token: &str, client: &Client) -> anyhow::Result<()> {
client
.delete("https://api.minecraftservices.com/minecraft/profile/capes/active")
.header("Authorization", format!("Bearer {access_token}"))
.send()
.await?
.error_for_status()?;
Ok(())
}