use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
use crate::core::types::{ExchangeError, ExchangeResult};
pub struct GrpcClient {
channel: Channel,
}
impl GrpcClient {
pub async fn connect(url: &str) -> ExchangeResult<Self> {
let tls = ClientTlsConfig::new().with_native_roots();
let channel = Endpoint::from_shared(url.to_string())
.map_err(|e| ExchangeError::Network(format!("Invalid gRPC URL: {}", e)))?
.tls_config(tls)
.map_err(|e| ExchangeError::Network(format!("TLS config error: {}", e)))?
.connect()
.await
.map_err(|e| ExchangeError::Network(format!("gRPC connect failed: {}", e)))?;
Ok(Self { channel })
}
pub async fn connect_with_token(url: &str, _token: &str) -> ExchangeResult<Self> {
Self::connect(url).await
}
pub fn channel(&self) -> Channel {
self.channel.clone()
}
}