use crate::Error; #[derive(Clone,Copy,serde::Serialize,serde::Deserialize)] pub struct ScriptID(i64); #[derive(Clone,Copy,serde::Serialize,serde::Deserialize)] pub struct ScriptPolicyID(i64); #[allow(nonstandard_style)] pub struct GetScriptRequest{ pub ScriptID:ScriptID, } #[allow(nonstandard_style)] #[derive(serde::Deserialize)] pub struct ScriptResponse{ pub ID:i64, pub Name:String, pub Hash:String, pub Source:String, pub SubmissionID:i64, } #[allow(nonstandard_style)] #[derive(serde::Serialize)] pub struct CreateScriptRequest<'a>{ pub Name:&'a str, pub Source:&'a str, pub SubmissionID:Option, } #[allow(nonstandard_style)] #[derive(serde::Deserialize)] pub struct ScriptIDResponse{ pub ID:ScriptID, } #[derive(serde_repr::Serialize_repr,serde_repr::Deserialize_repr)] #[repr(i32)] pub enum Policy{ None=0, // not yet reviewed Allowed=1, Blocked=2, Delete=3, Replace=4, } pub struct ScriptPolicyHashRequest<'a>{ pub hash:&'a str, } #[allow(nonstandard_style)] #[derive(serde::Deserialize)] pub struct ScriptPolicyResponse{ pub ID:i64, pub FromScriptHash:String, pub ToScriptID:ScriptID, pub Policy:Policy } #[allow(nonstandard_style)] #[derive(serde::Serialize)] pub struct CreateScriptPolicyRequest{ pub FromScriptID:ScriptID, pub ToScriptID:ScriptID, pub Policy:Policy, } #[allow(nonstandard_style)] #[derive(serde::Deserialize)] pub struct ScriptPolicyIDResponse{ pub ID:ScriptPolicyID, } #[allow(nonstandard_style)] pub struct UpdateSubmissionModelRequest{ pub SubmissionID:i64, pub ModelID:u64, pub ModelVersion:u64, } #[allow(nonstandard_style)] pub struct ActionSubmissionUploadedRequest{ pub SubmissionID:i64, pub TargetAssetID:Option, } pub struct SubmissionID(pub i64); #[derive(Clone)] pub struct Context(crate::context::Context); // there are lots of action endpoints and they all follow the same pattern macro_rules! action{ ($fname:ident,$action:expr)=>{ pub async fn $fname(&self,config:SubmissionID)->Result<(),Error>{ let url_raw=format!(concat!("{}/submissions/{}/status/",$action),self.0.base_url,config.0); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; crate::response_ok( self.0.post_empty_body(url).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)?; Ok(()) } }; } impl Context{ pub fn new(base_url:String)->reqwest::Result{ Ok(Self(crate::context::Context::new(base_url)?)) } pub async fn get_script(&self,config:GetScriptRequest)->Result{ let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; crate::response_ok( self.0.get(url).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn create_script<'a>(&self,config:CreateScriptRequest<'a>)->Result{ let url_raw=format!("{}/scripts",self.0.base_url); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; let body=serde_json::to_string(&config).map_err(Error::JSON)?; crate::response_ok( self.0.post(url,body).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn get_script_policy_from_hash<'a>(&self,config:ScriptPolicyHashRequest<'a>)->Result{ let url_raw=format!("{}/script-policy/hash/{}",self.0.base_url,config.hash); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; crate::response_ok( self.0.get(url).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn create_script_policy(&self,config:CreateScriptPolicyRequest)->Result{ let url_raw=format!("{}/script-policy",self.0.base_url); let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; let body=serde_json::to_string(&config).map_err(Error::JSON)?; crate::response_ok( self.0.post(url,body).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)? .json().await.map_err(Error::Reqwest) } pub async fn update_submission_model(&self,config:UpdateSubmissionModelRequest)->Result<(),Error>{ let url_raw=format!("{}/submissions/{}/model",self.0.base_url,config.SubmissionID); let mut url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; { url.query_pairs_mut() .append_pair("ModelID",config.ModelID.to_string().as_str()) .append_pair("ModelVersion",config.ModelVersion.to_string().as_str()); } crate::response_ok( self.0.post_empty_body(url).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)?; Ok(()) } pub async fn action_submission_uploaded(&self,config:ActionSubmissionUploadedRequest)->Result<(),Error>{ let url_raw=format!("{}/submissions/{}/status/validator-uploaded",self.0.base_url,config.SubmissionID); let mut url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?; if let Some(target_asset_id)=config.TargetAssetID{ url.query_pairs_mut() .append_pair("TargetAssetID",target_asset_id.to_string().as_str()); } crate::response_ok( self.0.post_empty_body(url).await.map_err(Error::Reqwest)? ).await.map_err(Error::Response)?; Ok(()) } action!(action_submission_validated,"validator-validated"); action!(action_submission_released,"releaser-released"); }