1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
use std::convert::TryInto;
use std::path::Path;
use failure::Error;
use http::Uri;
use rand::Rng;
use tonic::transport::{Certificate, ClientTlsConfig, Endpoint, Identity};
use tonic::Status;
use crate::errors::ClientError;
use crate::stub::Stub;
use crate::{
BestEffortTxn, DgraphClient, IDgraphClient, MutatedTxn, Operation, Payload, ReadOnlyTxn,
Result, Txn,
};
#[derive(Clone, Debug)]
pub struct Client {
stubs: Vec<Stub>,
}
impl Client {
fn balance_list<S: TryInto<Uri>>(endpoints: Vec<S>) -> Result<Vec<Uri>, Error> {
let mut balance_list: Vec<Uri> = Vec::new();
for maybe_endpoint in endpoints {
let endpoint = match maybe_endpoint.try_into() {
Ok(endpoint) => endpoint,
Err(_err) => {
return Err(ClientError::InvalidEndpoint.into());
}
};
balance_list.push(endpoint);
}
if balance_list.is_empty() {
return Err(ClientError::NoEndpointsDefined.into());
};
Ok(balance_list)
}
fn any_client(&self) -> Stub {
let mut rng = rand::thread_rng();
let i = rng.gen_range(0, self.stubs.len());
if let Some(client) = self.stubs.get(i) {
client.clone()
} else {
unreachable!()
}
}
fn make(stubs: Vec<Stub>) -> Self {
Self { stubs }
}
pub async fn new<S: TryInto<Uri>>(endpoints: Vec<S>) -> Result<Self, Error> {
let balance_list = Self::balance_list(endpoints)?;
let mut stubs = Vec::with_capacity(balance_list.len());
for uri in balance_list {
let endpoint: Endpoint = uri.into();
let channel = endpoint.connect().await?;
stubs.push(Stub::new(DgraphClient::new(channel)));
}
Ok(Self::make(stubs))
}
pub async fn new_with_tls_client_auth<S: TryInto<Uri>>(
endpoints: Vec<S>,
server_root_ca_cert: impl AsRef<Path>,
client_cert: impl AsRef<Path>,
client_key: impl AsRef<Path>,
) -> Result<Self, Error> {
let server_root_ca_cert_future = tokio::fs::read(server_root_ca_cert);
let client_cert_future = tokio::fs::read(client_cert);
let client_key_future = tokio::fs::read(client_key);
let server_root_ca_cert = Certificate::from_pem(server_root_ca_cert_future.await?);
let client_identity =
Identity::from_pem(client_cert_future.await?, client_key_future.await?);
let tls = ClientTlsConfig::new()
.ca_certificate(server_root_ca_cert)
.identity(client_identity);
let balance_list: Vec<Uri> = Self::balance_list(endpoints)?;
let mut stubs = Vec::with_capacity(balance_list.len());
for uri in balance_list {
let tls = tls
.clone()
.domain_name(uri.host().expect("host in endpoint"));
let endpoint: Endpoint = uri.into();
let channel = endpoint.tls_config(tls.clone()).connect().await?;
stubs.push(Stub::new(DgraphClient::new(channel)));
}
Ok(Self::make(stubs))
}
pub fn new_txn(&self) -> Txn {
Txn::new(self.any_client())
}
pub fn new_read_only_txn(&self) -> ReadOnlyTxn {
self.new_txn().read_only()
}
pub fn new_best_effort_txn(&self) -> BestEffortTxn {
self.new_read_only_txn().best_effort()
}
pub fn new_mutated_txn(&self) -> MutatedTxn {
self.new_txn().mutated()
}
pub async fn alter(&self, op: Operation) -> Result<Payload, Status> {
let mut stub = self.any_client();
stub.alter(op).await
}
}
#[cfg(test)]
mod tests {
use crate::Client;
use super::*;
#[tokio::test]
async fn alter() {
let client = Client::new(vec!["http://127.0.0.1:19080"]).await.unwrap();
let op = Operation {
schema: "name: string @index(exact) .".into(),
..Default::default()
};
let response = client.alter(op).await;
assert!(response.is_ok());
}
}