From 518327820deb401f0b4088acba6f4dc1472b97c0 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 18 Dec 2024 14:28:07 -0800 Subject: [PATCH] submissions-api: reintroduce external api --- validation/api/Cargo.toml | 1 + validation/api/src/external.rs | 144 +++++++++++++++++++++++++++++++++ validation/api/src/lib.rs | 3 + 3 files changed, 148 insertions(+) create mode 100644 validation/api/src/external.rs diff --git a/validation/api/Cargo.toml b/validation/api/Cargo.toml index eb75b1a..9083752 100644 --- a/validation/api/Cargo.toml +++ b/validation/api/Cargo.toml @@ -20,3 +20,4 @@ url = "2" [features] default = ["internal"] internal = [] +external = [] diff --git a/validation/api/src/external.rs b/validation/api/src/external.rs new file mode 100644 index 0000000..745b25e --- /dev/null +++ b/validation/api/src/external.rs @@ -0,0 +1,144 @@ +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)] +#[derive(serde::Serialize)] +pub struct UpdateScriptPolicyRequest{ + pub ScriptPolicyID:ScriptPolicyID, + pub FromScriptID:Option, + pub ToScriptID:Option, + pub Policy:Option, +} + +#[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); + +impl Context{ + pub fn new(base_url:String,cookie:crate::context::Cookie)->reqwest::Result{ + Ok(Self(crate::context::Context::new(base_url,Some(cookie))?)) + } + 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_script_policy(&self,config:UpdateScriptPolicyRequest)->Result<(),Error>{ + let url_raw=format!("{}/script-policy/id/{}",self.0.base_url,config.ScriptPolicyID.0); + 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)?; + + Ok(()) + } +} diff --git a/validation/api/src/lib.rs b/validation/api/src/lib.rs index d2c119d..49ea98d 100644 --- a/validation/api/src/lib.rs +++ b/validation/api/src/lib.rs @@ -4,6 +4,9 @@ pub use context::Cookie; #[cfg(feature="internal")] pub mod internal; +#[cfg(feature="external")] +pub mod external; + //lazy reexport pub type ReqwestError=reqwest::Error;