comparison utilities.py @ 0:35d52820e7c7 draft default tip

planemo upload for repository https://github.com/lu-brn/gmql-galaxy commit 953ee36ceda5814dc9baa03427bc0eb4ee2e93bd-dirty
author geco-team
date Tue, 26 Jun 2018 09:01:39 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:35d52820e7c7
1 # Helper functions to perform REST calls on the GMQL server.
2 # ----------------------------------------------------------------------------
3 # Luana Brancato, luana.brancato@mail.polimi.it
4 # ----------------------------------------------------------------------------
5
6 import sys
7 import os
8 import yaml
9 import requests
10
11
12 def load_parts(module, call) :
13 """Given the module and the single operation, returns the fragments for the url to call"""
14
15 y_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),'gmql_rest.yaml')
16
17 with open(y_path,'r') as yamlf :
18 cfg = yaml.load(yamlf)
19
20 parts = list ()
21
22 gmql = cfg['GMQL_URL']
23 prefix = cfg[module]['prefix']
24 op = cfg[module]['operations'][call]
25
26 parts.append(gmql)
27 if prefix :
28 parts.append(prefix)
29
30 for p in op :
31 parts.append(p)
32
33 return parts
34
35 def compose_url(module, call) :
36 """Given the fragments of a url, return the composite one"""
37
38 parts = load_parts(module,call)
39 url = '/'.join(parts)
40
41 return url
42
43 def add_url_param(params, module, op, value,) :
44 """Given the params dict, add a new pair of key:value with the given value and the key set for given module and operation"""
45
46 y_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'gmql_rest.yaml')
47
48 with open(y_path, 'r') as yamlf:
49 cfg = yaml.load(yamlf)
50 yamlf.close()
51
52 key = cfg[module]['params'][op]
53
54 params.update({key : value})
55
56 return params
57
58
59 def read_token(input):
60 """It takes the tabular file with the information over the user
61 name authToken valid_flag
62 It checks if the user is still valid and extract the authToken for the REST calls"""
63
64 with open(input,'r') as f_in :
65 user = f_in.readline().rstrip('\n').split('\t')
66
67 if user[2] :
68 token = user[1]
69 else :
70 stop_err("This session is no longer valid")
71
72
73 return token
74
75 def expire_user(input):
76 """Set the validity flag of a user token to false"""
77
78 with open(input,'r') as f:
79 user = f.readline().rstrip('\n').split('\t')
80
81 user[2] = False
82
83 with open(input,'w') as f :
84 f.write('{fullName}\t{token}\t{valid}\n'.format(fullName=user[0], token=user[1],
85 valid=user[2]))
86
87
88 def get(url, user=None, response_type='json') :
89 """GET Request
90 :param url: url where to fetch the requested resource
91 :param user: for authenticated requests; if not provided make an unauthenticated request (es. for login)
92 :param response_type: type of the fetched response.
93 JSON ( Default )
94 TEXT
95 ZIP
96 FILE
97 """
98
99 #Set request headers
100 headers = dict ()
101
102 if user :
103 headers.update({'X-AUTH-TOKEN' : read_token(user)})
104
105 if response_type == 'text' :
106 headers.update({'Accept' : 'text/plain'})
107 elif response_type == 'zip' :
108 pass
109 elif response_type == 'file' :
110 headers.update({'Accept' : 'file'})
111 else :
112 headers.update({'Accept' : 'application/json'})
113
114
115 #Make the request
116 response = requests.get(url, headers=headers)
117
118 #Check returned server status
119 status_code = response.status_code
120
121 #Read result. If Server OK, read according to response_type. Raise an error otherwise.
122 if status_code == requests.codes.ok :
123 if response_type == 'json' :
124 return response.json()
125 elif response_type == 'text' :
126 return response.text
127 else :
128 return response
129 elif status_code == requests.codes.unauthorized :
130 expire_user(user)
131 stop_err("You are not authorized to do this. \nPlease login first.")
132 elif status_code == requests.codes.not_found :
133 stop_err("Resource not found for this user.")
134 else :
135 stop_err("Error {code}: {reason}\n{message}".format(code=status_code,
136 reason=response.reason,
137 message=response.content))
138
139
140 def post(url, payload, user=None, params=None, content_type='json', response_type='json') :
141 """ POST Request
142 :param url: url where to post data
143 :param payload: payload for the post request. Type is specified by content_type.
144 :param user: for authenticated requests; if not provided make an unauthenticated request (es. for registration)
145 :param params: optional query parameters
146 :param content_type
147 :param response_type: Default is json
148 """
149
150
151 # Set request headers
152 headers = dict()
153
154 if user:
155 headers.update({'X-AUTH-TOKEN': read_token(user)})
156
157 headers.update({'Accept': 'application/json'})
158
159 if content_type == 'text' :
160 headers.update({'Content-Type' : 'text/plain'})
161 response = requests.post(url, params=params, headers=headers, data=payload)
162 elif content_type == 'multiform' :
163 response = requests.post(url, params=params, headers=headers, files=payload)
164 else :
165 headers.update({'Content-Type': 'application/json'})
166 response = requests.post(url, params=params, headers=headers, json=payload)
167
168 # Check returned server status
169 status_code = response.status_code
170
171
172 if status_code == requests.codes.ok :
173 return response.json()
174 elif status_code == requests.codes.unauthorized :
175 content = response.content
176 if content.__contains__("The username or password you entered don't match") :
177 stop_err("The username or password you entered don't match")
178 else:
179 expire_user(user)
180 stop_err("You are not authorized to do this. \nPlease login first.")
181 else :
182 stop_err("Error {code}: {reason}\n{message}".format(code=status_code,
183 reason=response.reason,
184 message=response.content))
185
186
187
188 def delete(url, user=None, response_type='json') :
189 """DELETE request
190 :param url: url where to post data
191 :param user: for authenticated requests; if not provided make an unauthenticated request (es. for registration)
192 :param response_type: Default is json
193 """
194
195 # Set request headers
196 headers = dict()
197
198 if user:
199 headers.update({'X-AUTH-TOKEN': read_token(user)})
200
201 headers.update({'Accept': 'application/json'})
202
203 #Make the request
204 response = requests.delete(url, headers=headers)
205
206 #Check returned server status
207 status_code = response.status_code
208
209
210 #If Server OK, read result. Raise an error otherwise.
211 if status_code == requests.codes.ok :
212 return response.json()
213 elif status_code == requests.codes.unauthorized :
214 expire_user(user)
215 stop_err("You are not authorized to do this. \nPlease login first.")
216 elif status_code == requests.codes.not_found :
217 stop_err("Resource not found for this user.")
218 else :
219 stop_err("Error {code}: {reason}".format(code=status_code,
220 reason=response.reason))
221
222
223
224 def stop_err(msg):
225 sys.stderr.write("%s\n" % msg)
226 sys.exit()