-
Notifications
You must be signed in to change notification settings - Fork 0
/
oasis.py
64 lines (55 loc) · 2.23 KB
/
oasis.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
import json
import requests
import sys
import time
from pyArango.connection import Connection
from arango import ArangoClient
# retrieving credentials from ArangoDB tutorial service
def getTempCredentials(
tutorialName=None,
credentialProvider="https://tutorials.arangodb.cloud:8529/_db/_system/tutorialDB/tutorialDB",
):
with open("creds.dat", "r") as cacheFile:
contents = cacheFile.readline()
if contents:
try: # check if credentials are still valid
login = json.loads(contents)
url = "https://" + login["hostname"] + ":" + str(login["port"])
Connection(
arangoURL=url,
username=login["username"],
password=login["password"],
)
print("Reusing cached credentials.")
return login
except: # Incorrect data in cred file and retrieve new credentials
print("Invalid/expired expired.")
# Retrieve new credentials from Foxx Service
with open("creds.dat", "w+") as cacheFile:
print("Requesting new temp credentials.")
body = {"tutorialName": tutorialName} if tutorialName else "{}"
x = requests.post(credentialProvider, data=json.dumps(body))
if x.status_code != 200:
print("Error retrieving login data.")
sys.exit()
# Caching credentials
cacheFile.write(x.text)
cacheFile.close()
time.sleep(5) # Wait for DB provisioning
print("Temp database ready to use.")
return json.loads(x.text)
# Connect against an oasis DB and return pyarango connection
def connect(login: dict):
url = "https://" + login["hostname"] + ":" + str(login["port"])
return Connection(
arangoURL=url, username=login["username"], password=login["password"]
)
# Connect against an oasis DB and return python-arango connection
def connect_python_arango(login: dict):
url = "https://" + login["hostname"] + ":" + str(login["port"])
return ArangoClient(hosts=url).db(
login["dbName"],
username=login["username"],
password=login["password"],
verify=True,
)