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 }
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 }
def state_date_cases(state, date): """ Handles requests for a specific state and date. Dates must be in ISO format and be between 20202-01-01 and today. For GET requests, the single record for the specified date is returned. If no record exists, a 404 NotFound error is returned. For DELETE requests, if the record does not exist, the request has no effect and 200 is returned. :param state: The state of the current request. :param date: The date of the current request. :return: For GET requests, the specified data record is returned in the response body in JSON format. For DELETE requests, only the status code is returned. """ logger.info("Got %s to /states/%s/%s.", app.current_request.method, state, date) state = urllib.parse.unquote(state) date = urllib.parse.unquote(date) verify_input(state, date=date) response = None if app.current_request.method == 'GET': response = storage.get_state_date_data(state, date) if response is not None: response = json.dumps(response, default=convert_decimal_to_int) else: raise chalice.NotFoundError( f"No data found for {state} on {date}.") elif app.current_request.method == 'DELETE': storage.delete_state_date_data(state, date) return response