Metadata-Version: 2.4
Name: ms-common
Version: 1.3.1
Summary: Shared utilities for Mei Sheng Textiles applications
Home-page: https://gitea.dev.meisheng.group/Mei_Sheng_Textiles/ms_common_python
Author: Mei Sheng Textiles Development Team
Author-email: Mei Sheng Textiles Development Team <dev@meisheng.group>
License: Proprietary
Project-URL: Homepage, https://gitea.dev.meisheng.group/Mei_Sheng_Textiles/ms_common_python
Project-URL: Bug Reports, https://gitea.dev.meisheng.group/Mei_Sheng_Textiles/ms_common_python/issues
Project-URL: Source, https://gitea.dev.meisheng.group/Mei_Sheng_Textiles/ms_common_python
Keywords: fastapi,microservices,textiles,manufacturing
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: Other/Proprietary License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Framework :: FastAPI
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Description-Content-Type: text/markdown
Requires-Dist: fastapi>=0.100.0
Requires-Dist: httpx>=0.24.0
Requires-Dist: sqlalchemy>=2.0.0
Requires-Dist: pydantic>=2.0.0
Requires-Dist: python-multipart>=0.0.6
Requires-Dist: faststream>0.6.1
Provides-Extra: dev
Requires-Dist: pytest>=7.0.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "dev"
Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
Requires-Dist: black>=23.0.0; extra == "dev"
Requires-Dist: isort>=5.12.0; extra == "dev"
Requires-Dist: flake8>=6.0.0; extra == "dev"
Requires-Dist: mypy>=1.0.0; extra == "dev"
Provides-Extra: postgres
Requires-Dist: psycopg2-binary>=2.9.0; extra == "postgres"
Requires-Dist: asyncpg>=0.28.0; extra == "postgres"
Provides-Extra: consul
Requires-Dist: python-consul>=1.1.0; extra == "consul"
Provides-Extra: messaging
Requires-Dist: faststream[kafka]>=0.5.0; extra == "messaging"
Dynamic: author
Dynamic: home-page
Dynamic: requires-python

# MS Common Python Package

Shared utilities and services for Mei Sheng Textiles applications.

## Overview

This package provides common functionality for the dye house application ecosystem:
- DH_Production_Manager
- MS_QC_DB
- dye_process_manager
- QR_code

## Features

- **API Key Management**: Abstract interface with PostgreSQL, SQLite, and config-based implementations
- **Service Discovery**: Consul-based service discovery with fallbacks
- **Inter-Service Communication**: Enhanced HTTP client with field mapping
- **Monitoring**: FastAPI health check and monitoring endpoints
- **Shared Utilities**: Encryption, validation, and common patterns

## Installation

### From Git Repository (Recommended)

```bash
pip install git+ssh://git@gitea.dev.meisheng.group:2222/Mei_Sheng_Textiles/ms_common_python.git@v1.0.0
```

### In Docker Base Image

```dockerfile
FROM python:3.11-slim
RUN pip install git+ssh://git@gitea.dev.meisheng.group:2222/Mei_Sheng_Textiles/ms_common_python.git@v1.0.0
```

## Quick Start

### API Key Service

```python
from ms_common import create_api_key_service

# PostgreSQL implementation (DH_Production_Manager)
api_key_service = create_api_key_service(
    'postgres',
    session_factory=get_async_session,
    user_model=User,
    api_key_model=ApiKey
)

# SQLite implementation (MS_QC_DB)
api_key_service = create_api_key_service(
    'sqlite',
    db_path='/app/data/api_keys.db'
)

# Config implementation (dye_process_manager)
api_key_service = create_api_key_service(
    'config',
    config_keys={'admin': 'your-api-key'}
)

# Use the service
key_info = await api_key_service.validate_api_key(api_key)
```

### Service Discovery

```python
from ms_common.service_client import create_service_client

# Create enhanced service client with field mapping
client = create_service_client("dh-production-manager")

# Make requests with automatic field transformation
response = await client.post("ms-qc-db", "/api/v1/lots", json={
    "lot": "25D4567",  # Automatically mapped to target service format
    "contract": "MS2024001"
})
```

### Monitoring Endpoints

```python
from fastapi import FastAPI
from ms_common.monitoring.endpoints import create_monitoring_router

app = FastAPI()

# Add health checks and monitoring
monitoring_router = create_monitoring_router("my-service")
app.include_router(monitoring_router, prefix="/api/v1/monitoring")
```

## Architecture

### Database Independence

Each application can use its existing database schema:

```python
# DH_Production_Manager - Uses existing PostgreSQL models
from app.models.postgres_models import User, ApiKey

# MS_QC_DB - Uses SQLite
# No model dependency, creates tables automatically

# dye_process_manager - Uses environment config
# No database required
```

### Service Communication

```python
# Automatic field mapping between services
# lot <-> lot_id <-> lotid transformations happen automatically

client = create_service_client("source-service")
response = await client.get("target-service", "/api/data")
# Field names automatically transformed for compatibility
```

## Development

### Setting up for Development

```bash
git clone ssh://git@gitea.dev.meisheng.group:2222/Mei_Sheng_Textiles/ms_common_python.git
cd ms_common_python
pip install -e .
pip install -r requirements-dev.txt
```

### Running Tests

```bash
pytest tests/
pytest --cov=ms_common tests/
```

### Building Package

```bash
python setup.py sdist bdist_wheel
```

## Version History

- **v1.0.0** - Initial release with API key management, service discovery, and monitoring
- **v0.1.0** - Development preview

## License

Internal use only - Mei Sheng Textiles Ltd.
