submissions-api: optional cookie
This commit is contained in:
parent
a94ae5d61e
commit
964fc24e26
@ -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)]
|
#[derive(Clone)]
|
||||||
pub struct Context{
|
pub struct Context{
|
||||||
pub base_url:String,
|
pub base_url:String,
|
||||||
@ -5,11 +13,19 @@ pub struct Context{
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl 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";
|
base_url+="/v1";
|
||||||
Ok(Self{
|
Ok(Self{
|
||||||
base_url,
|
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>{
|
pub async fn get(&self,url:impl reqwest::IntoUrl)->Result<reqwest::Response,reqwest::Error>{
|
||||||
|
@ -100,7 +100,7 @@ macro_rules! action{
|
|||||||
}
|
}
|
||||||
impl Context{
|
impl Context{
|
||||||
pub fn new(base_url:String)->reqwest::Result<Self>{
|
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>{
|
pub async fn get_script(&self,config:GetScriptRequest)->Result<ScriptResponse,Error>{
|
||||||
let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0);
|
let url_raw=format!("{}/scripts/{}",self.0.base_url,config.ScriptID.0);
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
mod context;
|
mod context;
|
||||||
|
pub use context::Cookie;
|
||||||
|
|
||||||
#[cfg(feature="internal")]
|
#[cfg(feature="internal")]
|
||||||
pub mod internal;
|
pub mod internal;
|
||||||
|
Loading…
Reference in New Issue
Block a user