Udacity is invested in creating bonding experiences for its employees and students. A bunch of team members got the idea to hold trivia on a regular basis and created a webpage to manage the trivia app and play the game, but their API experience is limited and still needs to be built out.
The application has the following features:
- Display questions - both all questions and by category. Questions should show the question, category and difficulty rating by default and can show/hide the answer.
- Delete questions.
- Add questions and require that they include question and answer text.
- Update questions.
- Play the quiz game, randomizing either all questions or within a specific category.
GET '/categories'
- Fetches a dictionary of categories in which the keys are the ids and the value is the corresponding string of the category
- Request Arguments: None
- Returns: An object with a single key, categories, that contains a object of id: category_string key:value pairs.
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
GET '/questions'
- Fetches a dictionary of categories in which the keys are the ids and the value is the corresponding string of the category
- Fetches a list of questions.
- Request Arguments: Page Number
- Returns: Dictionary of Categories, current category, list of questions and total number of questions.
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"current_category": null,
"questions": [
{
"answer": "Apollo 13",
"category": 5,
"difficulty": 4,
"id": 2,
"question": "What movie earned Tom Hanks his third straight Oscar nomination, in 1996?"
},
{
"answer": "Tom Cruise",
"category": 5,
"difficulty": 4,
"id": 4,
"question": "What actor did author Anne Rice first denounce, then praise in the role of her beloved Lestat?"
}
],
"total_questions": 26,
"success": true
}
DELETE '/questions/<int:question_id>'
- Deletes question from the database.
- Request Arguments: questions_id
- Returns: true with status 204 if successfully deleted.
{
"success": true
}
POST '/questions'
- Create a new question
- Request Body: question, answer, difficulty and category.
- Returns: true and question id with status 201 if successfully created.
Request
{
"question": "Test question",
"answer": "Answer",
"category": 1,
"difficulty": 1
}
Response
{
"success": true,
"id": 15
}
PATCH '/questions<int:question_id>'
- Updates the question.
- Request Arguments: questions_id
- Request Body: question, answer, difficulty and category.
- Returns: true and question id with status 200 if successfully updated.
Request
id=1
{
"question": "Test question",
"answer": "Answer",
"category": 1,
"difficulty": 1
}
Response
{
"question": "Test question",
"answer": "Answer",
"category": 1,
"difficulty": 1,
"id": 1
}
POST '/categories/<int:category_id>/questions'
- To get questions based on category
- Request Arguments: category_id
- Returns: List of questions, total number of questions and current category.
{
"current_category": {
"id": 1,
"type": "Science"
},
"questions": [
{
"answer": "The Liver",
"category": 1,
"difficulty": 4,
"id": 20,
"question": "What is the heaviest organ in the human body?"
},
{
"answer": "Alexander Fleming",
"category": 1,
"difficulty": 3,
"id": 21,
"question": "Who discovered penicillin?"
}
],
"total_questions": 13,
"success": true
}
POST '/quizzes'
- In order to play the quiz.
- Returns: Random question within the given category.
Request
{
"quiz_category": {
"id": 1
},
"previous_questions": []
}
Response
{
"question": {
"answer": "Blood",
"category": 1,
"difficulty": 4,
"id": 22,
"question": "Hematology is a branch of medicine involving the study of what?"
},
"success": true
}
Bad Request 400
{
'success': false,
'error': 400,
'message': 'Bad Request'
}
Unauthorized 401
{
'success': false,
'error': 401,
'message': 'Unauthorized'
}
Forbidden 403
{
'success': false,
'error': 403,
'message': 'Forbidden'
}
Not Found 404
{
'success': false,
'error': 404,
'message': 'Not Found'
}
Method Not Allowed 405
{
'success': false,
'error': 405,
'message': 'Method Not Allowed'
}
Unprocessable Entity 422
{
'success': false,
'error': 422,
'message': 'Unprocessable Entity'
}
Internal Server Error 500
{
'success': false,
'error': 500,
'message': 'Internal Server Error'
}
add:question
permission to add question through through POST'/questions'
apiedit:question
permission to update question through PATCH'/questions<int:question_id>'
apidelete:question
permission to delete question through through DELETE'/questions<int:question_id>'
apiplay:quiz
permission to play quiz through POST'/quizzes'
api
Can add/update/delete question
Permissions:
add:question
edit:question
delete:question
Can play quiz
Permissions:
play:quiz
Follow instructions to install the latest version of python for your platform in the python docs
We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organaized. Instructions for setting up a virual enviornment for your platform can be found in the python docs
Once you have your virtual environment setup and running, install dependencies by naviging to the /backend
directory and running:
pip install -r requirements.txt
This will install all of the required packages we selected within the requirements.txt
file.
-
Flask is a lightweight backend microservices framework. Flask is required to handle requests and responses.
-
SQLAlchemy is the Python SQL toolkit and ORM we'll use handle the lightweight sqlite database. You'll primarily work in app.py and can reference models.py.
-
Flask-CORS is the extension we'll use to handle cross origin requests from our frontend server.
With Postgres running, restore a database using the trivia.psql file provided. From the backend folder in terminal run:
psql trivia < trivia.psql
From within the backend
directory first ensure you are working using your created virtual environment.
To run the server, execute:
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
Setting the FLASK_ENV
variable to development
will detect file changes and restart the server automatically.
Setting the FLASK_APP
variable to flaskr
directs flask to use the flaskr
directory and the __init__.py
file to find the application.
To run the tests, run
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py
Tokens to test the api end points are added in the role_tokens.json
file.
The below link is used to retrieve tokens.
https://udacityfsnd.auth0.com/authorize?audience=capstone&response_type=token&client_id=hZOMNJb2DXT6khq5FWsmeblr7SE3wUNz&redirect_uri=http://localhost:5000/
safi.ullah@arbisoft.com
has the Admin
role and test.arbisofterp@gmail.com
has the Player
role.
Web app is deployed at:
https://udacity-demo.herokuapp.com/