Skip to content

Latest commit

 

History

History
235 lines (175 loc) · 8.26 KB

README.md

File metadata and controls

235 lines (175 loc) · 8.26 KB

Backend - Trivia API

Setting up the Backend

Install Dependencies

  1. Python 3.7 - Follow instructions to install the latest version of python for your platform in the python docs

  2. Virtual Environment - We recommend working within a virtual environment whenever using Python for projects. This keeps your dependencies for each project separate and organized. Instructions for setting up a virual environment for your platform can be found in the python docs

  3. PIP Dependencies - Once your virtual environment is setup and running, install the required dependencies by navigating to the /backend directory and running:

pip install -r requirements.txt

Key Pip Dependencies

  • 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 to handle the lightweight SQL database. You'll primarily work in app.pyand can reference models.py.

  • Flask-CORS is the extension we'll use to handle cross-origin requests from our frontend server.

Set up the Database

With Postgres running, create a trivia database:

createbd trivia

Populate the database using the trivia.psql file provided. From the backend folder in terminal run:

psql trivia < trivia.psql

Run the Server

From within the ./src directory first ensure you are working using your created virtual environment.

To run the server, execute:

flask run --reload

The --reload flag will detect file changes and restart the server automatically.

To Do Tasks

These are the files you'd want to edit in the backend:

  1. backend/flaskr/__init__.py
  2. backend/test_flaskr.py

One note before you delve into your tasks: for each endpoint, you are expected to define the endpoint and response data. The frontend will be a plentiful resource because it is set up to expect certain endpoints and response data formats already. You should feel free to specify endpoints in your own way; if you do so, make sure to update the frontend or you will get some unexpected behavior.

  1. Use Flask-CORS to enable cross-domain requests and set response headers.
  2. Create an endpoint to handle GET requests for questions, including pagination (every 10 questions). This endpoint should return a list of questions, number of total questions, current category, categories.
`/api/v1/questions/?page=1`
  1. Create an endpoint to handle GET requests for all available categories.
`/api/v1/categories`
  1. Create an endpoint to DELETE a question using a question ID.
`/api/v1/questions_delete/<int:id>/`
  1. Create an endpoint to POST a new question, which will require the question and answer text, category, and difficulty score.
 '/api/v1/create_question'
 `FormData: json={'question':'Who is the founder of Udacity?',"answer":"Sebastian Thrun","difficulty":"2","category":1,}
  1. Create a POST endpoint to get questions based on category.

    `/api/v1/categories/<int:id>/questions`
    
  2. Create a POST endpoint to get questions based on a search term. It should return any questions for whom the search term is a substring of the question.

    `/api/v1/create_search', json={'searchTerm':"Africa"}`
    
  3. Create a POST endpoint to get questions to play the quiz. This endpoint should take a category and previous question parameters and return a random questions within the given category, if provided, and that is not one of the previous questions.

`/api/v1/quizzes/', json={'previous_questions':[],'quiz_category':{'id':1}}`
  1. Create error handlers for all expected errors including 400, 404, 422, and 500.

Endpoints

GET '/api/v1.0/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.
{
  "1": "Science",
  "2": "Art",
  "3": "Geography",
  "4": "History",
  "5": "Entertainment",
  "6": "Sports"
}

GET '/api/v1/questions/'

  • Fetches a dictionary of questions in which the keys are the name of the columns and the value is the corresponding values. However, each unit represents a row of data.
  • Request Arguments: Number of pages
  • Returns: An object with a single key, questions, that contains an object with the following keys answer,category,difficulty, id, question, key: value pairs.
"questions": {
      "answer": "Maya Angelou",
      "category": 4,
      "difficulty": 2,
      "id": 5,
      "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
    }

POST '/api/v1/questions_delete/<int:id>'

  • Deletion a question with a given id.
  • Request Arguments: ID
  • Returns: An object with two keys, success and `message',
{
      "success": "True",
      "message": "question ({id}) deleted  successfully!",
    }

POST '/api/v1/create_question'

  • Create a new question.
  • Request Arguments: Formdata
  • Returns: An object with two keys, success and message,
{
      "success": "True",
      "message": "question created successfully!",
    }

POST '/api/v1/create_search'

  • Search for a question that match the keywords.
  • Request Arguments: Formdata
  • Returns: An object with four keys, success, message,questions, total_questions
{
      "success": "True",
      "message": "question created successfully!",
      "questions": {
                    "answer": "Maya Angelou",
                    "category": 4,
                    "difficulty": 2,
                    "id": 5,
                    "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
                  },
      "total_questions":1,
    }

POST '/api/v1/categories/<int:id>/questions

  • Questions based on categories.
  • Request Arguments: ID
  • Request Body:
  • Returns: An object with four keys, success, questions, total_questions, current_category
{
      "success": "True",
      "questions": {
                    "answer": "Maya Angelou",
                    "category": 4,
                    "difficulty": 2,
                    "id": 5,
                    "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?"
                  },
      "total_questions":1,
      "current_category":4,
    }

POST '/api/v1/quizzes

  • deliver a single question that has not been answered.
  • Request Arguments: ID
  • Request Body: FormData
  • Returns: An object with four keys, success, questions, total_questions, current_category
{
      "success": "True",
      "questions": {
                    "answer": "Maya Angelou",
                    "category": 4,
                    "difficulty": 2,
                    "id": 5,
                    "question": "Whose autobiography is entitled 'I Know Why the Caged Bird Sings'?",       
                    "quiz_category":4,
                  },
      "total_questions":1,
    }

Testing

  1. Create a psql database with the name trivia_test and import the psql file located in /backend/trivia.psql

  2. Tests are located in /backend/test_flaskr.py.

  3. To run the test go to the backend folder in your terminal and run python test_flaskr.py