use crate::error::BaseError;
use reqwest::header::HeaderMap;
use reqwest::Response as ReqwestResponse;
pub struct Response {
response: ReqwestResponse,
}
impl Response {
pub(crate) fn new(response: ReqwestResponse) -> Self {
Self { response }
}
pub fn status(&self) -> u16 {
self.response.status().as_u16()
}
pub fn is_success(&self) -> bool {
self.response.status().is_success()
}
pub fn headers(&self) -> &HeaderMap {
self.response.headers()
}
pub async fn text(self) -> Result<String, BaseError> {
self.response
.text()
.await
.map_err(|e| BaseError::HttpResponseParseFailed(e.to_string()))
}
pub async fn bytes(self) -> Result<Vec<u8>, BaseError> {
self.response
.bytes()
.await
.map(|b| b.to_vec())
.map_err(|e| BaseError::HttpResponseParseFailed(e.to_string()))
}
pub async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T, BaseError> {
self.response
.json()
.await
.map_err(|e| BaseError::JsonDeserializeFailed(e.to_string()))
}
}