vectorizer_sdk/
transport.rs

1//! Transport abstraction layer for Vectorizer client.
2//!
3//! Supports multiple transport protocols:
4//! - HTTP/HTTPS (default)
5//! - UMICP (Universal Messaging and Inter-process Communication Protocol)
6
7use async_trait::async_trait;
8use serde_json::Value;
9
10use crate::error::Result;
11
12/// Transport protocol type
13#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Protocol {
15    /// HTTP/HTTPS protocol
16    Http,
17    /// UMICP protocol
18    #[cfg(feature = "umicp")]
19    Umicp,
20}
21
22impl std::fmt::Display for Protocol {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Protocol::Http => write!(f, "http"),
26            #[cfg(feature = "umicp")]
27            Protocol::Umicp => write!(f, "umicp"),
28        }
29    }
30}
31
32/// Transport trait for making requests
33#[async_trait]
34pub trait Transport: Send + Sync {
35    /// Make a GET request
36    async fn get(&self, path: &str) -> Result<String>;
37
38    /// Make a POST request
39    async fn post(&self, path: &str, data: Option<&Value>) -> Result<String>;
40
41    /// Make a PUT request
42    async fn put(&self, path: &str, data: Option<&Value>) -> Result<String>;
43
44    /// Make a DELETE request
45    async fn delete(&self, path: &str) -> Result<String>;
46
47    /// Get the protocol being used
48    fn protocol(&self) -> Protocol;
49}
50
51/// Parse a connection string into protocol and connection details
52///
53/// Examples:
54/// - "http://localhost:15002" -> HTTP transport
55/// - "https://api.example.com" -> HTTPS transport
56/// - "umicp://localhost:15003" -> UMICP transport
57pub fn parse_connection_string(connection_string: &str) -> Result<(Protocol, String, Option<u16>)> {
58    // Simple manual parsing
59    let parts: Vec<&str> = connection_string.split("://").collect();
60
61    if parts.len() != 2 {
62        return Err(crate::error::VectorizerError::configuration(
63            "Invalid connection string format. Expected protocol://host[:port]",
64        ));
65    }
66
67    let scheme = parts[0];
68    let authority = parts[1];
69
70    // Parse host and port
71    #[allow(unused_variables)]
72    let (host, port) = if authority.contains(':') {
73        let host_port: Vec<&str> = authority.split(':').collect();
74        if host_port.len() != 2 {
75            return Err(crate::error::VectorizerError::configuration(
76                "Invalid host:port format",
77            ));
78        }
79        let port = host_port[1]
80            .parse::<u16>()
81            .map_err(|_| crate::error::VectorizerError::configuration("Invalid port number"))?;
82        (host_port[0].to_string(), Some(port))
83    } else {
84        (authority.to_string(), None)
85    };
86
87    match scheme {
88        "http" => Ok((Protocol::Http, format!("http://{}", authority), None)),
89        "https" => Ok((Protocol::Http, format!("https://{}", authority), None)),
90        #[cfg(feature = "umicp")]
91        "umicp" => Ok((Protocol::Umicp, host, port)),
92        _ => Err(crate::error::VectorizerError::configuration(format!(
93            "Unsupported protocol: {}",
94            scheme
95        ))),
96    }
97}