From 964fc24e26b4d7d247cd9fd77acb5e99933efe39 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 18 Dec 2024 14:20:01 -0800 Subject: [PATCH] submissions-api: optional cookie --- validation/api/src/context.rs | 20 ++++++++++++++++++-- validation/api/src/internal.rs | 2 +- validation/api/src/lib.rs | 1 + 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/validation/api/src/context.rs b/validation/api/src/context.rs index 880e704..5715aae 100644 --- a/validation/api/src/context.rs +++ b/validation/api/src/context.rs @@ -1,3 +1,11 @@ +pub struct Cookie(reqwest::header::HeaderValue); + +impl Cookie{ + pub fn new(cookie:&str)->Result{ + Ok(Self(reqwest::header::HeaderValue::from_str(&cookie)?)) + } +} + #[derive(Clone)] pub struct Context{ pub base_url:String, @@ -5,11 +13,19 @@ pub struct Context{ } impl Context{ - pub fn new(mut base_url:String)->reqwest::Result{ + pub fn new(mut base_url:String,cookie:Option)->reqwest::Result{ base_url+="/v1"; Ok(Self{ base_url, - client:reqwest::Client::new(), + client:{ + let mut builder=reqwest::ClientBuilder::new(); + if let Some(cookie)=cookie{ + let mut headers=reqwest::header::HeaderMap::new(); + headers.insert("session_id",cookie.0); + builder=builder.default_headers(headers); + } + builder.build()? + }, }) } pub async fn get(&self,url:impl reqwest::IntoUrl)->Result{ diff --git a/validation/api/src/internal.rs b/validation/api/src/internal.rs index bd636dc..f9926a1 100644 --- a/validation/api/src/internal.rs +++ b/validation/api/src/internal.rs @@ -100,7 +100,7 @@ macro_rules! action{ } impl Context{ pub fn new(base_url:String)->reqwest::Result{ - Ok(Self(crate::context::Context::new(base_url)?)) + Ok(Self(crate::context::Context::new(base_url,None)?)) } pub async fn get_script(&self,config:GetScriptRequest)->Result{ let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0); diff --git a/validation/api/src/lib.rs b/validation/api/src/lib.rs index 7794f6c..d2c119d 100644 --- a/validation/api/src/lib.rs +++ b/validation/api/src/lib.rs @@ -1,4 +1,5 @@ mod context; +pub use context::Cookie; #[cfg(feature="internal")] pub mod internal;