use std::convert::TryInto;
use anyhow::Result;
use async_trait::async_trait;
use http::Uri;
use std::fmt::Debug;
use crate::client::lazy::LazyClient;
#[cfg(feature = "acl")]
use crate::client::AclClientType;
use crate::client::{Client as AsyncClient, IClient as IAsyncClient, LazyChannel};
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 Default {
async_client: AsyncClient,
}
#[async_trait]
impl IClient for Default {
type AsyncClient = AsyncClient;
type Client = LazyClient<Self::Channel>;
type Channel = LazyChannel;
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 Client = ClientVariant<Default>;
pub type Txn = SyncTxn<LazyClient<LazyChannel>>;
pub type TxnReadOnly = TxnReadOnlyType<LazyClient<LazyChannel>>;
pub type TxnBestEffort = TxnBestEffortType<LazyClient<LazyChannel>>;
pub type TxnMutated = TxnMutatedType<LazyClient<LazyChannel>>;
impl Client {
pub fn new<S: TryInto<Uri>, E: Into<Endpoints<S>> + Debug>(endpoints: E) -> Result<Self> {
let extra = Default {
async_client: AsyncClient::new(endpoints)?,
};
let state = Box::new(ClientState::new());
Ok(Self { state, extra })
}
pub fn new_with_endpoint_config<
S: TryInto<Uri>,
E: Into<Endpoints<S>> + Debug,
C: EndpointConfig + 'static,
>(
endpoints: E,
endpoint_config: C,
) -> Result<Self> {
let extra = Default {
async_client: AsyncClient::new_with_endpoint_config(endpoints, endpoint_config)?,
};
let state = Box::new(ClientState::new());
Ok(Self { state, extra })
}
}