-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapp.py
98 lines (79 loc) · 3.11 KB
/
app.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
import chalice
import chalicelib.questions
import chalicelib.users
import chalicelib.constants
app = chalice.Chalice(app_name='chalice-trivia')
cognito_authorizer = chalice.CognitoUserPoolAuthorizer(
'MyCognitoAuthorizer', provider_arns=[chalicelib.constants.POOL_ARN])
@app.route('/questions/{question_id}', cors=True)
def get_question(question_id):
table = chalicelib.questions.QuestionsTable()
question = table.get_question(question_id)
if not question:
raise chalice.NotFoundError('Requested resource does not exist')
return {
'question_id': question.question_id,
'question': question.question,
'possible_answers': question.possible_answers
}
@app.route('/questions/{question_id}', methods=['POST'], cors=True)
def answer_question(question_id):
if 'answer' not in app.current_request.json_body:
raise chalice.BadRequestError(
'Missing "answer" in request body'
)
provided_answer = app.current_request.json_body['answer']
table = chalicelib.questions.QuestionsTable()
question = table.get_question(question_id)
if not question:
raise chalice.NotFoundError('Requested resource does not exist')
elif provided_answer not in question.possible_answers:
raise chalice.BadRequestError(
'Provided answer: %s is not a valid answer. Please submit an '
'answer from the list of possible answers: %s' % (
provided_answer, question.possible_answers)
)
return {
'is_correct': provided_answer == question.correct_answer,
'provided_answer': provided_answer,
'correct_answer': question.correct_answer,
'question_id': question.question_id
}
@app.route('/questions/{question_id}/user', methods=['POST'],
authorizer=cognito_authorizer, cors=True)
def answer_question_for_user(question_id):
answer_data = answer_question(question_id)
username = _get_authenticated_username()
user_table = chalicelib.users.UsersTable()
try:
user_table.update_user_score(
username, question_id, answer_data['provided_answer'],
answer_data['is_correct']
)
except chalicelib.users.UserAlreadyAnsweredError as e:
raise chalice.BadRequestError(str(e))
return answer_data
@app.route('/user', authorizer=cognito_authorizer, cors=True)
def get_user_data():
username = _get_authenticated_username()
user_table = chalicelib.users.UsersTable()
user = user_table.get_user(username)
return {
'usersname': user.username,
'answers': user.answers,
'total_correct': user.total_correct,
'total_answered': user.total_answered
}
def _get_authenticated_username():
return app.current_request.context[
'authorizer']['claims']['cognito:username']
@app.lambda_function()
def autoconfirm_user(event, context):
event['response']['autoConfirmUser'] = True
return event
@app.lambda_function()
def add_user(event, context):
username = event['userName']
user_table = chalicelib.users.UsersTable()
user_table.add_new_user(username)
return event