use super::error::{ResponseError, RspErr};
use reqwest::{Error, Response};
use serde::Deserialize;
pub(super) async fn process_response<T>(response: Result<Response, Error>) -> RspErr<T>
where
for<'de> T: Deserialize<'de>,
{
match response {
Ok(r) => {
let status = r.status();
let is_success = status.is_success();
match r.json().await {
Ok(m) => Ok(m),
Err(e) => {
if is_success {
Err(ResponseError::DeserializeErr(e))
} else {
Err(ResponseError::HttpErr(status))
}
}
}
}
Err(e) => Err(ResponseError::RequestErr(e)),
}
}