use crate::{client::HTTP_SUCCESS_CODES, errors::ClientError};
use bytes::Bytes;
use http::{Request, Response};
pub trait Client {
fn send(&self, req: Request<Bytes>) -> Result<Response<Bytes>, ClientError>;
fn base(&self) -> &str;
fn execute(&self, req: Request<Bytes>) -> Result<Response<Bytes>, ClientError> {
log::info!(
"Client sending {} request to {} with {} bytes of data",
req.method().to_string(),
req.uri(),
req.body().len(),
);
let response = self.send(req)?;
log::info!(
"Client received {} response with {} bytes of body data",
response.status().as_u16(),
response.body().len()
);
if !HTTP_SUCCESS_CODES.contains(&response.status().as_u16()) {
return Err(ClientError::ServerResponseError {
code: response.status().as_u16(),
content: String::from_utf8(response.body().to_vec()).ok(),
});
}
Ok(response)
}
}