diff --git a/rbx_asset/src/cloud.rs b/rbx_asset/src/cloud.rs index 7cc81a1..bf8a7a1 100644 --- a/rbx_asset/src/cloud.rs +++ b/rbx_asset/src/cloud.rs @@ -14,26 +14,27 @@ pub struct CreateAssetRequest{ pub displayName:String, } #[derive(Debug)] -pub enum CreateAssetResponseGetAssetError{ +pub enum AssetOperationError{ Operation(OperationError), Serialize(serde_json::Error), } -impl std::fmt::Display for CreateAssetResponseGetAssetError{ +impl std::fmt::Display for AssetOperationError{ fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{ write!(f,"{self:?}") } } -impl std::error::Error for CreateAssetResponseGetAssetError{} -pub struct CreateAssetResponse{ +impl std::error::Error for AssetOperationError{} +#[derive(Debug)] +pub struct AssetOperation{ operation:RobloxOperation, } -impl CreateAssetResponse{ - pub async fn try_get_asset(&self,context:&CloudContext)->Result{ +impl AssetOperation{ + pub async fn try_get_asset(&self,context:&CloudContext)->Result{ serde_json::from_value( self.operation .try_get_reponse(context).await - .map_err(CreateAssetResponseGetAssetError::Operation)? - ).map_err(CreateAssetResponseGetAssetError::Serialize) + .map_err(AssetOperationError::Operation)? + ).map_err(AssetOperationError::Serialize) } } #[derive(Debug)] @@ -111,8 +112,8 @@ impl std::fmt::Display for UpdateError{ } impl std::error::Error for UpdateError{} -pub struct GetAssetOperationRequest{ - pub operation_id:String, +struct GetAssetOperationRequest{ + operation_id:String, } pub struct GetAssetInfoRequest{ pub asset_id:u64, @@ -260,7 +261,7 @@ impl std::fmt::Display for OperationError{ impl std::error::Error for OperationError{} #[derive(Debug,serde::Deserialize,serde::Serialize)] #[allow(nonstandard_style,dead_code)] -pub struct RobloxOperation{ +struct RobloxOperation{ pub path:Option, pub metadata:Option, pub done:Option, @@ -354,7 +355,7 @@ impl CloudContext{ .multipart(form) .send().await } - pub async fn create_asset(&self,config:CreateAssetRequest,body:impl Into>)->Result{ + pub async fn create_asset(&self,config:CreateAssetRequest,body:impl Into>)->Result{ let url=reqwest::Url::parse("https://apis.roblox.com/assets/v1/assets").map_err(CreateError::Parse)?; let request_config=serde_json::to_string(&config).map_err(CreateError::Serialize)?; @@ -367,15 +368,16 @@ impl CloudContext{ .text("request",request_config) .part("fileContent",part); - let operation=self.post_form(url,form).await.map_err(CreateError::Reqwest)? + let operation=self.post_form(url,form).await + .map_err(CreateError::Reqwest)? .error_for_status().map_err(CreateError::Reqwest)? .json::().await.map_err(CreateError::Reqwest)?; - Ok(CreateAssetResponse{ + Ok(AssetOperation{ operation, }) } - pub async fn update_asset(&self,config:UpdateAssetRequest,body:impl Into>)->Result{ + pub async fn update_asset(&self,config:UpdateAssetRequest,body:impl Into>)->Result{ let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}",config.assetId); let url=reqwest::Url::parse(raw_url.as_str()).map_err(UpdateError::ParseError)?; @@ -385,13 +387,17 @@ impl CloudContext{ .text("request",request_config) .part("fileContent",reqwest::multipart::Part::bytes(body)); - self.patch_form(url,form).await + let operation=self.patch_form(url,form).await .map_err(UpdateError::Reqwest)? //roblox api documentation is very poor, just give the status code and drop the json .error_for_status().map_err(UpdateError::Reqwest)? - .json::().await.map_err(UpdateError::Reqwest) + .json::().await.map_err(UpdateError::Reqwest)?; + + Ok(AssetOperation{ + operation, + }) } - pub async fn get_asset_operation(&self,config:GetAssetOperationRequest)->Result{ + async fn get_asset_operation(&self,config:GetAssetOperationRequest)->Result{ let raw_url=format!("https://apis.roblox.com/assets/v1/operations/{}",config.operation_id); let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?; diff --git a/src/main.rs b/src/main.rs index ab0a9b8..e5968c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -624,13 +624,13 @@ struct CreateAssetMediaConfig{ async fn get_asset_exp_backoff( context:&CloudContext, - create_asset_response:&rbx_asset::cloud::CreateAssetResponse -)->Result{ + asset_operation:&rbx_asset::cloud::AssetOperation +)->Result{ let mut backoff:u16=0; loop{ - match create_asset_response.try_get_asset(&context).await{ + match asset_operation.try_get_asset(&context).await{ //try again when the operation is not done - Err(rbx_asset::cloud::CreateAssetResponseGetAssetError::Operation(rbx_asset::cloud::OperationError::NotDone))=>(), + Err(rbx_asset::cloud::AssetOperationError::Operation(rbx_asset::cloud::OperationError::NotDone))=>(), //return all other results other_result=>return other_result, } @@ -690,7 +690,7 @@ impl std::error::Error for CreateAssetMediasError{} #[allow(dead_code)] enum PollOperationError{ CreateAssetMedias(CreateAssetMediasError), - CreateAssetResponseGetAsset(rbx_asset::cloud::CreateAssetResponseGetAssetError), + AssetOperation(rbx_asset::cloud::AssetOperationError), } impl std::fmt::Display for PollOperationError{ fn fmt(&self, f: &mut std::fmt::Formatter<'_>)->std::fmt::Result{ @@ -762,8 +762,8 @@ async fn create_asset_medias(config:CreateAssetMediasConfig)->AResult<()>{ let context=&context; async{(path, async{ - let create_asset_response=create_result.map_err(PollOperationError::CreateAssetMedias)?; - get_asset_exp_backoff(context,&create_asset_response).await.map_err(PollOperationError::CreateAssetResponseGetAsset) + let asset_operation=create_result.map_err(PollOperationError::CreateAssetMedias)?; + get_asset_exp_backoff(context,&asset_operation).await.map_err(PollOperationError::AssetOperation) } .await)} })