From 4ea8cc609baa9d07d4cf74d37abef9c75ca4bbea Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 16 Aug 2024 15:57:27 -0700 Subject: [PATCH] cloud: extend operation --- rbx_asset/src/cloud.rs | 48 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/rbx_asset/src/cloud.rs b/rbx_asset/src/cloud.rs index 348ad53..de05590 100644 --- a/rbx_asset/src/cloud.rs +++ b/rbx_asset/src/cloud.rs @@ -88,7 +88,9 @@ impl std::fmt::Display for UpdateError{ } impl std::error::Error for UpdateError{} -#[allow(nonstandard_style,dead_code)] +pub struct GetAssetOperationRequest{ + pub operation_id:String, +} pub struct GetAssetInfoRequest{ pub asset_id:u64, } @@ -221,14 +223,46 @@ impl std::fmt::Display for InventoryPageError{ } impl std::error::Error for InventoryPageError{} +#[derive(Debug)] +pub enum OperationError{ + Get(GetError), + NoOperationId, + NotDone, +} +impl std::fmt::Display for OperationError{ + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f,"{self:?}") + } +} +impl std::error::Error for OperationError{} #[derive(Debug,serde::Deserialize,serde::Serialize)] #[allow(nonstandard_style,dead_code)] pub struct RobloxOperation{ - pub path:Option, + pub path:Option, pub metadata:Option, pub done:Option, pub error:Option, - pub response:Option, + pub response:Option, + pub operationId:Option, +} +impl RobloxOperation{ + pub fn operation_id(&self)->Option<&str>{ + match self.operationId.as_deref(){ + //try getting it from undocumented operationId first + Some(operation_id)=>Some(operation_id), + //skip the first 11 characters + //operations/[uuid] + None=>self.path.as_deref()?.get(11..), + } + } + pub async fn try_get_reponse(&self,context:&CloudContext)->Result{ + context.get_asset_operation(GetAssetOperationRequest{ + operation_id:self.operation_id() + .ok_or(OperationError::NoOperationId)? + .to_owned(), + }).await.map_err(OperationError::Get)? + .response.ok_or(OperationError::NotDone) + } } //idk how to do this better @@ -330,6 +364,14 @@ impl CloudContext{ .error_for_status().map_err(UpdateError::Reqwest)? .json::().await.map_err(UpdateError::Reqwest) } + pub 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)?; + + self.get(url).await.map_err(GetError::Reqwest)? + .error_for_status().map_err(GetError::Reqwest)? + .json::().await.map_err(GetError::Reqwest) + } pub async fn get_asset_info(&self,config:GetAssetInfoRequest)->Result{ let raw_url=format!("https://apis.roblox.com/assets/v1/assets/{}",config.asset_id); let url=reqwest::Url::parse(raw_url.as_str()).map_err(GetError::ParseError)?;