Skip to content

ChristianJodice/Summative-Lab-Python-REST-API-with-Flask--Inventory-Management-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Inventory Management System

A Flask-based REST API with CLI interface for managing inventory items, integrated with the OpenFoodFacts API for product information.

Features

  • 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

Project Structure

├── app.py              # Flask REST API server
├── cli.py              # Command-line interface
├── test_app.py         # Unit tests
├── requirements.txt    # Python dependencies
└── README.md          # This file

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd inventory-management-system
  2. Create a virtual environment:

    python -m venv venv
    source venv/bin/activate  # On Windows: venv\Scripts\activate
  3. Install dependencies:

    pip install -r requirements.txt

Usage

Starting the API Server

python app.py

The API will be available at http://localhost:5000

Using the CLI Interface

python cli.py

The CLI provides an interactive menu with the following options:

  1. List all inventory - View all items in the inventory
  2. View item details - Get detailed information about a specific item
  3. Add new item - Add a new product to the inventory
  4. Update item - Modify existing item details
  5. Delete item - Remove an item from inventory
  6. Fetch external product - Search and add products from OpenFoodFacts API
  7. Health check - Verify API connectivity
  8. Exit - Close the application

API Endpoints

Inventory Management

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

External API Integration

Method Endpoint Description
POST /inventory/fetch-external Fetch product from OpenFoodFacts API

Utility

Method Endpoint Description
GET /health Health check endpoint

API Examples

Get All Inventory Items

curl -X GET http://localhost:5000/inventory

Response:

{
  "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"
    }
  ]
}

Add New Item

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"
  }'

Update Item

curl -X PATCH http://localhost:5000/inventory/1 \
  -H "Content-Type: application/json" \
  -d '{
    "price": 5.99,
    "stock_quantity": 45
  }'

Fetch External Product

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
  }'

Data Model

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"
}

External API Integration

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

OpenFoodFacts API Response Format

{
  "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
    }
  }
}

Testing

Run the test suite using pytest:

pytest test_app.py -v

Test Coverage

The 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

Running Specific Tests

# 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 -v

Error Handling

The 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"
}

Development

Adding New Features

  1. API Endpoints: Add new routes in app.py
  2. CLI Commands: Extend the CLI menu in cli.py
  3. Tests: Add corresponding test cases in test_app.py
  4. Documentation: Update this README with new functionality

Code Style

  • Follow PEP 8 guidelines
  • Use type hints where appropriate
  • Include docstrings for all functions
  • Write comprehensive tests for new features

Dependencies

  • 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

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/new-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/new-feature)
  5. Create a Pull Request

Support

For questions or issues, please:

  1. Check the existing issues in the repository
  2. Create a new issue with detailed description
  3. Include error messages and steps to reproduce

Future Enhancements

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages