use std::convert::TryInto;
use anyhow::Result;
use async_trait::async_trait;
use http::Uri;
use std::fmt::Debug;
use crate::client::lazy::LazyClient;
use crate::client::tls::LazyTlsChannel;
#[cfg(feature = "acl")]
use crate::client::AclClientType;
use crate::client::{IClient as IAsyncClient, TlsClient as AsyncTlsClient};
use crate::sync::client::{ClientState, ClientVariant, IClient};
use crate::sync::txn::{TxnBestEffortType, TxnMutatedType, TxnReadOnlyType, TxnType as SyncTxn};
use crate::txn::TxnType;
use crate::{EndpointConfig, Endpoints};
#[derive(Debug)]
#[doc(hidden)]
pub struct Tls {
async_client: AsyncTlsClient,
}
#[async_trait]
impl IClient for Tls {
type AsyncClient = AsyncTlsClient;
type Client = LazyClient<Self::Channel>;
type Channel = LazyTlsChannel;
fn client(&self) -> Self::Client {
self.async_client.extra.client()
}
fn clients(self) -> Vec<Self::Client> {
self.async_client.extra.clients()
}
fn async_client_ref(&self) -> &Self::AsyncClient {
&self.async_client
}
fn async_client(self) -> Self::AsyncClient {
self.async_client
}
fn new_txn(&self) -> TxnType<Self::Client> {
self.async_client_ref().new_txn()
}
#[cfg(feature = "acl")]
async fn login<T: Into<String> + Send + Sync>(
self,
user_id: T,
password: T,
) -> Result<AclClientType<Self::Channel>> {
self.async_client.login(user_id, password).await
}
#[cfg(all(feature = "acl", feature = "dgraph-21-03"))]
async fn login_into_namespace<T: Into<String> + Send + Sync>(
self,
user_id: T,
password: T,
namespace: u64,
) -> Result<AclClientType<Self::Channel>> {
self.async_client
.login_into_namespace(user_id, password, namespace)
.await
}
}
pub type TlsClient = ClientVariant<Tls>;
pub type TxnTls = SyncTxn<LazyClient<LazyTlsChannel>>;
pub type TxnTlsReadOnly = TxnReadOnlyType<LazyClient<LazyTlsChannel>>;
pub type TxnTlsBestEffort = TxnBestEffortType<LazyClient<LazyTlsChannel>>;
pub type TxnTlsMutated = TxnMutatedType<LazyClient<LazyTlsChannel>>;
impl TlsClient {
pub fn new<S: TryInto<Uri>, E: Into<Endpoints<S>>, V: Into<Vec<u8>>>(
endpoints: E,
server_root_ca_cert: V,
client_cert: V,
client_key: V,
) -> Result<Self> {
let extra = Tls {
async_client: AsyncTlsClient::new(
endpoints,
server_root_ca_cert,
client_cert,
client_key,
)?,
};
let state = Box::new(ClientState::new());
Ok(Self { state, extra })
}
pub fn new_with_endpoint_config<
S: TryInto<Uri>,
E: Into<Endpoints<S>>,
V: Into<Vec<u8>>,
C: EndpointConfig + 'static,
>(
endpoints: E,
server_root_ca_cert: V,
client_cert: V,
client_key: V,
endpoint_config: C,
) -> Result<Self> {
let extra = Tls {
async_client: AsyncTlsClient::new_with_endpoint_config(
endpoints,
server_root_ca_cert,
client_cert,
client_key,
endpoint_config,
)?,
};
let state = Box::new(ClientState::new());
Ok(Self { state, extra })
}
}