use gaunt;
use uuid::Uuid;
use super::config::HttpConfig;
use crate::connector::{Connection, ConnectionError};
pub struct HttpConnection(gaunt::Connection);
impl HttpConnection {
pub(crate) fn open(config: &HttpConfig) -> Result<Self, ConnectionError> {
Ok(HttpConnection(gaunt::Connection::new(
&config.addr,
config.port,
&Default::default(),
)?))
}
pub(super) fn get(&self, path: &str) -> Result<Vec<u8>, ConnectionError> {
Ok(self.0.get(path, &Default::default())?.into_vec())
}
pub(super) fn post(
&self,
path: &str,
_uuid: Uuid,
body: &[u8],
) -> Result<Vec<u8>, ConnectionError> {
Ok(self
.0
.post(path, &gaunt::request::Body::from(body))?
.into_vec())
}
}
impl Connection for HttpConnection {
fn send_message(&self, uuid: Uuid, cmd: Vec<u8>) -> Result<Vec<u8>, ConnectionError> {
self.post("/connector/api", uuid, &cmd)
}
}