-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
413 lines (378 loc) · 14.7 KB
/
main.py
File metadata and controls
413 lines (378 loc) · 14.7 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from fastapi import FastAPI, Depends, Header, Response, status
from fastapi.responses import JSONResponse
from auth.auth_handler import JWTBearer
from models.request_fields import HelloResponse, \
FullDepartmentRequest, \
FullDepartmentResponse, SPHRResponse, \
StaffMemberDepartmentResponse, \
SingleHospitalTagsRequest, \
SingleHospitalTagsResponse, \
MultiHospitalTagsRequest, \
MultiHospitalTagsResponse, \
NotAuthenticated, \
HandleError500, \
UnauthorizedResponse, \
AddUserRequest, \
AddUserSuccessResponse, \
RemoveUserRequest, \
RemoveUserSuccessResponse, \
MLSuccessResponse, \
SearchRequest, \
SearchResponse, \
SPHRRequest, \
SPHRRequestEncrypted, \
SPHRResponseEncrypted, \
DVResponse
from components.staff.departments import get_departments
from components.staff.verify_staff_member import get_department_of_staff_member
from components.tags.tags import get_tags
from components.users.add_or_remove_users import add_user, remove_user
from components.ml.data_for_ml import get_patient_data_for_ml
from components.search.search import search_for_serums_id
from components.sphr.get_source_data import get_patient_data, parse_sphr
from components.encryption.encryption import encrypt_data_with_new_key, \
encrypt_key
from components.data_vaults.data_vault import create_data_vault
from components.data_vaults.satellites import process_satellites
from components.data_vaults.hub_post_processing import hub_equalizer
from components.data_vaults.link_post_processing import add_id_values
from components.jwt.validate import validate_jwt
responses = {
401: {"model": UnauthorizedResponse},
403: {"model": NotAuthenticated},
500: {"model": HandleError500}
}
description = """
This is an updated version of the Serums Datalake API. \
While the functionality remains the same, every single \
function has been evaluated and rewritten where needed, \
hopefully leading to a smoother experience when using \
as well as an easier time maintaining.
A big part of the change comes from the far more robust \
testing suite which now covers everything so finding issues \
in the future should be a much simpler task.
## Usage
You will require to have a few things in place to use the API. \
First, you must be able to log into the [Flexpass system]\
(https://flexpass.serums.cs.st-andrews.ac.uk/web_app/login_username.html).\
Additionally, if you are testing staff members you will need to have \
rules stored on the blockchain for that member of staff. For instance, \
In the testing suite, you will see that there are various rules in \
place for the patient with the id of 215 and the medical professional \
with an id of 216 within USTAN.
"""
contact = {
"name": "Euan Blackledge",
"email": "euan.blackledge@soprasteria.com",
"url": "https://github.com/SkinnyPigeon"
}
tags_metadata = [
{
"name": "HELLO",
"description": "A place to check the server is on"
},
{
"name": "STAFF",
"description": "Access various elements of the staff databases"
},
{
"name": "TAGS",
"description": "Access various elements of the tags tables"
},
{
"name": "USERS",
"description": "Add or remove users from the Serums network"
},
{
"name": "MACHINE LEARNING",
"description": "Return the patient data for the machine learning "
"algorithm"
},
{
"name": "SEARCH",
"description": "Search for a patient’s SERUMS ID"
},
{
"name": "SMART PATIENT HEALTH RECORD",
"description": "Retrieve the Smart Patient Health Record"
},
{
"name": "DATA VAULT",
"description": "Returns the patient data in data vault format"
}
]
app = FastAPI(
title="Serums Datalake API",
description=description,
contact=contact,
openapi_tags=tags_metadata
)
@app.get("/hello/hello", response_model=HelloResponse, tags=['HELLO'])
def say_hello():
return {"hello": "Welcome to the API. The server is on"}
@app.post("/staff_tables/departments",
tags=['STAFF'],
response_model=FullDepartmentResponse)
def request_get_departments(body: FullDepartmentRequest):
departments = get_departments(body.hospital_id)
return departments
@app.get("/staff_tables/get_department_of_staff_member",
tags=['STAFF'],
response_model=StaffMemberDepartmentResponse,
dependencies=[Depends(JWTBearer())])
def request_get_dpt_of_staff_member(Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
response = get_department_of_staff_member(
jwt_response['hospital_id'],
jwt_response['serums_id']
)
return response
@app.post('/tags_tables/tags',
tags=['TAGS'],
response_model=SingleHospitalTagsResponse)
def request_get_single_hospital_tags(body: SingleHospitalTagsRequest):
tags = get_tags(body.hospital_id)
return tags
@app.post('/tags_tables/all_tags',
tags=['TAGS'],
response_model=MultiHospitalTagsResponse)
def request_get_multi_hospital_tags(body: MultiHospitalTagsRequest):
tags = {}
for hospital_id in body.hospital_ids:
tags[hospital_id] = get_tags(hospital_id)
return tags
@app.post('/users/add_user',
tags=['USERS'],
response_model=AddUserSuccessResponse,
responses=responses,
dependencies=[Depends(JWTBearer())])
def request_add_user(body: AddUserRequest,
response: Response,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'SERUMS_ADMIN' in jwt_response['user_type'] \
or 'HOSPITAL_ADMIN' in jwt_response['user_type']:
result = add_user(body.serums_id,
body.patient_id,
body.hospital_id)
if result[1] == 200:
return result[0]
else:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return result[0]
else:
return JSONResponse(status_code=401, content={
"message": "Only admins can add users"
})
@app.post('/users/remove_user',
tags=['USERS'],
response_model=RemoveUserSuccessResponse,
responses=responses,
dependencies=[Depends(JWTBearer())])
def request_remove_user(body: RemoveUserRequest,
response: Response,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'SERUMS_ADMIN' in jwt_response['user_type'] \
or 'HOSPITAL_ADMIN' in jwt_response['user_type']:
results = remove_user(body.serums_id,
body.hospital_ids)
if results[1] == 200:
return results[0]
else:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return results[0]
else:
return JSONResponse(status_code=401, content={
"message": "Only admins can remove users"
})
@app.get('/machine_learning/analytics',
tags=['MACHINE LEARNING'],
response_model=MLSuccessResponse,
responses=responses,
dependencies=[Depends(JWTBearer())])
def get_ml_data(response: Response, Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'PATIENT' in jwt_response['user_type']:
results = get_patient_data_for_ml(jwt_response['serums_id'])
if results[1] == 200:
return results[0]
else:
response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
return results[0]
else:
return JSONResponse(status_code=401, content={
"message": "Only patients can access their own ML data"
})
@app.post('/search/serums_id',
tags=['SEARCH'],
response_model=SearchResponse,
responses=responses,
dependencies=[Depends(JWTBearer())])
def get_search_for_serums_id(body: SearchRequest,
response: Response,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'MEDICAL_STAFF' in jwt_response['user_type'] or \
'HOSPITAL_ADMIN' in jwt_response['user_type'] or \
'SERUMS_ADMIN' in jwt_response['user_type']:
results = search_for_serums_id(dict(body))
if results[1] == 200:
return results[0]
else:
return JSONResponse(status_code=500, content={
"message": "Unable to find a patient with those details"
})
else:
return JSONResponse(status_code=401, content={
"message": "Must be either a medical staff or "
"admin to search for users"
})
@app.post('/smart_patient_health_record/get_sphr',
response_model=SPHRResponse,
responses=responses,
tags=['SMART PATIENT HEALTH RECORD'],
dependencies=[Depends(JWTBearer())])
def get_the_unencrypted_sphr(body: SPHRRequest,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'PATIENT' in jwt_response['user_type'] and \
body.serums_id != jwt_response['serums_id']:
return JSONResponse(status_code=401, content={
"message": "Patients can only access their own records, "
"please check the serums id in request body"
})
patient_data, proof_id = get_patient_data(
body.serums_id,
body.hospital_ids,
body.tags,
Authorization
)
if patient_data:
parsed_data = parse_sphr(patient_data)
return parsed_data
return JSONResponse(status_code=500, content={
"message": "Unable to create SPHR"
})
@app.post('/smart_patient_health_record/encrypted',
response_model=SPHRResponseEncrypted,
responses=responses,
tags=['SMART PATIENT HEALTH RECORD'],
dependencies=[Depends(JWTBearer())])
def get_the_encrypted_sphr(body: SPHRRequestEncrypted,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'PATIENT' in jwt_response['user_type'] and \
body.serums_id != jwt_response['serums_id']:
return JSONResponse(status_code=401, content={
"message": "Patients can only access their own records, "
"please check the serums id in request body"
})
patient_data, proof_id = get_patient_data(
body.serums_id,
body.hospital_ids,
body.tags,
Authorization
)
if patient_data:
parsed_data = parse_sphr(patient_data)
encrypted_data, encryption_key, public_key = \
encrypt_data_with_new_key(parsed_data, body.public_key)
encrypted_key = encrypt_key(encryption_key, public_key)
return {"data": encrypted_data, "key": encrypted_key}
return JSONResponse(status_code=500, content={
"message": "Unable to create SPHR"
})
@app.post('/data_vault/data_vault',
response_model=DVResponse,
responses=responses,
tags=['DATA VAULT'],
dependencies=[Depends(JWTBearer())])
def get_the_unencrypted_data_vault(body: SPHRRequest,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'PATIENT' in jwt_response['user_type'] and \
body.serums_id != jwt_response['serums_id']:
return JSONResponse(status_code=401, content={
"message": "Patients can only access their own records, "
"please check the serums id in request body"
})
patient_data, proof_id = get_patient_data(
body.serums_id,
body.hospital_ids,
body.tags,
Authorization
)
if patient_data:
satellites = process_satellites(patient_data)
data_vault = create_data_vault(satellites)
add_id_values(data_vault['links'])
hub_equalizer(data_vault['hubs'])
return data_vault
return JSONResponse(status_code=500, content={
"message": "Unable to create SPHR"
})
@app.post('/data_vault/encrypted',
response_model=SPHRResponseEncrypted,
responses=responses,
tags=['DATA VAULT'],
dependencies=[Depends(JWTBearer())])
def get_the_encrypted_data_vault(body: SPHRRequestEncrypted,
Authorization: str = Header(None)):
jwt_response = validate_jwt(Authorization)
if jwt_response['status_code'] != 200:
return JSONResponse(status_code=403, content={
"message": "Not authenticated"
})
if 'PATIENT' in jwt_response['user_type'] and \
body.serums_id != jwt_response['serums_id']:
return JSONResponse(status_code=401, content={
"message": "Patients can only access their own records, "
"please check the serums id in request body"
})
patient_data, proof_id = get_patient_data(
body.serums_id,
body.hospital_ids,
body.tags,
Authorization
)
if patient_data:
satellites = process_satellites(patient_data)
data_vault = create_data_vault(satellites)
add_id_values(data_vault['links'])
hub_equalizer(data_vault['hubs'])
encrypted_data, encryption_key, public_key = \
encrypt_data_with_new_key(data_vault, body.public_key)
encrypted_key = encrypt_key(encryption_key, public_key)
return {"data": encrypted_data, "key": encrypted_key}
return JSONResponse(status_code=500, content={
"message": "Unable to create SPHR"
})