-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathoasis.py
62 lines (51 loc) · 2.01 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
import json
import requests
import sys
import time
from pyArango.connection import *
# retrieving credentials from ArangoDB tutorial service
def getTempCredentials():
with open("creds.dat","r+") as cacheFile:
contents = cacheFile.readline()
if len(contents) > 0:
login = None
url = ""
# check if credentials are still valid
try:
login = json.loads(contents)
url = "https://"+login["hostname"]+":"+str(login["port"])
except:
# Incorrect data in cred file and retrieve new credentials
cacheFile.truncate(0)
pass
conn =""
if (login is not None):
try:
conn = Connection(arangoURL=url, username=login["username"], password=login["password"],)
print("Reusing cached credentials.")
return login
except:
print("Credentials expired.")
pass # Ignore and retrieve new
# Retrieve new credentials from Foxx Service
print("Requesting new temp credentials.")
url = 'https://tutorials.arangodb.cloud:8529/_db/_system/tutorialDB/tutorialDB'
x = requests.post(url, data = "{}")
if x.status_code != 200:
print("Error retrieving login data.")
sys.exit()
# Caching credentials
cacheFile.truncate(0)
cacheFile.write(x.text)
print("Temp database ready to use.")
return json.loads(x.text)
# Connect against an oasis DB and return pyarango connection
def connect(login):
url = "https://"+login["hostname"]+":"+str(login["port"])
conn = None
try:
conn = Connection(arangoURL=url, username=login["username"], password=login["password"],)
except:
time.sleep(1)
conn = Connection(arangoURL=url, username=login["username"], password=login["password"],)
return conn