maps-service/submissions-api-rs/src/lib.rs

152 lines
4.2 KiB
Rust
Raw Normal View History

#[derive(Debug)]
2024-12-07 03:36:10 +00:00
pub enum Error{
ParseError(url::ParseError),
Reqwest(reqwest::Error),
}
2024-12-07 03:36:10 +00:00
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
write!(f,"{self:?}")
}
}
2024-12-07 03:36:10 +00:00
impl std::error::Error for Error{}
#[derive(serde::Deserialize)]
pub struct ScriptID(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,
}
#[derive(serde::Deserialize)]
#[repr(i32)]
pub enum Policy{
2024-12-14 05:50:19 +00:00
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,
2024-12-07 03:02:57 +00:00
pub FromScriptHash:String,
pub ToScriptID:ScriptID,
pub Policy:Policy
}
#[allow(nonstandard_style)]
pub struct UpdateSubmissionModelRequest{
2024-12-14 20:44:56 +00:00
pub SubmissionID:i64,
pub ModelID:u64,
pub ModelVersion:u64,
}
2024-12-07 03:42:31 +00:00
pub struct SubmissionID(pub i64);
2024-12-14 20:44:56 +00:00
#[allow(nonstandard_style)]
pub struct ActionSubmissionUploadedRequest{
pub SubmissionID:i64,
pub TargetAssetID:Option<u64>,
}
#[derive(Clone)]
pub struct Context{
base_url:String,
client:reqwest::Client,
}
pub type ReqwestError=reqwest::Error;
2024-12-07 05:00:19 +00:00
// there are lots of action endpoints and they all follow the same pattern
macro_rules! action{
2024-12-12 01:54:11 +00:00
($fname:ident,$action:expr)=>{
2024-12-07 05:00:19 +00:00
pub async fn $fname(&self,config:SubmissionID)->Result<(),Error>{
2024-12-12 01:54:11 +00:00
let url_raw=format!(concat!("{}/submissions/{}/status/",$action),self.base_url,config.0);
2024-12-07 05:00:19 +00:00
let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?;
2024-12-11 06:28:52 +00:00
self.post(url).await.map_err(Error::Reqwest)?
2024-12-07 05:00:19 +00:00
.error_for_status().map_err(Error::Reqwest)?;
Ok(())
}
};
}
impl Context{
2024-12-11 06:29:15 +00:00
pub fn new(mut base_url:String)->reqwest::Result<Self>{
base_url+="/v1";
Ok(Self{
base_url,
client:reqwest::Client::new(),
})
}
async fn get(&self,url:impl reqwest::IntoUrl)->Result<reqwest::Response,reqwest::Error>{
self.client.get(url)
.send().await
}
2024-12-11 06:28:52 +00:00
async fn post(&self,url:impl reqwest::IntoUrl)->Result<reqwest::Response,reqwest::Error>{
self.client.post(url)
2024-12-06 02:27:25 +00:00
.send().await
}
2024-12-07 03:36:10 +00:00
pub async fn get_script(&self,config:GetScriptRequest)->Result<ScriptResponse,Error>{
let url_raw=format!("{}/scripts/{}",self.base_url,config.ScriptID.0);
2024-12-07 03:36:10 +00:00
let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?;
2024-12-07 03:36:10 +00:00
self.get(url).await.map_err(Error::Reqwest)?
.error_for_status().map_err(Error::Reqwest)?
.json().await.map_err(Error::Reqwest)
}
2024-12-07 03:36:10 +00:00
pub async fn get_script_policy_from_hash(&self,config:ScriptPolicyHashRequest)->Result<ScriptPolicyResponse,Error>{
let url_raw=format!("{}/script-policy/hash/{}",self.base_url,config.hash);
2024-12-07 03:36:10 +00:00
let url=reqwest::Url::parse(url_raw.as_str()).map_err(Error::ParseError)?;
2024-12-07 03:36:10 +00:00
self.get(url).await.map_err(Error::Reqwest)?
.error_for_status().map_err(Error::Reqwest)?
.json().await.map_err(Error::Reqwest)
}
2024-12-07 03:36:10 +00:00
pub async fn update_submission_model(&self,config:UpdateSubmissionModelRequest)->Result<(),Error>{
2024-12-14 20:44:56 +00:00
let url_raw=format!("{}/submissions/{}/model",self.base_url,config.SubmissionID);
2024-12-07 03:36:10 +00:00
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());
}
2024-12-11 06:28:52 +00:00
self.post(url).await.map_err(Error::Reqwest)?
2024-12-07 03:36:10 +00:00
.error_for_status().map_err(Error::Reqwest)?;
2024-12-07 03:42:31 +00:00
Ok(())
}
2024-12-14 20:44:56 +00:00
pub async fn action_submission_uploaded(&self,config:ActionSubmissionUploadedRequest)->Result<(),Error>{
let url_raw=format!("{}/submissions/{}/status/uploaded",self.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());
}
self.post(url).await.map_err(Error::Reqwest)?
.error_for_status().map_err(Error::Reqwest)?;
Ok(())
}
action!(action_submission_validated,"validator-validated");
action!(action_submission_released,"releaser-released");
}