Giter Club home page Giter Club logo

02_trivia_api's Introduction

Full Stack Trivia API Project

This project is a game where users can test their knowledge answering trivia questions. The task for the project was to create an API and test suite for implementing the following functionality:

  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.

Getting Started

Installing Dependencies

Developers using this project should already have Python3, pip, node, and npm installed.

Frontend Dependencies

This project uses NPM to manage software dependencies. NPM Relies on the package.json file located in the /starter/frontend directory of this repository. After cloning, open your terminal and run:

npm install

Backend Dependencies

Once you have your virtual environment setup and running, install dependencies by navigating to the /starter/backend directory and running:

pip install -r requirements.txt

Running the Frontend in Dev Mode

The frontend app was built using create-react-app. In order to run the app in development mode use npm start. You can change the script in the package.json file.

Open http://localhost:3000 to view it in the browser. The page will reload if you make edits.

npm start

Running the Server

From within the /starter/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

Windows users:

set FLASK_APP=flaskr
set FLASK_ENV=development
set run

Testing

To run the tests, run

dropdb trivia_test
createdb trivia_test
psql trivia_test < trivia.psql
python test_flaskr.py

Omit the dropdb command the first time you run tests.

API Reference

Getting Started

  • Base URL: Currently this application is only hosted locally. The backend is hosted at http://127.0.0.1:5000/
  • Authentication: This version does not require authentication or API keys.

Error Handling

Errors are returned as JSON in the following format:

{
    "success": False,
    "error": 404,
    "message": "resource not found"
}

The API will return three types of errors:

  • 400 – bad request
  • 404 – resource not found
  • 422 – unprocessable

Endpoints

GET /categories

  • General: Returns a list categories.

  • Sample: curl http://127.0.0.1:5000/categories

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

GET /questions

  • General:

    • Returns a list questions.
    • Results are paginated in groups of 10.
    • Also returns list of categories, current category and total number of questions.
  • Sample: curl http://127.0.0.1:5000/questions

      {
          "categories": {
              "1": "Science", 
              "2": "Art", 
              "3": "Geography", 
              "4": "History", 
              "5": "Entertainment", 
              "6": "Sports"
          },
          "current_category": "Science",
          "questions": [
            {
              "answer": "Blood",
              "category": 1,
              "difficulty": 4,
              "id": 22,
              "question": "Hematology is a branch of medicine involving the study of what?"
            },
            {
              "answer": "Alexander Fleming",
              "category": 1,
              "difficulty": 3,
              "id": 21,
              "question": "Who discovered penicillin?"
            },
            {
              "answer": "The Liver",
              "category": 1,
              "difficulty": 4,
              "id": 20,
              "question": "What is the heaviest organ in the human body?"
            },
            {
              "answer": "One",
              "category": 2,
              "difficulty": 4,
              "id": 18,
              "question": "How many paintings did Van Gogh sell in his lifetime?"
            },
            {
              "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": "The Palace of Versailles",
              "category": 3,
              "difficulty": 3,
              "id": 14,
              "question": "In which royal palace would you find the Hall of Mirrors?"
            },
            {
              "answer": "Heres a new answer string",
              "category": 3,
              "difficulty": 1,
              "id": 24,
              "question": "Heres a new question string"
            },
            {
              "answer": "Lake Victoria",
              "category": 3,
              "difficulty": 2,
              "id": 13,
              "question": "What is the largest lake in Africa?"
            },
            {
              "answer": "Agra",
              "category": 3,
              "difficulty": 2,
              "id": 15,
              "question": "The Taj Mahal is located in which Indian city?"
            }
          ],
          "success": true,
          "total_questions": 20
      }
    

DELETE /questions/<int:id>

  • General:

    • Deletes a question by id using url parameters.
    • Returns id of deleted question upon success.
  • Sample: curl http://127.0.0.1:5000/questions/6 -X DELETE

      {
          "deleted": 11, 
          "success": true
      }
    

POST /questions

This endpoint either creates a new question or returns search results.

  1. If no search term is included in request:
  • General:

    • Creates a new question using JSON request parameters.
    • Returns JSON object with newly created question, as well as paginated questions.
  • Sample: curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{ "question": "Heres a new question string?", "answer": "Heres a new answer string", "difficulty": 1, "category": "5" }'

      {
        "success": true
      }
    
  1. If search term is included in request:
  • General:

    • Searches for questions using search term in JSON request parameters.
    • Returns JSON object with paginated matching questions.
  • Sample: curl http://127.0.0.1:5000/questions -X POST -H "Content-Type: application/json" -d '{"searchTerm": "boxer"}'

      {
          "current_category": 4,
          "questions": [
              {
                  "answer": "Muhammad Ali",
                  "category": 4,
                  "difficulty": 1,
                  "id": 9,
                  "question": "What boxer's original name is Cassius Clay?"
              }
          ],
          "success": true,
          "total_questions": 1
      }
    

GET /categories/<int:id>/questions

  • General:

    • Get questions by category id using url parameters.
    • Returns JSON object with paginated matching questions.
  • Sample: curl http://127.0.0.1:5000/categories/6/questions

    {
      "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?"
        }, 
        {
          "answer": "test A", 
          "category": 6, 
          "difficulty": 1, 
          "id": 25, 
          "question": "test Q"
        }
      ], 
      "success": true, 
      "total_questions": 20
    }
    

POST /quizzes

  • General:

    • Allows users to play the quiz game.
    • Uses JSON request parameters of category and previous questions.
    • Returns JSON object with random question not among previous questions.
  • Sample: curl http://127.0.0.1:5000/quizzes -X POST -H "Content-Type: application/json" -d '{"previous_questions": [16, 17, 18], "quiz_category": {"type": "Art", "id": "2"}}'

    {
        "question": {
            "answer": "One",
            "category": 2,
            "difficulty": 4,
            "id": 18,
            "question": "How many paintings did Van Gogh sell in his lifetime?"
        }
    }
    

Authors

Fisal Assubaieye authored the API (__init__.py), test unit (test_flaskr.py), and this README.
All other project files, including the models and frontend, were created by Udacity as a project template for the Full Stack Web Developer Nanodegree.

02_trivia_api's People

Contributors

fisal77 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.