Skip to content

OneEyeX/Trivia-API-Project-2-Nanodegree-Udacity

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TRIVIA APP

Project DOCUMENTATION

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.

All backend code follows PEP8 style guidelines.

Getting Started

Pre-requisites and Local Development

Developers using this project should already have Python3, pip and node installed on their local machines.

Backend

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.

Frontend

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.

Tests

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.

API Reference

Error Handling

Errors are returned as JSON objects in the following format:

{
    "success": False, 
    "error": 400,
    "message": "Bad request"
}

The API will return 4 error types when requests fail:

  • 400: Bad request
  • 404: Page not found
  • 422: Unprocessable resource
  • 500: Internal server error

Endpoints

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 an 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?page=${integer}'

  • 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
{
    "categories": {
        "1": "Science",
        "2": "Art",
        "3": "Geography",
        "4": "History",
        "5": "Entertainment",
        "6": "Sports",
    },
    "questions": [
        {
            "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": "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?"
        },
        {
            "answer": "Escher",
            "category": 2,
            "difficulty": 1,
            "id": 16,
            "question": "Which Dutch graphic artist–initials 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?"
        }
    ],
    "success": true,
    "total_questions": 18
}

GET '/categories/${id}/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
{
    "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": 2
}

DELETE '/questions/${id}'

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

the output:

{
    "question_deleted_id": 18,
    "success": true,
    "total_questions": 8
}

POST '/quizzes'

  • Sends a post request in order to get the next question
  • Request Body:
{
    "quiz_category": "current category"
    "previous_questions": [1, 7, 5, 4]
 }
  • Returns: a single new question object
{
    "success":true,
    "question": {
        "id": 1,
        "question": "This is a question",
        "answer": "This is an answer",
        "difficulty": 5,
        "category": 4
            }
}

POST '/questions'

  • Sends a post request in order to add a new question
  • Request Body:
{
  "question": "new question text",
  "answer": "new answer text",
  "difficulty": 1,
  "category": 1
}
  • Returns:
{
    "new_question": "new question text",
    "new_question_id": 47,
    "success": true,
    "total_questions": 11
}

POST '/categories'

  • Sends a post request in order to add a new category (added for CHALLENGE 3)
  • Request Body:
{
  "type": "Movies",
}
  • Returns:
{
  "message": "Category created successfully",
  "new_category_id": 10,
  "new_category_type": "Movies",
  "success": true
}

POST '/users'

  • Sends a post request in order to add a new user (Player) (added for CHALLENGE 2)
  • Request Body:
{
  "username": "Player 2",
  "score": 0
}
  • Returns:
{
  "message": "User created successfully",
  "new_user_id": 3,
  "success": true,
  "username": "Player 7"
}

POST '/questions/search'

  • 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 example of output for term what
{
    "current_category": null,
    "questions": [
        {
            "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": "Lake Victoria",
            "category": 3,
            "difficulty": 2,
            "id": 13,
            "question": "What is the largest lake in Africa?"
        },
        {
            "answer": "Mona Lisa",
            "category": 2,
            "difficulty": 3,
            "id": 17,
            "question": "La Giaconda is better known as what?"
        }
    ],
    "success": true,
    "total_questions": 3
}

CURL REQUESTS :

--------------- CURL QUERIES TO TEST ENDPOINTS -------------

to get a list of all categories

curl http://127.0.0.1:5000/categories

to get all questions (paginated and default page is page 1)

curl http://127.0.0.1:5000/questions

to get all questions of a specific page (page 3 for example)

curl http://127.0.0.1:5000/questions?page=3

to delete a specific question (with ID)

curl -X DELETE http://127.0.0.1:5000/questions/7 

to add a new question

curl -X POST -H "Content-Type: application/json" -d '{"question":"what is my country?", "answer":"Tunisia", "category":"1", "difficulty":"2"}' http://127.0.0.1:5000/questions 

to add a new user (added for CHALLENGE 2)

curl -X POST -H "Content-Type: application/json" -d '{"username":"Player 7" , "score":0 }' http://127.0.0.1:5000/users

to delete a specific user (with ID) (added for CHALLENGE 2)

curl -X DELETE http://127.0.0.1:5000/users/5 

to add a new category (added for CHALLENGE 3)

curl -X POST -H "Content-Type: application/json" -d '{"type":"Movies"}' http://127.0.0.1:5000/categories 

Authors

Yours truly, Chedly CHAHED

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published