2024-12-15 08:55:11 +00:00
|
|
|
mod context;
|
2024-12-18 22:20:01 +00:00
|
|
|
pub use context::Cookie;
|
2024-12-15 08:55:11 +00:00
|
|
|
|
|
|
|
#[cfg(feature="internal")]
|
|
|
|
pub mod internal;
|
|
|
|
|
|
|
|
//lazy reexport
|
|
|
|
pub type ReqwestError=reqwest::Error;
|
|
|
|
|
2024-12-05 00:46:20 +00:00
|
|
|
#[derive(Debug)]
|
2024-12-07 03:36:10 +00:00
|
|
|
pub enum Error{
|
2024-12-05 00:46:20 +00:00
|
|
|
ParseError(url::ParseError),
|
|
|
|
Reqwest(reqwest::Error),
|
2024-12-18 03:31:45 +00:00
|
|
|
Response(ResponseError),
|
2024-12-14 08:15:05 +00:00
|
|
|
JSON(serde_json::Error),
|
2024-12-05 00:46:20 +00:00
|
|
|
}
|
2024-12-07 03:36:10 +00:00
|
|
|
impl std::fmt::Display for Error{
|
2024-12-05 00:46:20 +00:00
|
|
|
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{}
|
2024-12-18 03:31:45 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct StatusCodeWithUrlAndBody{
|
|
|
|
pub status_code:reqwest::StatusCode,
|
|
|
|
pub url:url::Url,
|
|
|
|
pub body:String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ResponseError{
|
|
|
|
Reqwest(reqwest::Error),
|
|
|
|
StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody),
|
|
|
|
}
|
|
|
|
impl std::fmt::Display for ResponseError{
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f,"{self:?}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl std::error::Error for ResponseError{}
|
|
|
|
// lazy function to draw out meaningful info from http response on failure
|
|
|
|
pub async fn response_ok(response:reqwest::Response)->Result<reqwest::Response,ResponseError>{
|
|
|
|
let status_code=response.status();
|
|
|
|
if status_code.is_success(){
|
|
|
|
Ok(response)
|
|
|
|
}else{
|
|
|
|
let url=response.url().to_owned();
|
|
|
|
let bytes=response.bytes().await.map_err(ResponseError::Reqwest)?;
|
|
|
|
let body=String::from_utf8_lossy(&bytes).to_string();
|
|
|
|
Err(ResponseError::StatusCodeWithUrlAndBody(StatusCodeWithUrlAndBody{
|
|
|
|
status_code,
|
|
|
|
url,
|
|
|
|
body,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|