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 Hash:String, pub Source:String, pub SubmissionID:i64, } #[allow(nonstandard_style)] #[derive(serde::Serialize)] pub struct CreateScriptRequest{ pub Source:String, pub SubmissionID:Option, } #[allow(nonstandard_style)] #[derive(serde::Deserialize)] pub struct ScriptIDResponse{ pub ID:ScriptID, } #[derive(serde::Serialize,serde::Deserialize)] #[repr(i32)] pub enum Policy{ None=0, // not yet reviewed Allowed=1, Blocked=2, Delete=3, Replace=4, } pub struct ScriptPolicyHashRequest{ pub hash:String, } #[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, } #[derive(Clone)] pub struct Context(crate::context::Context); 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)?; self.0.get(url).await.map_err(Error::Reqwest)? .error_for_status().map_err(Error::Reqwest)? .json().await.map_err(Error::Reqwest) } pub async fn create_script(&self,config:CreateScriptRequest)->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)?; self.0.post(url,body).await.map_err(Error::Reqwest)? .error_for_status().map_err(Error::Reqwest)? .json().await.map_err(Error::Reqwest) } pub async fn get_script_policy_from_hash(&self,config:ScriptPolicyHashRequest)->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)?; self.0.get(url).await.map_err(Error::Reqwest)? .error_for_status().map_err(Error::Reqwest)? .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)?; self.0.post(url,body).await.map_err(Error::Reqwest)? .error_for_status().map_err(Error::Reqwest)? .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()); } self.0.post_empty_body(url).await.map_err(Error::Reqwest)? .error_for_status().map_err(Error::Reqwest)?; Ok(()) } }