UDACITRIVIA Trivia App

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.

This application will:

  1. 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.
  2. Delete questions.
  3. Add questions and require that they include question and answer text.
  4. Search for questions based on a text query string.
  5. Play the quiz game, randomizing either all questions or within a specific category.

Completing this trivia app will give you the ability to structure plan, implement, and test an API - skills essential for enabling your future applications to communicate with others.

Getting Started

Pre-requisite and Local Development

This app requires 'nodejs', 'python3', 'pip' and 'postgres' installed on the local machine

Backend

From the backend folder run pip install requirements.txt. All required packages are included in the requirements file.

Create virtual enviroment with python3 -m venv venv

To run the application run the following commands:

export FLASK_APP=flaskr
export FLASK_ENV=development
flask run

By default, the frontend will run on localhost:3000.

Frontend

tip: this frontend is designed to work with Flask-based Backend so it will not load successfully if the backend is not working or not connected. We recommend that you stand up the backend first, test using Postman or curl, update the endpoints in the frontend, and then the frontend should integrate smoothly.

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.

API Reference

Getting Started

  • 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.

Error Handling

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

Endpoints

GET '/categories'

  • curl -X 'GET' http://127.0.0.1:5000/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 an object of id: category_string key:value pairs.

{
  "categories": {
    "1": "Science",
    "2": "Art",
    "3": "Geography",
    "4": "History",
    "5": "Entertainment",
    "6": "Sports"
  }
}

GET '/questions?page=${integer}'

  • curl -X 'GET' http://127.0.0.1:5000/questions?page=1``

  • Fetches a paginated set of questions, a total number of questions, all categories and current category string.

  • Request Arguments: page - integer

  • Returns: An object with 10 paginated questions, total questions, object including all categories, and current category string

{
  "questions": [
    {
      "id": 1,
      "question": "This is a question",
      "answer": "This is an answer",
      "difficulty": 5,
      "category": 2
    }
  ],
  "totalQuestions": 100,
  "categories": {
    "1": "Science",
    "2": "Art",
    "3": "Geography",
    "4": "History",
    "5": "Entertainment",
    "6": "Sports"
  },
  "currentCategory": "History"
}

GET '/categories/${id}/questions'

  • curl -X 'GET' http://127.0.0.1:5000/categories/4/questions

  • Fetches questions for a cateogry specified by id request argument

  • Request Arguments: id - integer

  • Returns: An object with questions for the specified category, total questions, and current category string

{
  "questions": [
    {
      "id": 1,
      "question": "This is a question",
      "answer": "This is an answer",
      "difficulty": 5,
      "category": 4
    }
  ],
  "totalQuestions": 100,
  "currentCategory": "History"
}

DELETE '/questions/${id}'

  • curl -X 'DELETE' http://127.0.0.1:5000/books/16?page=2

  • Deletes a specified question using the id of the question

  • Request Arguments: id - integer

  • Returns: Does not need to return anything besides the appropriate HTTP status code. Optionally can return the id of the question. If you are able to modify the frontend, you can have it remove the question using the id instead of refetching the questions.


POST '/quizzes'

  • curl -X 'GET' http://127.0.0.1:5000/quizzes -H '{"Content-Type":'application/json'}' -d body``

  • Sends a post request in order to get the next question

  • Request Body:

{
    'previous_questions': [1, 4, 20, 15]
    quiz_category': 'current category'
 }
  • Returns: a single new question object
{
  "question": {
    "id": 1,
    "question": "This is a question",
    "answer": "This is an answer",
    "difficulty": 5,
    "category": 4
  }
}

POST '/questions'

  • curl -X 'POST' http://127.0.0.1:5000/questions -H '{"Content-Type":'application/json'}' -d body``

  • Sends a post request in order to add a new question

  • Request Body:

{
  "question": "Heres a new question string",
  "answer": "Heres a new answer string",
  "difficulty": 1,
  "category": 3
}
  • Returns: Does not return any new data

POST '/questions'

  • curl -X 'POST' http://127.0.0.1:5000/questions -H '{"Content-Type":'application/json'}' -d body``

  • Sends a post request in order to search for a specific question by search term

  • Request Body:

{
  "searchTerm": "this is the term the user is looking for"
}
  • Returns: any array of questions, a number of totalQuestions that met the search term and the current category string
{
  "questions": [
    {
      "id": 1,
      "question": "This is a question",
      "answer": "This is an answer",
      "difficulty": 5,
      "category": 5
    }
  ],
  "totalQuestions": 100,
  "currentCategory": "Entertainment"
}

Deployment N/A

Authors

Yours truly, Azeez Mumeen

Acknowledgements

The awesome team at Udacity and all of the students.