kode_bridge/
ipc_client.rs

1use std::path::Path;
2
3use interprocess::local_socket::tokio::prelude::LocalSocketStream;
4use interprocess::local_socket::traits::tokio::Stream;
5use interprocess::local_socket::{GenericFilePath, Name, ToFsName};
6
7use crate::errors::AnyResult;
8use crate::types::Response;
9use crate::http_client::http_request;
10
11/// Generic IPC HTTP client that works on both Unix and Windows platforms
12pub struct IpcHttpClient {
13    name: Name<'static>,
14}
15
16impl IpcHttpClient {
17    /// Create a new IPC HTTP client with the given socket/named pipe path
18    /// 
19    /// # Arguments
20    /// * `path` - The path to the socket (Unix) or named pipe (Windows)
21    pub fn new<P: AsRef<Path>>(path: P) -> AnyResult<Self> {
22        let name = path
23            .as_ref()
24            .to_fs_name::<GenericFilePath>()?
25            .into_owned();
26        Ok(Self { name })
27    }
28
29    /// Send an HTTP request over the IPC connection
30    /// 
31    /// # Arguments
32    /// * `method` - HTTP method (GET, POST, etc.)
33    /// * `path` - Request path
34    /// * `body` - Optional JSON body
35    pub async fn request(
36        &self,
37        method: &str,
38        path: &str,
39        body: Option<&serde_json::Value>,
40    ) -> AnyResult<Response> {
41        let stream = LocalSocketStream::connect(self.name.clone()).await?;
42        http_request(stream, method, path, body).await
43    }
44}