Hone logo
Hone
Problems

Building a Simple To-Do List API with FastAPI

This challenge focuses on creating a basic but functional API for managing a to-do list using FastAPI, a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. You will learn to define API endpoints, handle different HTTP methods, and process JSON data.

Problem Description

Your task is to implement a RESTful API for a simple to-do list application. The API should allow users to perform the following operations:

  1. Create a new to-do item: Add a new task to the list.
  2. Read all to-do items: Retrieve all tasks currently in the list.
  3. Read a specific to-do item: Retrieve a single task by its unique ID.
  4. Update a to-do item: Modify an existing task's description or completion status.
  5. Delete a to-do item: Remove a task from the list.

You will need to define Pydantic models to represent your to-do items and their attributes.

Key Requirements:

  • Use FastAPI to create the API endpoints.
  • Implement endpoints for GET, POST, PUT, and DELETE requests.
  • Store the to-do items in memory (no database persistence required for this challenge).
  • Each to-do item should have a unique integer ID, a string title, and a boolean completed status (defaulting to False for new items).
  • The API should return JSON responses.

Expected Behavior:

  • When a new to-do is created, it should be assigned a unique, auto-incrementing ID.
  • When retrieving all to-dos, a list of to-do objects should be returned.
  • When retrieving a specific to-do, if it exists, return the to-do object; otherwise, return a 404 Not Found error.
  • When updating a to-do, modify the specified fields and return the updated to-do object. If the to-do doesn't exist, return a 404.
  • When deleting a to-do, remove it from the list. If it doesn't exist, return a 404.

Edge Cases:

  • Handling requests for non-existent to-do IDs.
  • Ensuring completed status is correctly toggled or set.
  • Validating input data for new and updated to-do items.

Examples

Example 1: Creating a To-Do Item

Request: POST /todos/ Body:

{
  "title": "Buy groceries"
}

Response:

{
  "id": 1,
  "title": "Buy groceries",
  "completed": false
}

Explanation: A new to-do item with title: "Buy groceries" and completed: false is created. It is assigned ID 1.

Example 2: Getting All To-Do Items

Request: GET /todos/

Response (assuming Example 1 was executed):

[
  {
    "id": 1,
    "title": "Buy groceries",
    "completed": false
  }
]

Explanation: Retrieves all to-do items currently stored in memory.

Example 3: Getting a Specific To-Do Item

Request: GET /todos/1

Response:

{
  "id": 1,
  "title": "Buy groceries",
  "completed": false
}

Explanation: Retrieves the to-do item with ID 1.

Example 4: Updating a To-Do Item

Request: PUT /todos/1 Body:

{
  "title": "Buy organic groceries",
  "completed": true
}

Response:

{
  "id": 1,
  "title": "Buy organic groceries",
  "completed": true
}

Explanation: The to-do item with ID 1 is updated with a new title and marked as completed.

Example 5: Deleting a To-Do Item

Request: DELETE /todos/1

Response:

{
  "message": "To-do item with id 1 deleted"
}

Explanation: The to-do item with ID 1 is successfully removed.

Example 6: Handling Non-existent ID

Request: GET /todos/99

Response:

{
  "detail": "To-do item with id 99 not found"
}

Explanation: A 404 Not Found error is returned because no to-do item with ID 99 exists.

Constraints

  • The id for to-do items will be a positive integer, starting from 1.
  • The title will be a non-empty string.
  • The completed status will be a boolean.
  • The in-memory storage can be a Python list or dictionary.
  • The API should respond within reasonable timeframes for typical API requests (e.g., < 200ms).

Notes

  • Familiarize yourself with FastAPI's path operation decorators (e.g., @app.get, @app.post).
  • Consider using Pydantic models for data validation and serialization.
  • You'll need a way to manage the in-memory list and assign unique IDs. A simple counter will suffice.
  • Remember to import necessary components from fastapi and pydantic.
  • Use HTTPException from fastapi to return appropriate HTTP status codes and error messages for non-existent items.
Loading editor...
python