-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathErmrestHandler.py
More file actions
executable file
·81 lines (60 loc) · 3 KB
/
Copy pathErmrestHandler.py
File metadata and controls
executable file
·81 lines (60 loc) · 3 KB
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
import requests
import json
class ErmrestHandler(object):
#A class the interacts with ERMrest
#Allows you to create schemas,tables etc. and to store and pull data
#from the database for further use.
def __init__(self, host, username, password):
self.host = host
self._cookie = {"ermrest": self.get_cookie(username, password)}
def get_cookie(self, username, password):
r = requests.post('http://'+self.host+'/ermrest/authn/session',
data={'username': username,
'password': password})
r.raise_for_status()
return r.text[:-1]
def create_catalog(self):
#Returns the catalog id which is needed to fetch data and
#store data. Also needed to create new schemas and tables.
#Take note of it.
r = requests.post("http://"+self.host+"/ermrest/catalog",cookies=self._cookie)
r.raise_for_status()
return json.loads(r.text)
def create_schema(self,catalog_id, name):
r = requests.post("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+
"/schema/"+name,cookies=self._cookie)
r.raise_for_status()
def create_table(self,catalog_id,schema_name, table):
#Table item must be in the proper json format.
#Table must also be of python dict type
#For more information go to:
#https://github.com/informatics-isi-edu/ermrest/blob/master/api-doc/model/rest.md
r = requests.post("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+
"/schema/"+schema_name+"/table",json=table,cookies=self._cookie)
r.raise_for_status()
def delete_catalog(self,catalog_id):
r = requests.delete("http://"+self.host+"/ermrest/catalog/"+str(catalog_id),
cookies=self._cookie)
r.raise_for_status()
def delete_schema(self,catalog_id,schema_name):
r = requests.delete("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+
"/schema/"+schema_name,cookies=self._cookie)
r.raise_for_status()
def delete_table(self,catalog_id,schema_name,table_name):
r = requests.delete("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+
"/schema/"+schema_name+"/table/"+table_name,cookies=self._cookie)
r.raise_for_status()
def get_data(self, catalog_id, table_name, extra_info=""):
r = requests.get("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+"/entity/"+
table_name+extra_info, cookies=self._cookie)
return json.loads(r.text)
def delete_data(self, catalog_id, table_name, extra_info=""):
r = requests.delete("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+"/entity/"+
table_name+extra_info, cookies=self._cookie)
r.raise_for_status()
def put_data(self, catalog_id, table_name, data):
#data must be in a python dict type and in the correct table format.
data = [data]
r = requests.put("http://"+self.host+"/ermrest/catalog/"+str(catalog_id)+"/entity/"+table_name,
json=data, cookies=self._cookie)
r.raise_for_status()