-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.py
553 lines (401 loc) · 14.7 KB
/
handler.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import json
from bson import json_util
from functions import slr
from functions.db import connector
# https://docs.aws.amazon.com/lambda/latest/dg/python-handler.html
# def sample_handler(event, *args):
# try:
# body = json.loads(event["body"])
# # these have to be defined in serverless.yml
# query_string = event.get('queryStringParameters').get('my_query_string')
# path_param = event.get('pathParameters').get('my_path_param')
# resp_body = {
# "response": "..."
# }
# return make_response(status_code=200, body=resp_body)
# except Exception as e:
# return make_response(status_code=500, body={"error": error})
def make_response(status_code: int, body: dict):
"""Makes response dict
Args:
status_code: http status code
body: json serializable http response body
Returns:
lambda response dict
"""
return {
"statusCode": status_code,
"headers": {
'Access-Control-Allow-Headers': 'Content-Type, authorizationToken',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
},
"body": json.dumps(body, default=json_util.default)
}
def add_collaborator_to_review(event, *args):
"""Handles requests to add collaborators to a review
Args:
url: review/{review_id}/collaborator?username
Returns:
updated review
"""
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
username = event.get('queryStringParameters').get('username')
user = connector.get_user_by_username(username)
updated_result = connector.add_collaborator_to_review(review, user)
resp_body = {
"updated_result": updated_result.to_son().to_dict()
}
return make_response(status_code=201, body=resp_body)
def get_reviews_for_user(event, *args):
"""Handles requests to get all reviews a user is part of
Args:
url: users/{username}/reviews
Returns:
list of reviews
"""
username = event.get('pathParameters').get('username')
user = connector.get_user_by_username(username)
reviews = connector.get_reviews(user)
resp_body = {
"reviews": reviews
}
return make_response(status_code=201, body=resp_body)
def dry_query(event, *args):
"""Handles running a dry query
Args:
url: dry_query?page&page_length&review_id
body:
search: search dict <wrapper/input_format.py>
Returns:
{
<wrapper/output_format.py>
}
"""
# try:
body = json.loads(event["body"])
search = body.get('search')
try:
page = int(event.get('queryStringParameters').get('page', 1))
except AttributeError:
page = 1
try:
page_length = int(
event.get('queryStringParameters').get('page_length', 50))
except AttributeError:
page_length = 50
results = slr.conduct_query(search, page, page_length)
# (optionally) mark previously persisted results
try:
review_id = event.get('queryStringParameters').get('review_id')
review = connector.get_review_by_id(review_id)
results = slr.results_persisted_in_db(results, review)
except AttributeError:
pass
return make_response(status_code=201, body=results)
# except Exception as e:
# return make_response(status_code=500, body={"error": e})
def new_query(event, *args):
"""Add new query session <kind of deprecated>
Args:
url: review/{review_id}/query
body:
"search" <search dict (wrapper/input_format.py)>
Returns:
{
"review": review object,
"new_query_id": new_query_id
}
"""
# try:
body = json.loads(event["body"])
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
search = body.get('search')
new_query = connector.new_query(review, search)
resp_body = {
"review": review.to_son().to_dict(),
"new_query_id": new_query._id
}
return make_response(status_code=201, body=resp_body)
# except Exception as e:
# return make_response(status_code=500, body={"error": str(e)})
def get_persisted_results(event, *args):
"""Handles getting persisted results
Args:
url: results/{review_id}?page=1&page_length=50&query_id
Returns:
{
"results": [{<result from mongodb>}],
"query_id": <query_id>
}
"""
# try:
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
try:
page = int(event.get('queryStringParameters').get('page', 1))
except AttributeError:
page = 1
try:
page_length = int(
event.get('queryStringParameters').get('page_length', 50))
except AttributeError:
page_length = 50
try:
query_id = event.get('queryStringParameters').get('query_id')
except AttributeError:
query_id = None
if query_id != None:
obj = connector.get_query_by_id(review, query_id)
else:
obj = review
# this works for either query or reviews. use whatever is given to us
results = connector.get_persisted_results(obj, page, page_length)
return make_response(status_code=200, body=results)
# except Exception as e:
# return make_response(status_code=500, body={"error": str(e)})
def persist_pages_of_query(event, *args):
"""Handles persisting a range of pages of a dry query.
Args:
url: persist/{review_id}
body:
pages: [1, 3, 4, 6] list of pages
page_length: int
search <search dict (wrapper/input_format.py)>
Returns:
{
"success": True,
"num_persisted": num_persisted,
"query_id": query.pk
}
"""
# try:
body = json.loads(event["body"])
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
search = body.get('search')
query = connector.new_query(review, search)
pages = body.get('pages')
page_length = body.get('page_length')
num_persisted = 0
for page in pages:
results = slr.conduct_query(search, page, page_length)
for wrapper_results in results:
connector.save_results(
wrapper_results.get('records'), review, query)
num_persisted += len(wrapper_results.get('records'))
resp_body = {
"success": True,
"num_persisted": num_persisted,
"query_id": query._id
}
return make_response(status_code=200, body=resp_body)
# except Exception as e:
# return make_response(status_code=500, body={"error": str(e)})
def persist_list_of_results(event, *args):
"""Handles persisting results
Args:
url: persist/{review_id}/list
body:
results: [{<result>}, {...}]
search <search dict (wrapper/input_format.py)>
Returns:
{
"success": True
}
"""
# try:
body = json.loads(event["body"])
results = body.get('results')
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
search = body.get('search')
query = connector.new_query(review, search)
connector.save_results(results, review, query)
resp_body = {
"success": True,
"query_id": query._id
}
return make_response(status_code=201, body=resp_body)
# except Exception as e:
# return make_response(status_code=500, body={"error": str(e)})
def delete_results_by_dois(event, body):
"""Handles deleting a list of results
Args:
url: results/{review_id}
body:
dois: list of dois
Returns:
{
"success": True
}
"""
# try:
body = json.loads(event["body"])
dois = body.get('dois')
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
connector.delete_results_by_dois(review, dois)
resp_body = {
"success": True
}
return make_response(status_code=200, body=resp_body)
# except Exception as e:
# return make_response(status_code=500, body={"error": str(e)})
def add_review(event, *args):
"""POST Method: create a new review
"name" is mandatory in body
"""
from functions.db.connector import add_review
body = json.loads(event["body"])
owner_name = body.get('owner_name')
owner = connector.get_user_by_username(owner_name)
name = body.get('name')
description = body.get('description')
review = add_review(name, description, owner=owner)
return make_response(201, review.to_son().to_dict())
def get_review_by_id(event, *args):
"""GET Method: get a review by id
accessible with review/{review_id}
"""
from functions.db.connector import get_review_by_id
review_id = event.get('pathParameters').get('review_id')
review = get_review_by_id(review_id)
return make_response(200, review.to_son().to_dict())
def delete_review(event, *args):
"""DELETE Method: delete a review by id
accessible with review/{review_id}
"""
from functions.db.connector import delete_review
review_id = event.get('pathParameters').get('review_id')
delete_review(review_id)
return make_response(204, dict())
def update_review(event, *args):
"""PUT Method: updates a review by its id
accessible with review/{review_id}, "name" and "description" is mandatory in body
"""
from functions.db.connector import update_review
review_id = event.get('pathParameters').get('review_id')
body = json.loads(event["body"])
name = body.get('review').get('name')
description = body.get('review').get('description')
updated_review = update_review(review_id, name, description)
return make_response(200, updated_review.to_son().to_dict())
def add_user_handler(event, *args):
from functions.db.connector import add_user
from bson import json_util
body = json.loads(event["body"])
username = body.get('username')
name = body.get('name')
surname = body.get('surname')
email = body.get('email')
password = body.get('password')
added_user = add_user(username, name, surname, email, password)
return make_response(201, added_user.to_son().to_dict())
def get_user_by_username_handler(event, *args):
from functions.db.connector import get_user_by_username
username = event.get('pathParameters').get('username')
user = get_user_by_username(username)
return make_response(200, user.to_son().to_dict())
def get_all_users_handler(event, *args):
from functions.db.connector import get_users
users = get_users()
return make_response(200, users)
def update_user_handler(event, *args):
from functions.db.connector import update_user, get_user_by_username
body = json.loads(event["body"])
username = body.get('username')
name = body.get('name')
surname = body.get('surname')
email = body.get('email')
password = body.get('password')
user = get_user_by_username(username)
updated_user = update_user(user, name, surname, email, password)
return make_response(200, updated_user.to_son().to_dict())
def add_api_key_to_user_handler(event, *args):
from functions.db.connector import add_api_key_to_user, get_user_by_username
from functions.authentication import get_username_from_jwt
headers = event["headers"]
token = headers.get('authorizationToken')
user = get_user_by_username(get_username_from_jwt(token))
body = json.loads(event["body"])
api_key = body.get('db_name')
db_name = body.get('api_key')
add_api_key_to_user(user, body)
return make_response(201, dict())
def delete_user_handler(event, *args):
from functions.db.connector import delete_user, get_user_by_username
username = event.get('pathParameters').get('username')
user_to_delete = get_user_by_username(username)
delete_user(user_to_delete)
return make_response(200, dict())
def login_handler(event, *args):
from functions.db.connector import get_user_by_username, check_if_password_is_correct, add_jwt_to_session
from functions.authentication import get_jwt_for_user
body = json.loads(event["body"])
username = body.get('username')
password = body.get('password')
user = get_user_by_username(username)
password_correct = check_if_password_is_correct(user, password)
if password_correct:
token = get_jwt_for_user(user)
add_jwt_to_session(user, token)
return make_response(200, token)
else:
return make_response(401, "Authentication failed")
def logout_handler(event, *args):
from functions.authentication import check_for_token, get_username_from_jwt
from functions.db.connector import remove_jwt_from_session, get_user_by_username
headers = event["headers"]
token = headers.get('authorizationToken')
if check_for_token(token):
username = get_username_from_jwt(token)
user = get_user_by_username(username)
remove_jwt_from_session(user)
return make_response(200, "Successfully logged out")
else:
return make_response(401, "Authentication failed")
def check_jwt_handler(event, *args):
from functions.authentication import check_for_token
from functions.db.connector import check_if_jwt_is_in_session
headers = event["headers"]
token = headers.get('authorizationToken')
if check_for_token(token) and check_if_jwt_is_in_session(token):
return make_response(200, token)
else:
return make_response(401, "Authentication failed")
def update_score(event, *args):
"""Handles score updates
Args:
url: score/{review_id}?doi
body:
username:
score:
comment:
Returns:
{
"result": {
...
}
}
"""
body = json.loads(event["body"])
review_id = event.get('pathParameters').get('review_id')
review = connector.get_review_by_id(review_id)
doi = event.get('queryStringParameters').get('doi')
result = connector.get_result_by_doi(review, doi)
user_id = body.get('username')
score = body.get('score')
comment = body.get('comment')
evaluation = {
"user": user_id,
"score": score,
"comment": comment
}
updated_result = connector.update_score(review, result, evaluation)
resp_body = {
"result": updated_result.to_son().to_dict()
}
return make_response(status_code=201, body=resp_body)