use crate::http_status_code::HttpStatusCode;
#[derive(Debug)]
pub struct HttpResponse {
inner: reqwest::Response,
}
impl HttpResponse {
pub fn new(inner: reqwest::Response) -> Self {
Self { inner }
}
pub fn as_reqwest(&self) -> &reqwest::Response {
&self.inner
}
pub fn as_mut_reqwest(&mut self) -> &mut reqwest::Response {
&mut self.inner
}
pub fn into_inner(self) -> reqwest::Response {
self.inner
}
pub fn status(&self) -> HttpStatusCode {
self.inner.status().into()
}
pub async fn json<T: serde::de::DeserializeOwned>(self) -> crate::error::HttpResult<T> {
let status = self.inner.status();
let url = self.inner.url().clone();
let bytes = match self.inner.bytes().await {
Ok(bytes) => bytes,
Err(e) => return Err(crate::error::HttpError::Reqwest(e)),
};
match serde_json::from_slice::<T>(&bytes) {
Ok(data) => Ok(data),
Err(serde_err) => {
let body_text = String::from_utf8_lossy(&bytes);
let error_msg = format!(
"Failed to deserialize JSON response from {} (status: {}). \
Response body: {}. \
Serde error: {}",
url, status, body_text, serde_err
);
Err(crate::error::HttpError::Custom(error_msg))
}
}
}
pub async fn json_bytes(self) -> crate::error::HttpResult<bytes::Bytes> {
self.inner
.bytes()
.await
.map_err(crate::error::HttpError::from)
}
pub async fn text(self) -> crate::error::HttpResult<String> {
self.inner
.text()
.await
.map_err(crate::error::HttpError::from)
}
}