REST API written in Python/Flask to manage a simple todo list.
Documents are persisted in MongoDB.
- Install MongoDB
- cd to the project folder, create and activate a virtual env, e.g.:
python3 -m virtualenv env && source env/bin/activate
pip install -r requirements.txt && pip install .
To run the tests, a local instance of MongoDB must be running (e.g.: running MongoDB on Linux)
Ensure that you are in the virtualenv, cd to the project folder and: python -m unittest discover -s tests
To run the Flask app cd to the project folder and python todo_api/main.py
The app has been hardcoded with a single user (login = test_login, password = password123).
After login the session value in the response cookie can be used to authenticate further api requests.
To obtain a session cookie for this user.
POST http://localhost:5000/login
{
"login": "test_login",
"password": "password123"
}
Status: 200 OK
Headers:
Set-Cookie: session=.eJwlzrsNwzAMANFdVLsgRUqivEzAj4ikteMqyO4xkAEO7z7lkcc6n2V_H9fayuMVZS9TQ8h8uGjNGHWFuTqoKy0ZYZgAXWt1Vkaj5gGMXu8GeSZoBkjnLgNxdWuSvUsDJmhzVQ5CAUcEdkJ0t5vJfnuYU_FWrWzlOtfxn2nWrTanljMpzOrEucShfH-IRjYj.Dk5TQA.rDiobiHxUpvjFf7F8txh4-1Vvm4;
A todo item requires a title, the description field is optional.
Returns a new todo item.
POST http://localhost:5000/todo/todo_item
{
"title": "title",
"description": "description"
}
{
"item": {
"completed": false,
"description": "description",
"id": "5b6cbd6a57fac3dbb6cb242f",
"title": "title",
"user_id": "5b6b25c35f9f3dbb2919e8c0"
}
}
GET http://localhost:5000/todo/todo_item/:user_id
Returns a list of todo items.
GET http://localhost:5000/todo/todo_item/5b6b25c35f9f3dbb2919e8c0
[
{
"item": {
"completed": true,
"description": "description1",
"id": "5b6b37245f9f3dbfda30cc7f",
"title": "title1",
"user_id": "5b6b25c35f9f3dbb2919e8c0"
}
},
{
"item": {
"completed": false,
"description": "description2",
"id": "5b6b37585f9f3dbfef989bd2",
"title": "title2",
"user_id": "5b6b25c35f9f3dbb2919e8c0"
}
},
]
PUT http://localhost:5000/todo/todo_item/:item_id
The same request can also be used to mark the todo item as not complete (by using "completed": false
in the request body).
Returns the updated todo item.
PUT http://localhost:5000/todo/todo_item/5b6b4f2d57fac3c5eac1aa6a
{
"completed": true
}
{
"item": {
"completed": true,
"description": "description",
"id": "5b6b4f2d57fac3c5eac1aa6a",
"title": "title",
"user_id": "5b6b25c35f9f3dbb2919e8c0"
}
}
DELETE http://localhost:5000/todo/todo_item/:item_id
DELETE http://localhost:5000/todo/todo_item/5b6b4f2d57fac3c5eac1aa6a
Status: 204 NO CONTENT
GET http://localhost:5000/logout
Status: 204 NO CONTENT