use tonic::transport::Channel;
use crate::error::GqlError;
use super::GqlSession;
use super::admin::AdminClient;
use super::catalog::CatalogClient;
use super::search::SearchClient;
#[derive(Debug, Clone)]
pub struct GqlConnection {
channel: Channel,
}
impl GqlConnection {
pub async fn connect(endpoint: &str) -> Result<Self, GqlError> {
let channel = Channel::from_shared(endpoint.to_owned())
.map_err(|e| GqlError::Protocol(e.to_string()))?
.connect()
.await?;
Ok(Self { channel })
}
#[must_use]
pub fn from_channel(channel: Channel) -> Self {
Self { channel }
}
pub async fn create_session(&self) -> Result<GqlSession, GqlError> {
GqlSession::new(self.channel.clone()).await
}
#[must_use]
pub fn create_catalog_client(&self) -> CatalogClient {
CatalogClient::new(self.channel.clone())
}
#[must_use]
pub fn create_admin_client(&self) -> AdminClient {
AdminClient::new(self.channel.clone())
}
#[must_use]
pub fn create_search_client(&self) -> SearchClient {
SearchClient::new(self.channel.clone())
}
#[cfg(feature = "tls")]
pub async fn connect_tls(
endpoint: &str,
tls_config: tonic::transport::ClientTlsConfig,
) -> Result<Self, GqlError> {
let channel = Channel::from_shared(endpoint.to_owned())
.map_err(|e| GqlError::Protocol(e.to_string()))?
.tls_config(tls_config)
.map_err(|e| GqlError::Protocol(e.to_string()))?
.connect()
.await?;
Ok(Self { channel })
}
#[must_use]
pub fn channel(&self) -> &Channel {
&self.channel
}
}