Simple Inventory Management API with FastAPI
This challenge asks you to build a basic API for managing a small inventory of items using FastAPI. You'll define endpoints for adding new items, retrieving all items, and retrieving a specific item by its ID. This exercise will solidify your understanding of FastAPI's routing, request/response handling, and data serialization.
Problem Description
You are tasked with creating a FastAPI application that manages a simple inventory. The inventory consists of items, each with an id (integer), name (string), and quantity (integer). The API should provide the following endpoints:
POST /items/: Adds a new item to the inventory. The request body should be a JSON object containing thenameandquantityof the item. The endpoint should return the newly created item (including the assignedid) as a JSON response.GET /items/: Retrieves all items in the inventory. The endpoint should return a JSON array containing all items.GET /items/{item_id}: Retrieves a specific item by itsid. The endpoint should return the item as a JSON object if found. If the item is not found, it should return a 404 Not Found error with a JSON body containing a message "Item not found".
The inventory should be stored in memory (no database required for this challenge). You can use a Python list or dictionary to represent the inventory.
Examples
Example 1:
Input (POST /items/):
{
"name": "Laptop",
"quantity": 10
}
Output:
{
"id": 1,
"name": "Laptop",
"quantity": 10
}
Explanation: A new item "Laptop" with quantity 10 is added to the inventory and assigned the ID 1. The response includes the newly created item.
Example 2:
Input (GET /items/):
(Inventory contains: [ { "id": 1, "name": "Laptop", "quantity": 10 }, { "id": 2, "name": "Mouse", "quantity": 25 } ])
Output:
[
{
"id": 1,
"name": "Laptop",
"quantity": 10
},
{
"id": 2,
"name": "Mouse",
"quantity": 25
}
]
Explanation: All items in the inventory are returned as a JSON array.
Example 3:
Input (GET /items/99):
(Inventory does not contain item with id 99)
Output:
HTTP Status: 404 Not Found
Body:
{
"message": "Item not found"
}
Explanation: The requested item with ID 99 does not exist in the inventory, so a 404 error is returned with a descriptive message.
Constraints
- The
idof each item should be a unique integer. You can auto-increment the ID when adding a new item. - The
nameshould be a non-empty string. - The
quantityshould be a non-negative integer. - The API should handle invalid input gracefully (e.g., missing fields, incorrect data types) and return appropriate error responses (e.g., 400 Bad Request). While comprehensive error handling isn't required, basic validation is expected.
- Performance is not a primary concern for this challenge. Focus on correctness and clarity.
Notes
- You'll need to install FastAPI and Uvicorn:
pip install fastapi uvicorn - Use Pydantic models to define the structure of the request and response data. This will help with data validation and serialization.
- Consider using a simple in-memory data structure (list or dictionary) to store the inventory.
- Remember to start the FastAPI application using Uvicorn:
uvicorn main:app --reload(assuming your main file is namedmain.pyand your FastAPI instance is namedapp). - Focus on implementing the core functionality of the API. Advanced features like pagination or filtering are not required.