48 lines
1.3 KiB
Rust

pub struct Cookie(reqwest::header::HeaderValue);
impl Cookie{
/// cookie is prepended with "session_id=" by this function
pub fn new(cookie:&str)->Result<Self,reqwest::header::InvalidHeaderValue>{
Ok(Self(reqwest::header::HeaderValue::from_str(&format!("session_id={}",cookie))?))
}
}
#[derive(Clone)]
pub struct Context{
pub base_url:String,
client:reqwest::Client,
}
impl Context{
pub fn new(base_url:String,cookie:Option<Cookie>)->reqwest::Result<Self>{
Ok(Self{
base_url,
client:{
let mut builder=reqwest::ClientBuilder::new();
if let Some(mut cookie)=cookie{
cookie.0.set_sensitive(true);
let mut headers=reqwest::header::HeaderMap::new();
headers.insert("Cookie",cookie.0);
builder=builder.default_headers(headers);
}
builder.build()?
},
})
}
pub async fn get(&self,url:impl reqwest::IntoUrl)->Result<reqwest::Response,reqwest::Error>{
self.client.get(url)
.send().await
}
#[cfg(feature="internal")]
pub async fn post_empty_body(&self,url:impl reqwest::IntoUrl)->Result<reqwest::Response,reqwest::Error>{
self.client.post(url)
.send().await
}
pub async fn post(&self,url:impl reqwest::IntoUrl,body:impl Into<reqwest::Body>)->Result<reqwest::Response,reqwest::Error>{
self.client.post(url)
.header("Content-Type","application/json")
.body(body)
.send().await
}
}