-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathtls_example.py
executable file
·74 lines (61 loc) · 1.66 KB
/
tls_example.py
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
#!/usr/bin/env python3
import grpc
import pydgraph
def create_client(addr="localhost:9080"):
# Read certs
with open("./tls/ca.crt", "rb") as f:
root_ca_cert = f.read()
with open("./tls/client.user.key", "rb") as f:
client_cert_key = f.read()
with open("./tls/client.user.crt", "rb") as f:
client_cert = f.read()
# Connect to Dgraph via gRPC with mutual TLS.
creds = grpc.ssl_channel_credentials(
root_certificates=root_ca_cert,
private_key=client_cert_key,
certificate_chain=client_cert,
)
client_stub = pydgraph.DgraphClientStub(addr, credentials=creds)
return pydgraph.DgraphClient(client_stub)
def main():
client = create_client("localhost:9080")
client.login("groot", "password")
# Drop all
client.alter(pydgraph.Operation(drop_all=True))
# Update schema
schema = """
name: string @index(exact) .
description: string .
url: string .
"""
op = pydgraph.Operation(schema=schema)
client.alter(op)
# Mutate
dgraph = {
"name": "Dgraph",
"description": "Scalable, Distributed, Low Latency Graph Database",
"url": "https://dgraph.io",
}
txn = client.txn()
try:
txn.mutate(set_obj=dgraph)
txn.commit()
finally:
txn.discard()
# Query
res = client.txn(read_only=True).query(
"""
query dgraph($name: string) {
data(func: eq(name, $name)) {
uid
name
description
url
}
}
""",
variables={"$name": "Dgraph"},
)
print(res.json)
if __name__ == "__main__":
main()