use super::ipc_handler::{self, unix_socket, Connect};
use crate::error::{ClientErrorKind, Result};
use derivative::Derivative;
use parsec_interface::requests::{Request, Response};
use std::env;
use std::time::Duration;
const DEFAULT_MAX_BODY_SIZE: usize = usize::max_value();
#[derive(Derivative)]
#[derivative(Debug)]
pub struct RequestClient {
pub max_body_size: usize,
#[derivative(Debug = "ignore")]
pub ipc_handler: Box<dyn Connect + Send + Sync>,
}
impl RequestClient {
pub fn new() -> Result<Self> {
Ok(RequestClient {
ipc_handler: ipc_handler::connector_from_url(url::Url::parse(
&env::var("PARSEC_SERVICE_ENDPOINT")
.unwrap_or(format!("unix:{}", unix_socket::DEFAULT_SOCKET_PATH)),
)?)?,
max_body_size: DEFAULT_MAX_BODY_SIZE,
})
}
pub fn process_request(&self, request: Request) -> Result<Response> {
let mut stream = self.ipc_handler.connect()?;
request
.write_to_stream(&mut stream)
.map_err(ClientErrorKind::Interface)?;
Ok(Response::read_from_stream(&mut stream, self.max_body_size)
.map_err(ClientErrorKind::Interface)?)
}
}
impl Default for RequestClient {
fn default() -> Self {
RequestClient {
max_body_size: DEFAULT_MAX_BODY_SIZE,
ipc_handler: Box::from(unix_socket::Handler::default()),
}
}
}
impl crate::BasicClient {
pub fn set_max_body_size(&mut self, max_body_size: usize) {
self.op_client.request_client.max_body_size = max_body_size;
}
pub fn set_ipc_handler(&mut self, ipc_handler: Box<dyn Connect + Send + Sync>) {
self.op_client.request_client.ipc_handler = ipc_handler;
}
pub fn set_timeout(&mut self, timeout: Option<Duration>) {
self.op_client
.request_client
.ipc_handler
.set_timeout(timeout);
}
}