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.
Completing this trivia app gave me the ability to structure plan, implement, and test an API - skills essential for enabling my future applications to communicate with others.
Developers using this project should already have Python3, pip and node installed on their local machines.
From the backend folder run pip install requirements.txt
. All required packages are included in the requirements file.
To run the application run the following commands:
export FLASK_APP=flaskr
export FLASK_ENV=development
flask run
These commands put the application in development and directs our application to use the __init__.py
file in our flaskr folder. Working in development mode shows an interactive debugger in the console and restarts the server whenever changes are made. If running locally on Windows, look for the commands in the Flask documentation.
The application is run on http://127.0.0.1:5000/
by default and is a proxy in the frontend configuration.
From the frontend folder, run the following commands to start the client:
npm install // only once to install dependencies
npm start
By default, the frontend will run on localhost:3000.
In order to run tests navigate to the backend folder and run the following commands:
dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py
The first time you run the tests, omit the dropdb command.
All tests are kept in that file and should be maintained as updates are made to app functionality.
- Base URL: At present this app can only be run locally and is not hosted as a base URL. The backend app is hosted at the default,
http://127.0.0.1:5000/
, which is set as a proxy in the frontend configuration. - Authentication: This version of the application does not require authentication or API keys.
Errors are returned as JSON objects in the following format:
{
"success": False,
"error": 400,
"message": "bad request"
}
The API will return three error types when requests fail:
- 400: Bad Request
- 404: Resource Not Found
- 422: Not Processable
- General:
- Returns a list of categories , success value.
- Sample:
curl http://127.0.0.1:5000/categories
{
"categories": {
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
},
"success": true
}
- General:
- Returns a list of question objects based on category, categories, current category, success value, and total number of questions
- Results are paginated in groups of 10. Include a request argument to choose page number, starting from 1.
- Sample:
curl http://127.0.0.1:5000/questions
{
"categories": [
{
"1": "Science",
"2": "Art",
"3": "Geography",
"4": "History",
"5": "Entertainment",
"6": "Sports"
}
],
"current_category": "Sports",
"questions": [
{
"answer": "Brazil",
"category": 6,
"difficulty": 3,
"id": 10,
"question": "Which is the only team to play in every soccer World Cup tournament?"
},
{
"answer": "Uruguay",
"category": 6,
"difficulty": 4,
"id": 11,
"question": "Which country won the first ever soccer World Cup in 1930?"
}
],
"success": true,
"total_questions": 19
}
- General:
- Returns a list of question objects based on category, current category, success value, and total number of questions
- Results are paginated in groups of 10. Include a request argument to choose page number, starting from 1.
- Sample:
curl http://127.0.0.1:5000/categories/2/questions
{
"currentCategory": 2,
"questions": [
{
"answer": "Escher",
"category": 2,
"difficulty": 1,
"id": 16,
"question": "Which Dutch graphic artist\u2013initials M C was a creator of optical illusions?"
},
{
"answer": "Mona Lisa",
"category": 2,
"difficulty": 3,
"id": 17,
"question": "La Giaconda is better known as what?"
},
{
"answer": "One",
"category": 2,
"difficulty": 4,
"id": 18,
"question": "How many paintings did Van Gogh sell in his lifetime?"
},
{
"answer": "Jackson Pollock",
"category": 2,
"difficulty": 2,
"id": 19,
"question": "Which American artist was a pioneer of Abstract Expressionism, and a leading exponent of action painting?"
},
{
"answer": "yellow and blue",
"category": 2,
"difficulty": 1,
"id": 24,
"question": "what is the mix that produce green color?"
}
],
"success": true,
"totalQuestions": 19
}
- General:
- Deletes the question of the given ID if it exists. Returns the id of the deleted question, success value, total questions, and question list based on current page number .
curl -X DELETE http://127.0.0.1:5000/questions/16?page=2
{
"questions": [
{
"answer": "Maya Angelou",
"category": 4,
"difficulty": 2,
"id": 5,
"question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
},
{
"answer": "Muhammad Ali",
"category": 4,
"difficulty": 1,
"id": 9,
"question": "What boxer's original name is Cassius Clay?"
},
{
"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?"
},
{
"answer": "Edward Scissorhands",
"category": 5,
"difficulty": 3,
"id": 6,
"question": "What was the title of the 1990 fantasy directed by Tim Burton about a young man with multi-bladed appendages?"
},
{
"answer": "Brazil",
"category": 6,
"difficulty": 3,
"id": 10,
"question": "Which is the only team to play in every soccer World Cup tournament?"
},
{
"answer": "Uruguay",
"category": 6,
"difficulty": 4,
"id": 11,
"question": "Which country won the first ever soccer World Cup in 1930?"
},
{
"answer": "George Washington Carver",
"category": 4,
"difficulty": 2,
"id": 12,
"question": "Who invented Peanut Butter?"
},
{
"answer": "Lake Victoria",
"category": 3,
"difficulty": 2,
"id": 13,
"question": "What is the largest lake in Africa?"
},
{
"answer": "The Palace of Versailles",
"category": 3,
"difficulty": 3,
"id": 14,
"question": "In which royal palace would you find the Hall of Mirrors?"
}
],
"deleted": 16,
"success": true,
"total_questions": 18
}
-
General:
- Creates a new question using the submitted question, answer, category and difficulty. Returns the id of the created question, success value, total questions, and questions list based on current page number to update the frontend.
- Sends a post request in order to search for a specific question by search term
-
curl http://127.0.0.1:5000/questions?page=3 -X POST -H "Content-Type: application/json" -d '{"question":"This is a question", "answer":"This is an answer","category":"5", "difficulty":"5"}'
{
'questions': [
{
'id': 20,
'question': 'This is a question',
'answer': 'This is an answer',
'difficulty': 5,
'category': 5
},
],
'total_questions': 100,
'current_category': 'Entertainment'
}
- Sends a post request in order to get the next question
- Returns a single new question object
{
'question': {
'id': 1,
'question': 'This is a question',
'answer': 'This is an answer',
'difficulty': 5,
'category': 4
}
}
Udacity team , Maymonah Althunayan
The awesome team at Udacity and all of the students, soon to be full stack extraordinaires!