mod context;
pub use context::Cookie;

#[cfg(feature="internal")]
pub mod internal;

#[cfg(feature="external")]
pub mod external;

//lazy reexport
pub type ReqwestError=reqwest::Error;

#[derive(Debug)]
pub enum Error{
	ParseError(url::ParseError),
	Reqwest(reqwest::Error),
	Response(ResponseError),
	JSON(serde_json::Error),
}
impl std::fmt::Display for Error{
	fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
		write!(f,"{self:?}")
	}
}
impl std::error::Error for Error{}

#[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,
		}))
	}
}