-
Notifications
You must be signed in to change notification settings - Fork 0
/
verifier.py
279 lines (255 loc) · 13.3 KB
/
verifier.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
from classes import JSONSchemaValidator
import argparse
import requests
import json
from jsonschema import validate, RefResolver, Draft202012Validator
import os
from get_map import list_endpoints
parser = argparse.ArgumentParser()
parser.add_argument("-url", "--url")
args = parser.parse_args()
root_path = '/app/'
def list_endpoints(list_of_endpoints, endpoints):
for k, v in endpoints.items():
for k2, v2 in v.items():
if k2 == 'rootUrl':
list_of_endpoints.append(v2)
elif k2 == 'endpoints':
for k3, v3 in v2.items():
for k4, v4 in v3.items():
if k4 == 'url':
list_of_endpoints.append(v4)
return list_of_endpoints
def endpoint_check(endpoint:str, id_parameter: bool, url: str):
endpoint_validation=[]
is_error = False
root_path = '/app/'
if endpoint != 'genomicVariations' and id_parameter == False:
url = url + '/' + endpoint
f = requests.get(url)
total_response = json.loads(f.text)
elif id_parameter == False:
url = url + '/' + 'g_variants'
f = requests.get(url)
total_response = json.loads(f.text)
else:
last_part = endpoint.split('/')
url = url + '/' + last_part[-3]
try:
f = requests.get(url)
total_response = json.loads(f.text)
except Exception:
raise ValueError('{} is not a valid URL. Please review urls from /map endpoint'.format(url))
try:
error = total_response["error"]
is_error = True
except Exception:
try:
if last_part[-3] == 'g_variants':
id = total_response["response"]["resultSets"][0]["results"][0]["variantInternalId"]
url = endpoint.replace('{variantInternalId}', id)
elif last_part[-3] == 'cohorts':
id = total_response["response"]["collections"][0]["id"]
url = endpoint.replace('{id}', id)
elif last_part[-3] == 'datasets':
id = total_response["response"]["collections"][0]["id"]
url = endpoint.replace('{id}', id)
else:
id = total_response["response"]["resultSets"][0]["results"][0]["id"]
url = endpoint.replace('{id}', id)
except Exception:
pass
f = requests.get(url)
try:
total_response = json.loads(f.text)
except Exception as e:
endpoint_validation.append(e)
return endpoint_validation
endpoint = last_part[-1]
if endpoint == 'g_variants':
endpoint = 'genomicVariations'
endpoint_validation.append(url)
try:
meta = total_response["meta"]
granularity = meta["returnedGranularity"]
except Exception:
try:
meta = total_response["meta"]
granularity = meta["receivedRequestSummary"]["requestedGranularity"]
except Exception:
granularity = 'record'
if endpoint in ['cohorts', 'datasets']:
resultsets = total_response["response"]["collections"]
else:
try:
resultsets=total_response["response"]["resultSets"][0]["results"]
except Exception:
granularity = 'boolean'
if is_error == True:
with open(root_path+'ref_schemas/framework/json/responses/beaconErrorResponse.json', 'r') as f:
response = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconErrorResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, response)
endpoint_validation.append(JSONSchemaValidator.validate(total_response, response, resolver))
else:
if granularity == 'record':
if endpoint in ['cohorts', 'datasets']:
with open(root_path+'ref_schemas/framework/json/responses/beaconCollectionsResponse.json', 'r') as f:
response = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconCollectionsResponse.json').replace("\\", "/"))
else:
with open(root_path+'ref_schemas/framework/json/responses/beaconResultsetsResponse.json', 'r') as f:
response = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconResultsetsResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, response)
endpoint_validation.append(JSONSchemaValidator.validate(total_response, response, resolver))
with open(root_path+'ref_schemas/models/json/beacon-v2-default-model/' +endpoint+'/defaultSchema.json', 'r') as f:
response = json.load(f)
schema_path = 'file://{0}/'.format(
os.path.dirname(root_path+'ref_schemas/models/json/beacon-v2-default-model/'+endpoint+'/defaultSchema.json').replace("\\", "/"))
resolver = RefResolver(schema_path, response)
if endpoint in ['cohorts', 'datasets']:
resultsets=total_response["response"]["collections"]
for resultset in resultsets:
dataset = resultset["id"]
endpoint_validation.append(dataset)
endpoint_validation.append(JSONSchemaValidator.validate(resultset, response, resolver))
else:
resultsets=total_response["response"]["resultSets"]
for resultset in resultsets:
dataset = resultset["id"]
results = resultset["results"]
endpoint_validation.append(dataset)
#print(results[0])
for result in results:
#print(result)
endpoint_validation.append(JSONSchemaValidator.validate(result, response, resolver))
elif granularity == 'count':
with open(root_path+'ref_schemas/framework/json/responses/beaconCountResponse.json', 'r') as f:
response = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconCountResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, response)
endpoint_validation.append(JSONSchemaValidator.validate(total_response, response, resolver))
elif granularity == 'boolean':
with open(root_path+'ref_schemas/framework/json/responses/beaconBooleanResponse.json', 'r') as f:
response = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconBooleanResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, response)
endpoint_validation.append(JSONSchemaValidator.validate(total_response, response, resolver))
return endpoint_validation
def general_checks(url: str):
endpoint=None
output_validation=[]
root_path = '/app/'
new_url = url + '/map'
endpoint = '/map'
f = requests.get(new_url)
total_response = json.loads(f.text)
resultsets = total_response["response"]
endpoints = resultsets["endpointSets"]
list_of_endpoints=[]
endpoints_to_verify = list_endpoints(list_of_endpoints, endpoints)
new_url = url + '/map'
output_validation.append(new_url)
f = requests.get(new_url)
total_response = json.loads(f.text)
with open(root_path+'ref_schemas/framework/json/responses/beaconMapResponse.json', 'r') as f:
map = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconMapResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, map)
output_validation.append(JSONSchemaValidator.validate(total_response, map, resolver))
new_url = url + '/info'
output_validation.append(new_url)
f = requests.get(new_url)
total_response = json.loads(f.text)
with open(root_path+'ref_schemas/framework/json/responses/beaconInfoResponse.json', 'r') as f:
info = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconInfoResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, info)
output_validation.append(JSONSchemaValidator.validate(total_response, info, resolver))
new_url = url + '/configuration'
output_validation.append(new_url)
f = requests.get(new_url)
total_response = json.loads(f.text)
with open(root_path+'ref_schemas/framework/json/responses/beaconConfigurationResponse.json', 'r') as f:
configuration = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconConfigurationResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, configuration)
output_validation.append(JSONSchemaValidator.validate(total_response, configuration, resolver))
new_url = url + '/filtering_terms'
output_validation.append(new_url)
f = requests.get(new_url)
total_response = json.loads(f.text)
with open(root_path+'ref_schemas/framework/json/responses/beaconFilteringTermsResponse.json', 'r') as f:
filtering_terms = json.load(f)
schema_path = 'file:///{0}/'.format(
os.path.dirname(root_path+'ref_schemas/framework/json/responses/beaconFilteringTermsResponse.json').replace("\\", "/"))
resolver = RefResolver(schema_path, filtering_terms)
output_validation.append(JSONSchemaValidator.validate(total_response, filtering_terms, resolver))
for endpoint in endpoints_to_verify:
if endpoint.endswith('analyses') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('analyses', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('analyses'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('biosamples') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('biosamples', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('biosamples'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('g_variants') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('genomicVariations', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('g_variants'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('individuals') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('individuals', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('individuals'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('runs') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('runs', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('runs'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('datasets') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('datasets', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('datasets'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('cohorts') and 'd}' not in endpoint:
endpoint_validation=endpoint_check('cohorts', False, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
elif endpoint.endswith('cohorts'):
endpoint_validation=endpoint_check(endpoint, True, url)
for validated_endpoint in endpoint_validation:
output_validation.append(validated_endpoint)
return output_validation
general_checks(args.url)