This RESTful API, built using Flask, manages a to-do list. It allows creating, retrieving, updating, and deleting tasks. Each task includes a unique identifier, name, description, completion status, and an optional due date in ISO format (YYYY-MM-DD). The API uses a dictionary to store tasks. This approach allows for efficient retrieval, updating, and deletion of tasks by their unique identifiers, compared to a list where each element might need to be iterated over to find a specific task.
Retrieves a list of all tasks, with optional parameters to limit the number of tasks and sort by due date.
| Aspect | Details |
|---|---|
| URL Params | limit=[integer]: Limits the number of tasks returned.`sort_by_date=[asc |
| Success Response | Code: 200 OKContent: List of tasks |
| Error Response | Code: 400 Bad RequestContent: {'message': 'Invalid limit value. It must be a positive integer.'} |
Retrieves a specific task by its unique identifier.
| Aspect | Details |
|---|---|
| URL Params | task_id=[string]: Unique identifier of the task. |
| Success Response | Code: 200 OKContent: Task details |
| Error Response | Code: 404 Not FoundContent: {'message': 'Task not found'} |
Creates a new task with provided details.
| Aspect | Details |
|---|---|
| Data Params | name=[string]description=[string]due_date=[string] (Optional, must be in ISO format) |
| Success Response | Code: 201 CreatedContent: Created task details |
| Error Responses | Missing Required Fields: - Code: 400 Bad Request- Content: {'message': 'Missing required fields (name, description).'}Invalid Due Date Format: - Code: 400 Bad Request- Content: {'message': 'Invalid due date format. Please use YYYY-MM-DD.'} |
Updates an existing task identified by its unique identifier.
| Aspect | Details |
|---|---|
| URL Params | task_id=[string]: Unique identifier of the task. |
| Data Params | Same as POST /tasks. |
| Success Response | Code: 200 OKContent: Updated task details |
| Error Responses | Task Not Found: - Code: 404 Not Found- Content: {'message': 'Task not found'}Invalid Due Date Format: - Code: 400 Bad Request- Content: {'message': 'Invalid due date format. Please use YYYY-MM-DD.'} |
Deletes a task by its unique identifier.
| Aspect | Details |
|---|---|
| URL Params | task_id=[string]: Unique identifier of the task. |
| Success Response | Code: 200 OKContent: {'message': 'Task deleted'} |
| Error Response | Code: 404 Not FoundContent: {'message': 'Task not found'} |
- Due Date in ISO Format: To maintain a standard and internationally recognized date format.
- Sorting by Due Date: Enhances usability by allowing users to view tasks in order of their deadlines.
- Limiting Task Retrieval: Provides control over data load, useful in scenarios with many tasks.
- Make sure you have Python and Flask installed on your system.
- Navigate to the directory containing your Flask application file, app.py, and run the application via
python app.py. This will start the Flask server onhttp://localhost:5000.
The app_test.py script contains unit tests for the Flask To-Do List API. To run these tests, execute the test script using Python's unittest module via the command python -m unittest app_test or python3 -m unittest app_test depending on your version.
Test Script Details
app_test.pyincludes tests for creating tasks (both valid and invalid due dates), retrieving tasks, updating tasks, and deleting tasks.- The script utilizes the Flask testing client to simulate requests to the API and assert the responses.
- Get a List of All Tasks:
curl -X GET http://localhost:5000/tasks
- Get a Specific Task by Its Unique Identifier:
Replace <task_id> with the actual ID of the task:
curl -X GET http://localhost:5000/tasks/<task_id>
- Create a New Task:
curl -X POST http://localhost:5000/tasks \
-H "Content-Type: application/json" \
-d '{"name": "New Task", "description": "New Task Description", "due_date": "2023-01-01"}'
- Update an Existing Task:
Replace <task_id> with the ID of the task:
curl -X PUT http://localhost:5000/tasks/<task_id> \
-H "Content-Type: application/json" \
-d '{"name": "Updated Task", "description": "Updated Description", "completion_status": true, "due_date": "2023-01-05"}'
- Delete a Task by Its Unique Identifier:
Replace <task_id> with the ID of the task:
curl -X DELETE http://localhost:5000/tasks/<task_id>
- Get Tasks with Sorting by Due Date (Ascending/Descending):
For ascending order:
curl -X GET "http://localhost:5000/tasks?sort_by_date=asc"
For descending order:
curl -X GET "http://localhost:5000/tasks?sort_by_date=desc"
- Get Tasks with a Limit on the Number of Results: Replace with the number of tasks you want to retrieve:
curl -X GET "http://localhost:5000/tasks?limit=<number>"
The To-Do List API works well for basic operations but can be enhanced in several ways:
Database Integration
- Current State: Tasks are stored in a Python dictionary.
- Improvement: Implement a database like SQLite or PostgreSQL for persistent and scalable data storage.
User Authentication and Authorization
- Need: To secure the API and allow user-specific task management.
- Solution: Implement authentication mechanisms and access controls.