mirror of
https://github.com/Xevion/power-math.git
synced 2025-12-06 19:15:51 -06:00
flask restful based API handling & exceptions work
This commit is contained in:
@@ -7,10 +7,9 @@ API interactions, static or dynamic.
|
||||
import copy
|
||||
import random
|
||||
|
||||
from flask import jsonify
|
||||
from flask_restful import Resource, reqparse, abort
|
||||
|
||||
from server import questions
|
||||
from server import questions, exceptions
|
||||
from server.helpers import generate_id
|
||||
|
||||
# Stores active questions in memory for checking answers.
|
||||
@@ -19,7 +18,10 @@ active_questions = {}
|
||||
|
||||
# Question categories, key name, value function for acquiring a random question generator.
|
||||
categories = {
|
||||
'arithmetic': questions.get_arithmetic
|
||||
'arithmetic': (
|
||||
questions.get_arithmetic,
|
||||
'The basic four mathematical functions, plus fractions, exponents and a little more.'
|
||||
)
|
||||
}
|
||||
|
||||
parser = reqparse.RequestParser()
|
||||
@@ -35,11 +37,17 @@ class Question(Resource):
|
||||
They are identified by a String ID.
|
||||
"""
|
||||
|
||||
def get(self, question_id):
|
||||
def get(self, question_id=None):
|
||||
"""Retrieve information about a given question, including the answer for it."""
|
||||
pass
|
||||
if question_id is not None:
|
||||
if question_id in active_questions.keys():
|
||||
return active_questions[question_id]
|
||||
else:
|
||||
raise exceptions.InvalidQuestion(404, question=question_id)
|
||||
else:
|
||||
abort(404, message='Use PUT to create questions, otherwise specify a question ID')
|
||||
|
||||
def put(self, question_id = None):
|
||||
def put(self, question_id=None):
|
||||
"""
|
||||
Request a new question.
|
||||
|
||||
@@ -48,26 +56,46 @@ class Question(Resource):
|
||||
The Question object is returned, although the answer is omitted.
|
||||
"""
|
||||
args = parser.parse_args()
|
||||
q_id = None
|
||||
|
||||
# Generate new Question ID
|
||||
q_id = Nonewt
|
||||
while q_id in active_questions.keys() or q_id is None:
|
||||
q_id = generate_id(5)
|
||||
|
||||
# Get category arg or choose one if not specified.
|
||||
if args.get('category') is not None:
|
||||
category = args.get('category')
|
||||
# Check that the category is valid
|
||||
if category not in categories.keys():
|
||||
abort(404, message=f'Category {category} is not valid.')
|
||||
raise exceptions.InvalidCategory(404, category=category)
|
||||
else:
|
||||
category = random.choice(list(categories.keys()))
|
||||
|
||||
# Acquire a question generator and generate a function, then store the result.
|
||||
active_questions[q_id] = categories[category]()()
|
||||
active_questions[q_id] = categories[category][0]()()
|
||||
|
||||
# Make a shallow copy, hide 'answer' key.
|
||||
question = copy.copy(active_questions[q_id])
|
||||
del question['answer']
|
||||
|
||||
return question, 200
|
||||
return 201, question
|
||||
|
||||
def delete(self, question_id):
|
||||
"""Delete a question object from the running before it is automatically removed."""
|
||||
if question_id in active_questions.keys():
|
||||
del active_questions[question_id]
|
||||
return {'message': f'Successfully deleted Question ID {question_id}'}
|
||||
else:
|
||||
raise exceptions.InvalidQuestion(404, question=question_id)
|
||||
|
||||
|
||||
class Questions(Resource):
|
||||
"""
|
||||
Simple resource for listing all available question objects.
|
||||
"""
|
||||
|
||||
def get(self):
|
||||
return active_questions
|
||||
|
||||
|
||||
class Category(Resource):
|
||||
@@ -78,7 +106,10 @@ class Category(Resource):
|
||||
|
||||
def get(self, category_id):
|
||||
"""Get all information about a category"""
|
||||
pass
|
||||
if category_id in categories.keys():
|
||||
return categories[category_id][1]
|
||||
else:
|
||||
raise exceptions.InvalidCategory(404, category=category_id)
|
||||
|
||||
|
||||
class Categories(Resource):
|
||||
@@ -90,5 +121,3 @@ class Categories(Resource):
|
||||
def get(self):
|
||||
"""Get a list of all categories."""
|
||||
return list(categories.keys()), 200
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user