A Flask-based REST API with CLI interface for managing inventory items, integrated with the OpenFoodFacts API for product information.
- REST API: Complete CRUD operations for inventory management
- External API Integration: Fetch product details from OpenFoodFacts API
- CLI Interface: Command-line tool for interacting with the API
- Comprehensive Testing: Unit tests for all components
- Mock Database: In-memory storage simulation
├── app.py # Flask REST API server
├── cli.py # Command-line interface
├── test_app.py # Unit tests
├── requirements.txt # Python dependencies
└── README.md # This file
-
Clone the repository:
git clone <repository-url> cd inventory-management-system
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
python app.pyThe API will be available at http://localhost:5000
python cli.pyThe CLI provides an interactive menu with the following options:
- List all inventory - View all items in the inventory
- View item details - Get detailed information about a specific item
- Add new item - Add a new product to the inventory
- Update item - Modify existing item details
- Delete item - Remove an item from inventory
- Fetch external product - Search and add products from OpenFoodFacts API
- Health check - Verify API connectivity
- Exit - Close the application
| Method | Endpoint | Description |
|---|---|---|
| GET | /inventory |
Get all inventory items |
| GET | /inventory/<id> |
Get specific inventory item |
| POST | /inventory |
Add new inventory item |
| PATCH | /inventory/<id> |
Update inventory item |
| DELETE | /inventory/<id> |
Delete inventory item |
| Method | Endpoint | Description |
|---|---|---|
| POST | /inventory/fetch-external |
Fetch product from OpenFoodFacts API |
| Method | Endpoint | Description |
|---|---|---|
| GET | /health |
Health check endpoint |
curl -X GET http://localhost:5000/inventoryResponse:
{
"status": "success",
"count": 2,
"data": [
{
"id": "1",
"product_name": "Organic Almond Milk",
"brands": "Silk",
"barcode": "025293000000",
"price": 4.99,
"stock_quantity": 50,
"category": "Beverages",
"ingredients_text": "Filtered water, almonds, cane sugar...",
"nutrition_grade": "A",
"created_at": "2024-01-15T10:30:00Z",
"updated_at": "2024-01-15T10:30:00Z"
}
]
}curl -X POST http://localhost:5000/inventory \
-H "Content-Type: application/json" \
-d '{
"product_name": "Organic Granola",
"brands": "Nature Valley",
"price": 6.99,
"stock_quantity": 30,
"category": "Snacks",
"barcode": "123456789012"
}'curl -X PATCH http://localhost:5000/inventory/1 \
-H "Content-Type: application/json" \
-d '{
"price": 5.99,
"stock_quantity": 45
}'curl -X POST http://localhost:5000/inventory/fetch-external \
-H "Content-Type: application/json" \
-d '{
"barcode": "025293000000",
"auto_add": true,
"price": 4.99,
"stock_quantity": 20
}'Each inventory item contains the following fields:
{
"id": "string (UUID)",
"product_name": "string (required)",
"brands": "string",
"barcode": "string",
"price": "number (required)",
"stock_quantity": "number (required)",
"category": "string",
"ingredients_text": "string",
"nutrition_grade": "string",
"created_at": "ISO 8601 timestamp",
"updated_at": "ISO 8601 timestamp"
}The system integrates with the OpenFoodFacts API to fetch product information by:
- Barcode: Direct product lookup using UPC/EAN codes
- Product Name: Search functionality for product discovery
{
"status": 1,
"product": {
"product_name": "Product Name",
"brands": "Brand Name",
"code": "Barcode",
"categories": "Category",
"ingredients_text": "Ingredients list",
"nutrition_grades": "Nutrition Grade",
"image_url": "Product image URL",
"nutriments": {
"energy": 100,
"fat": 5.2
}
}
}Run the test suite using pytest:
pytest test_app.py -vThe test suite includes:
- API Endpoint Tests: All CRUD operations
- External API Tests: Mocked OpenFoodFacts API responses
- CLI Tests: Command-line interface functionality
- Error Handling Tests: Invalid inputs and edge cases
- Integration Tests: End-to-end workflow validation
# Run only API tests
pytest test_app.py::TestInventoryAPI -v
# Run only external API tests
pytest test_app.py::TestExternalAPI -v
# Run only CLI tests
pytest test_app.py::TestCLI -vThe API provides comprehensive error handling:
- 400 Bad Request: Missing required fields or invalid data
- 404 Not Found: Item not found or invalid ID
- 500 Internal Server Error: Server-side errors
All error responses follow this format:
{
"status": "error",
"message": "Descriptive error message"
}- API Endpoints: Add new routes in
app.py - CLI Commands: Extend the CLI menu in
cli.py - Tests: Add corresponding test cases in
test_app.py - Documentation: Update this README with new functionality
- Follow PEP 8 guidelines
- Use type hints where appropriate
- Include docstrings for all functions
- Write comprehensive tests for new features
- Flask 2.3.3: Web framework for the REST API
- requests 2.31.0: HTTP library for external API calls
- pytest 7.4.2: Testing framework
- pytest-flask 1.2.0: Flask testing utilities
- python-dotenv 1.0.0: Environment variable management
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Create a Pull Request
For questions or issues, please:
- Check the existing issues in the repository
- Create a new issue with detailed description
- Include error messages and steps to reproduce
- Database Integration: Replace mock storage with persistent database
- Authentication: Add user authentication and authorization
- Web Interface: Create web-based admin panel
- Bulk Operations: Support for bulk import/export
- Advanced Search: Enhanced filtering and search capabilities
- Analytics: Inventory reports and analytics dashboard