kode_bridge/ipc_cilent/
unix.rs

1use interprocess::local_socket::tokio::prelude::LocalSocketStream;
2use interprocess::local_socket::{GenericFilePath, Name};
3use interprocess::local_socket::{ToFsName, traits::tokio::Stream};
4
5use crate::errors::{AnyError, AnyResult};
6use crate::ipc_http::http_over_stream::send_http_over_stream;
7use crate::types::Response;
8
9pub struct UnixIpcHttpClient {
10    socket_path: String,
11}
12
13impl UnixIpcHttpClient {
14    pub fn set_path<S>(&mut self, path: S) -> Result<(), AnyError>
15    where
16        S: Into<String>,
17    {
18        self.socket_path = path.into();
19        Ok(())
20    }
21
22    pub fn get_name(&self) -> Result<Name<'_>, AnyError> {
23        Ok(self.socket_path.as_str().to_fs_name::<GenericFilePath>()?)
24    }
25}
26
27impl UnixIpcHttpClient {
28    pub async fn new(socket_path: &str) -> Self {
29        UnixIpcHttpClient {
30            socket_path: socket_path.to_string(),
31        }
32    }
33
34    pub async fn request(
35        &self,
36        method: &str,
37        path: &str,
38        body: Option<&serde_json::Value>,
39    ) -> AnyResult<Response> {
40        let name = self.get_name()?;
41        let stream = LocalSocketStream::connect(name).await?;
42        send_http_over_stream(stream, method, path, body).await
43    }
44}