parsec_client/core/
request_client.rs1use super::ipc_handler::{self, unix_socket, Connect};
5use crate::error::{ClientErrorKind, Result};
6use derivative::Derivative;
7use parsec_interface::requests::{Request, Response};
8use std::env;
9use std::time::Duration;
10
11const DEFAULT_MAX_BODY_SIZE: usize = usize::max_value();
12
13#[derive(Derivative)]
19#[derivative(Debug)]
20pub struct RequestClient {
21 pub max_body_size: usize,
25 #[derivative(Debug = "ignore")]
29 pub ipc_handler: Box<dyn Connect + Send + Sync>,
30}
31
32impl RequestClient {
33 pub fn new() -> Result<Self> {
36 Ok(RequestClient {
37 ipc_handler: ipc_handler::connector_from_url(url::Url::parse(
38 &env::var("PARSEC_SERVICE_ENDPOINT")
39 .unwrap_or(format!("unix:{}", unix_socket::DEFAULT_SOCKET_PATH)),
40 )?)?,
41 max_body_size: DEFAULT_MAX_BODY_SIZE,
42 })
43 }
44
45 pub fn process_request(&self, request: Request) -> Result<Response> {
47 let mut stream = self.ipc_handler.connect()?;
49
50 request
51 .write_to_stream(&mut stream)
52 .map_err(ClientErrorKind::Interface)?;
53 Ok(Response::read_from_stream(&mut stream, self.max_body_size)
54 .map_err(ClientErrorKind::Interface)?)
55 }
56}
57
58impl Default for RequestClient {
59 fn default() -> Self {
60 RequestClient {
61 max_body_size: DEFAULT_MAX_BODY_SIZE,
62 ipc_handler: Box::from(unix_socket::Handler::default()),
63 }
64 }
65}
66
67impl crate::BasicClient {
69 pub fn set_max_body_size(&mut self, max_body_size: usize) {
73 self.op_client.request_client.max_body_size = max_body_size;
74 }
75
76 pub fn set_ipc_handler(&mut self, ipc_handler: Box<dyn Connect + Send + Sync>) {
80 self.op_client.request_client.ipc_handler = ipc_handler;
81 }
82
83 pub fn set_timeout(&mut self, timeout: Option<Duration>) {
87 self.op_client
88 .request_client
89 .ipc_handler
90 .set_timeout(timeout);
91 }
92}