vectorizer_sdk/
transport.rs1use async_trait::async_trait;
8use serde_json::Value;
9
10use crate::error::Result;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
14pub enum Protocol {
15 Http,
17 #[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#[async_trait]
34pub trait Transport: Send + Sync {
35 async fn get(&self, path: &str) -> Result<String>;
37
38 async fn post(&self, path: &str, data: Option<&Value>) -> Result<String>;
40
41 async fn put(&self, path: &str, data: Option<&Value>) -> Result<String>;
43
44 async fn delete(&self, path: &str) -> Result<String>;
46
47 fn protocol(&self) -> Protocol;
49}
50
51pub fn parse_connection_string(connection_string: &str) -> Result<(Protocol, String, Option<u16>)> {
58 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 #[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}