submissions-api: optional cookie

This commit is contained in:
Quaternions 2024-12-18 14:20:01 -08:00
parent a94ae5d61e
commit 964fc24e26
3 changed files with 20 additions and 3 deletions

View File

@ -1,3 +1,11 @@
pub struct Cookie(reqwest::header::HeaderValue);
impl Cookie{
pub fn new(cookie:&str)->Result<Self,reqwest::header::InvalidHeaderValue>{
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<Self>{
pub fn new(mut base_url:String,cookie:Option<Cookie>)->reqwest::Result<Self>{
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<reqwest::Response,reqwest::Error>{

View File

@ -100,7 +100,7 @@ macro_rules! action{
}
impl Context{
pub fn new(base_url:String)->reqwest::Result<Self>{
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<ScriptResponse,Error>{
let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0);

View File

@ -1,4 +1,5 @@
mod context;
pub use context::Cookie;
#[cfg(feature="internal")]
pub mod internal;