use crate::{ide::ide_types::NewAnimation, Client, RoboatError};
pub mod ide_types;
const UPLOAD_ANIMATION_API: &str = "https://www.roblox.com/ide/publish/uploadnewanimation?assetTypeName=Animation&name={name}&description={description}&AllID=1&ispublic=False&allowComments=True&isGamesAsset=False&groupId={groupId}";
impl Client {
pub async fn upload_new_animation(
&self,
animation_info: NewAnimation,
) -> Result<String, RoboatError> {
match self
.upload_new_animation_internal(animation_info.clone())
.await
{
Ok(x) => Ok(x),
Err(RoboatError::InvalidXcsrf(new_xcsrf)) => {
self.set_xcsrf(new_xcsrf).await;
self.upload_new_animation_internal(animation_info).await
}
Err(e) => Err(e),
}
}
}
mod internal {
use crate::{
ide::{ide_types::NewAnimation, UPLOAD_ANIMATION_API},
Client, RoboatError, XCSRF_HEADER,
};
use reqwest::header::{self, USER_AGENT};
impl Client {
pub(super) async fn upload_new_animation_internal(
&self,
animation_info: NewAnimation,
) -> Result<String, RoboatError> {
let cookie = self.cookie_string()?;
let xcsrf = self.xcsrf().await;
let mut formatted_url = UPLOAD_ANIMATION_API
.replace("{name}", &animation_info.name)
.replace("{description}", &animation_info.description);
if let Some(group_id) = animation_info.group_id {
formatted_url = formatted_url.replace("{groupId}", &group_id.to_string());
}
let request_result = self
.reqwest_client
.post(formatted_url)
.header(header::COOKIE, cookie)
.body(animation_info.animation_data)
.header(XCSRF_HEADER, xcsrf)
.header(USER_AGENT, "Roblox/WinInet")
.send()
.await;
let response = Self::validate_request_result(request_result).await?;
let response_id = response.text().await.map_err(RoboatError::ReqwestError)?;
Ok(response_id)
}
}
}