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> {
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)
}
pub async fn rename_texture(&self, tid: u64, name: &str) -> Result<()> {
self.put_unit(&format!("/skinlib/{tid}/name"), &NameBody { name })
.await
}
pub async fn change_texture_model(&self, tid: u64, model: TextureModel) -> Result<()> {
self.put_unit(
&format!("/skinlib/{tid}/type"),
&ModelBody {
texture_type: model,
},
)
.await
}
pub async fn set_texture_public(&self, tid: u64, public: bool) -> Result<()> {
self.put_unit(&format!("/skinlib/{tid}/privacy"), &PrivacyBody { public })
.await
}
pub async fn delete_texture(&self, tid: u64) -> Result<()> {
self.delete_unit(&format!("/skinlib/{tid}")).await
}
}