forked from bioinformatics-ua/montra-client-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
montra.py
240 lines (181 loc) · 8.3 KB
/
montra.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
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
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__title__ = 'montra-client-python'
__version__ = '1.0'
__author__ = 'João Rafael Almeida'
__license__ = 'GPL v3'
__copyright__ = 'Copyright 2019, João Rafael Almeida, Universidade de Aveiro'
__url__ = 'https://github.com/bioinformatics-ua/montra-client-python'
__maintainer__ = 'João Rafael Almeida'
__email__ = 'joao.rafael.almeida@ua.pt'
__all__ = ()
import requests
import sys
import traceback
from requests.auth import HTTPDigestAuth
URL = 'https://bioinformatics.ua.pt/ehden'
COMMSlug = "ehden"
COMMName = "ehden"
QUESSlug = "ehden"
ERROR_MESSAGES = {
"bad_arg": "The argments passed in the constructor are invalid! Please provide a token or the basic credentials using the parameters 'username' and 'password'",
"bad_auth_type": "Authentication type not valid. Authentication type should be set as 'basic' or 'token' ",
"bad_auth_params": ("Authentication parameters are missing! Please provide valid credentials."
"You can use either an username and password for a basic authentication or a valid API token using token authentication"),
"bad_get_request_generic": "Something wrong with the request!"
}
class Montra:
def __init__(self, url=URL, **args):
if "token" in args:
self.token = args["token"]
self.auth_type = 'token'
elif "username" in args and "password" in args:
self.username = args["username"]
self.password = args["password"]
self.auth_type = 'basic'
else:
raise ValueError(ERROR_MESSAGES["bad_arg"])
self.ENDPOINT = url
def search_datasets(self, questionnaire=QUESSlug):
"""
Search for the questionnaires in all communities
return: the list of questionnaires
"""
url = self.ENDPOINT + "/api/questionnaires/?search=" + str(questionnaire)
return self.__get_request(url=url)
def get_dataset(self, communityName=COMMName, questionnaireSlug=QUESSlug):
"""
Gets the dataset by the slug, which is the identifier
return: json with the dataset (questionnaire)
"""
communityInfo = self.__get_community_info(communityName=communityName)
try:
qSlug = [ele for ele in communityInfo["questionnaires"] if ele['slug'] == questionnaireSlug][0]
url = self.ENDPOINT + "/api/questionnaires/" + str(qSlug['slug'])
return self.__get_request(url=url)
except IndexError as err:
traceback.print_exc(file=sys.stdout)
print err
return None
def __get_community_info(self, communityName):
"""
Gets the community info by the name
return: json with all the information (community, questionnaires and fingerprints hashs)
"""
url = self.ENDPOINT + "/api/communities/" + str(communityName)
return self.__get_request(url=url)
def get_database(self, **args):
if(len(args) == 1):
return self.__get_database_by_hash(args["fingerprintHash"])
else:
try:
communityInfo = self.__get_community_info(communityName=args["communityName"])
comm_slug = communityInfo['slug']
return self.__get_database_by_database_name(args["database_name"], comm_slug)
except IndexError as err:
traceback.print_exc(file=sys.stdout)
print err
return None
def __get_database_by_hash(self, fingerprintHash):
"""
Gets the fingerprint by the fingprint hash
return: json with the fingerprint
"""
url = self.ENDPOINT + "/api/fingerprints/" + str(fingerprintHash)
return self.__get_request(url=url)
def __get_database_by_database_name(self, database_name, communitySlug=COMMSlug):
"""
Gets the fingerprint by the fingprint name and the questionnaire slug
return: json with the fingerprint
"""
url = self.ENDPOINT + "/api/fingerprint-cslug-fslug/" + str(communitySlug) + "/" + str(database_name) + "/"
return self.__get_request(url=url)
def list_answer(self, fingerprintHash):
"""
Gets the list of available questions to get or update data of the fingerprint hash
return: json with the fingerprint available questions (some types not available in the API)
"""
url = self.ENDPOINT + "/api/fingerprints/" + str(fingerprintHash) + "/answers"
return self.__get_request(url=url)
def get_answer(self, fingerprintHash, question):
"""
Gets the the question of the fingerprint hash
return: json with question and answer
"""
url = self.ENDPOINT + "/api/fingerprints/" + str(fingerprintHash) + "/answers/" + str(question)
return self.__get_request(url=url)
def put_answer(self, fingerprintHash, question, newAnswer):
"""
Post a new answer in the question of the fingerprint hash
return: json with question and answer
"""
url = self.ENDPOINT + "/api/fingerprints/" + str(fingerprintHash) + "/answers/" + str(question) + "/"
return self.__put_request(url=url, data={"data":newAnswer})
def new_database(self, database_name, communityName=COMMName, questionnaireSlug=QUESSlug, description=""):
"""
Post a new database (fingerprint) in the community
return: json with fingerprint informantion, inclusive the hash
"""
communityInfo = self.__get_community_info(communityName=communityName)
try:
qSlug = [ele for ele in communityInfo["questionnaires"] if ele['slug'] == questionnaireSlug][0]
qSlug['id']
url = self.ENDPOINT + "/api/fingerprints/"
return self.__post_request(url=url, data={
"questionnaire":qSlug['id'],
"description": description,
"database_name": database_name,
"draft": True,
"community": communityInfo["id"]})
except IndexError as err:
traceback.print_exc(file=sys.stdout)
print err
return None
def update_database(self, fingerprintHash, draft=True, description=""):
"""
Post a new database (fingerprint) in the community
return: json with fingerprint informantion, inclusive the hash
"""
url = self.ENDPOINT + "/api/fingerprints/" + fingerprintHash + "/"
return self.__put_request(url=url, data={
"description": description,
"draft": draft
})
def __get_request(self, url):
try:
if self.auth_type == 'basic':
response = requests.get(url, auth=(self.username, self.password))
elif self.auth_type == 'token':
response = requests.get(url, headers={'Authorization': 'Token ' + self.token})
else:
raise ValueError(ERROR_MESSAGES["bad_auth_type"])
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as err:
traceback.print_exc(file=sys.stdout)
print err
return None
def __post_request(self, url, data):
try:
if self.auth_type == 'basic':
response = requests.post(url, auth=(self.username, self.password), data=data)
elif self.auth_type == 'token':
response = requests.post(url, headers={'Authorization': 'Token ' + self.token}, data=data)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as err:
traceback.print_exc(file=sys.stdout)
print err
return None
def __put_request(self, url, data):
try:
if self.auth_type == 'basic':
response = requests.put(url, auth=(self.username, self.password), data=data)
elif self.auth_type == 'token':
response = requests.put(url, headers={'Authorization': 'Token ' + self.token}, data=data)
response.raise_for_status()
return response.json()
except requests.exceptions.HTTPError as err:
traceback.print_exc(file=sys.stdout)
print err
return None