osbot-fast-api


Nameosbot-fast-api JSON
Version 0.23.0 PyPI version JSON
download
home_pageNone
SummaryOWASP Security Bot - Fast API
upload_time2025-09-01 21:29:19
maintainerNone
docs_urlNone
authorDinis Cruz
requires_python<4.0,>=3.11
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # OSBot-Fast-API

![Current Release](https://img.shields.io/badge/release-v0.23.0-blue)
![Python](https://img.shields.io/badge/python-3.8+-green)
![FastAPI](https://img.shields.io/badge/FastAPI-0.100+-red)
![Type-Safe](https://img.shields.io/badge/Type--Safe-โœ“-brightgreen)
![AWS Lambda](https://img.shields.io/badge/AWS_Lambda-Ready-orange)

A Type-Safe wrapper around FastAPI that provides strong typing, comprehensive middleware support, HTTP event tracking, and seamless AWS Lambda integration through Mangum.

## โœจ Key Features

- **๐Ÿ” Type-Safe First**: Automatic bidirectional conversion between Type_Safe classes and Pydantic BaseModels
- **๐Ÿ›ก๏ธ Built-in Middleware**: API key validation, CORS, disconnect detection, and HTTP event tracking
- **๐Ÿ“Š HTTP Event System**: Comprehensive request/response tracking with configurable storage
- **๐Ÿš€ AWS Lambda Ready**: Direct integration with Mangum for serverless deployment
- **๐Ÿงช Testing Utilities**: Built-in test server with Type-Safe support
- **๐Ÿ”„ Auto-conversion**: Seamless Type_Safe โ†” BaseModel โ†” Dataclass conversions
- **๐Ÿ“ Route Organization**: Clean route structure with automatic path generation

## ๐Ÿ“ฆ Installation

```bash
pip install osbot-fast-api
```

## ๐Ÿš€ Quick Start

### Basic Application

```python
from osbot_fast_api.api.Fast_API import Fast_API
from osbot_fast_api.api.routes.Fast_API__Routes import Fast_API__Routes
from osbot_utils.type_safe.Type_Safe import Type_Safe


# Define Type-Safe schema
class User(Type_Safe):
    username: str
    email: str
    age: int


# Create routes
class Routes_Users(Fast_API__Routes):
    tag = 'users'

    def create_user(self, user: User):
        # user is automatically converted from BaseModel to Type_Safe
        return {'created': user.username}

    def get_user__id(self, id: str):  # Becomes /users/get-user/{id}
        return {'user_id': id}

    def setup_routes(self):
        self.add_route_post(self.create_user)
        self.add_route_get(self.get_user__id)


# Setup application
fast_api = Fast_API(enable_cors=True)
fast_api.setup()
fast_api.add_routes(Routes_Users)

# Get FastAPI app instance
app = fast_api.app()
```

### With Middleware & Authentication

```python
import os

# Configure API key authentication
os.environ['FAST_API__AUTH__API_KEY__NAME'] = 'X-API-Key'
os.environ['FAST_API__AUTH__API_KEY__VALUE'] = 'your-secret-key'

# Create app with middleware
fast_api = Fast_API(
    enable_cors=True,      # Enable CORS support
    enable_api_key=True,   # Enable API key validation
    default_routes=True    # Add /status, /version routes
)

# Configure HTTP event tracking
fast_api.http_events.max_requests_logged = 100
fast_api.http_events.clean_data = True  # Sanitize sensitive headers

fast_api.setup()
```

## ๐Ÿ—๏ธ Architecture

OSBot-Fast-API extends FastAPI with a comprehensive Type-Safe layer and monitoring capabilities:

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   Your Application                  โ”‚
โ”‚                                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚  โ”‚ Type-Safe    โ”‚  โ”‚   Routes     โ”‚  โ”‚  Events  โ”‚   โ”‚
โ”‚  โ”‚  Schemas     โ”‚  โ”‚   Classes    โ”‚  โ”‚ Handlers โ”‚   โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  OSBot-Fast-API                     โ”‚
โ”‚                                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚          Type Conversion System            โ”‚     โ”‚
โ”‚  โ”‚   Type_Safe โ†” BaseModel โ†” Dataclass        โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚           Middleware Pipeline              โ”‚     โ”‚
โ”‚  โ”‚  Disconnect โ†’ Events โ†’ CORS โ†’ API Key      โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ”‚                                                     โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”‚
โ”‚  โ”‚         HTTP Event Tracking System         โ”‚     โ”‚
โ”‚  โ”‚   Request/Response/Traces/Monitoring       โ”‚     โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     FastAPI                         โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## ๐Ÿ” Type-Safe Integration

OSBot-Fast-API automatically converts between Type_Safe classes and Pydantic BaseModels:

```python
from osbot_utils.type_safe.Type_Safe import Type_Safe
from typing import List, Optional

# Define Type-Safe schemas (not Pydantic!)
class Address(Type_Safe):
    street: str
    city: str
    country: str

class Person(Type_Safe):
    name: str
    age: int
    email: Optional[str] = None
    addresses: List[Address] = []

# Use directly in routes - automatic conversion happens
class Routes_People(Fast_API__Routes):
    tag = 'people'
    
    def create_person(self, person: Person):
        # person is Type_Safe instance, not BaseModel
        # Full type validation and conversion handled automatically
        return person  # Automatically converted back to JSON
    
    def setup_routes(self):
        self.add_route_post(self.create_person)
```

## ๐Ÿ“Š HTTP Event Tracking

Built-in comprehensive request/response tracking:

```python
# Configure event tracking
fast_api.http_events.max_requests_logged = 100
fast_api.http_events.clean_data = True  # Sanitize sensitive data
fast_api.http_events.trace_calls = True  # Enable execution tracing (debug)

# Add event callbacks
def on_request(event):
    print(f"Request: {event.http_event_request.path}")

def on_response(response, event):
    print(f"Response: {event.http_event_response.status_code}")
    print(f"Duration: {event.http_event_request.duration}s")

fast_api.http_events.callback_on_request = on_request
fast_api.http_events.callback_on_response = on_response
```

## ๐Ÿ›ก๏ธ Middleware Stack

Built-in middleware pipeline (in execution order):

1. **Detect_Disconnect**: Monitor client disconnections
2. **Http_Request**: Event tracking and logging
3. **CORS**: Cross-origin resource sharing
4. **API_Key_Check**: Header/cookie API key validation

### Custom Middleware

```python
class Custom_Fast_API(Fast_API):
    def setup_middlewares(self):
        super().setup_middlewares()  # Add default middleware
        
        @self.app().middleware("http")
        async def add_process_time(request: Request, call_next):
            import time
            start = time.time()
            response = await call_next(request)
            response.headers["X-Process-Time"] = str(time.time() - start)
            return response
```

## ๐Ÿงช Testing

Built-in test server with Type-Safe support:

```python
from osbot_fast_api.utils.Fast_API_Server import Fast_API_Server

def test_api():
    fast_api = Fast_API()
    fast_api.setup()
    fast_api.add_routes(Routes_Users)
    
    with Fast_API_Server(app=fast_api.app()) as server:
        # Test with Type-Safe object
        user_data = {'username': 'alice', 'email': 'alice@example.com', 'age': 30}
        response = server.requests_post('/users/create-user', data=user_data)
        
        assert response.status_code == 200
        assert response.json()['created'] == 'alice'
```

## ๐Ÿš€ AWS Lambda Deployment

```python
from mangum import Mangum
from osbot_fast_api.api.Fast_API import Fast_API

# Create and setup application
fast_api = Fast_API()
fast_api.setup()
fast_api.add_routes(Routes_Users)

# Create Lambda handler
app = fast_api.app()
handler = Mangum(app)

def lambda_handler(event, context):
    return handler(event, context)
```

## ๐Ÿ“ Project Structure

```
osbot_fast_api/
โ”œโ”€โ”€ api/
โ”‚   โ”œโ”€โ”€ Fast_API.py                 # Main FastAPI wrapper
โ”‚   โ”œโ”€โ”€ Fast_API__Routes.py          # Route organization base class
โ”‚   โ”œโ”€โ”€ Fast_API__Http_Event*.py    # Event tracking components
โ”‚   โ””โ”€โ”€ middlewares/                # Built-in middleware
โ”œโ”€โ”€ utils/
โ”‚   โ”œโ”€โ”€ type_safe/                  # Type conversion system
โ”‚   โ”‚   โ”œโ”€โ”€ Type_Safe__To__BaseModel.py
โ”‚   โ”‚   โ”œโ”€โ”€ BaseModel__To__Type_Safe.py
โ”‚   โ”‚   โ””โ”€โ”€ ...
โ”‚   โ”œโ”€โ”€ Fast_API_Server.py          # Test server
โ”‚   โ””โ”€โ”€ Fast_API_Utils.py           # Utilities
โ””โ”€โ”€ examples/                        # Usage examples
```

## ๐Ÿ“š Documentation

Comprehensive documentation is available in the [`/docs`](./docs) folder:

- [๐Ÿ“– Main Documentation](./docs/README.md)
- [๐Ÿ—๏ธ Architecture Overview](./docs/architecture/osbot-fast-api-architecture.md)
- [๐Ÿ” Type-Safe Integration](./docs/type-safe/type-safe-integration.md)
- [๐Ÿ“Š HTTP Events System](./docs/features/http-events-system.md)
- [๐Ÿ›ก๏ธ Middleware Stack](./docs/features/middleware-stack.md)
- [๐Ÿš€ Quick Start Guide](./docs/guides/quick-start.md)
- [๐Ÿค– LLM Prompts](./docs/guides/llm-prompts.md)
- [๐Ÿงช Testing Guide](./docs/guides/testing.md)

## ๐ŸŽฏ Key Benefits

### For Developers
- **Type Safety**: Catch errors at development time with Type_Safe validation
- **Less Boilerplate**: Convention over configuration approach
- **Auto-conversion**: Seamless type conversions at API boundaries
- **Built-in Testing**: Integrated test server and utilities

### For Production
- **Monitoring**: Comprehensive HTTP event tracking
- **Security**: Built-in API key validation and header sanitization
- **Performance**: Cached type conversions and efficient middleware
- **AWS Ready**: Direct Lambda integration with Mangum

### For Teams
- **Organized Code**: Clear separation with Fast_API__Routes classes
- **Consistent Patterns**: Standardized route naming and structure
- **Easy Testing**: Type-Safe test utilities
- **Documentation**: Auto-generated OpenAPI/Swagger docs

## ๐Ÿ”ง Advanced Features

### Route Path Generation
- `get_users()` โ†’ `/get-users`
- `get_user__id()` โ†’ `/get-user/{id}`
- `user__id_posts__post_id()` โ†’ `/user/{id}/posts/{post_id}`

### Type-Safe Primitives
```python
from osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive

class Email(Type_Safe__Primitive, str):
    def __new__(cls, value):
        if '@' not in value:
            raise ValueError("Invalid email")
        return super().__new__(cls, value)
```

### Event Access in Routes
```python
from fastapi import Request

def get_request_info(self, request: Request):
    return {
        'event_id': str(request.state.request_id),
        'thread_id': request.state.request_data.http_event_info.thread_id
    }
```

## ๐Ÿค Contributing

Contributions are welcome! Please check the [documentation](./docs) for architecture details and patterns.

## ๐Ÿ“„ License

This project is licensed under the Apache 2.0 License.

## ๐Ÿ”— Related Projects

- [OSBot-Utils](https://github.com/owasp-sbot/OSBot-Utils) - Core Type-Safe implementation
- [OSBot-AWS](https://github.com/owasp-sbot/OSBot-AWS) - AWS utilities
- [OSBot-Fast-API-Serverless](https://github.com/owasp-sbot/OSBot-Fast-API-Serverless) - Serverless extensions

## ๐Ÿ’ก Examples

For more examples, see:
- [Basic FastAPI application](./docs/guides/quick-start.md)
- [Type-Safe schemas](./docs/type-safe/type-safe-integration.md)
- [Testing patterns](./docs/guides/testing.md)
- [Real-world usage in MGraph-AI](./docs/type-safe/type-safe-integration.md#real-world-example-mgraph-ai-service)

---

**Built with โค๏ธ using Type-Safe principles for robust, maintainable APIs**

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "osbot-fast-api",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.11",
    "maintainer_email": null,
    "keywords": null,
    "author": "Dinis Cruz",
    "author_email": "dinis.cruz@owasp.org",
    "download_url": "https://files.pythonhosted.org/packages/38/87/10cf28d7fafdb2fbf185cdebd277dcf5b828458391c2d5e3c8ea6a5490a6/osbot_fast_api-0.23.0.tar.gz",
    "platform": null,
    "description": "# OSBot-Fast-API\n\n![Current Release](https://img.shields.io/badge/release-v0.23.0-blue)\n![Python](https://img.shields.io/badge/python-3.8+-green)\n![FastAPI](https://img.shields.io/badge/FastAPI-0.100+-red)\n![Type-Safe](https://img.shields.io/badge/Type--Safe-\u2713-brightgreen)\n![AWS Lambda](https://img.shields.io/badge/AWS_Lambda-Ready-orange)\n\nA Type-Safe wrapper around FastAPI that provides strong typing, comprehensive middleware support, HTTP event tracking, and seamless AWS Lambda integration through Mangum.\n\n## \u2728 Key Features\n\n- **\ud83d\udd10 Type-Safe First**: Automatic bidirectional conversion between Type_Safe classes and Pydantic BaseModels\n- **\ud83d\udee1\ufe0f Built-in Middleware**: API key validation, CORS, disconnect detection, and HTTP event tracking\n- **\ud83d\udcca HTTP Event System**: Comprehensive request/response tracking with configurable storage\n- **\ud83d\ude80 AWS Lambda Ready**: Direct integration with Mangum for serverless deployment\n- **\ud83e\uddea Testing Utilities**: Built-in test server with Type-Safe support\n- **\ud83d\udd04 Auto-conversion**: Seamless Type_Safe \u2194 BaseModel \u2194 Dataclass conversions\n- **\ud83d\udcdd Route Organization**: Clean route structure with automatic path generation\n\n## \ud83d\udce6 Installation\n\n```bash\npip install osbot-fast-api\n```\n\n## \ud83d\ude80 Quick Start\n\n### Basic Application\n\n```python\nfrom osbot_fast_api.api.Fast_API import Fast_API\nfrom osbot_fast_api.api.routes.Fast_API__Routes import Fast_API__Routes\nfrom osbot_utils.type_safe.Type_Safe import Type_Safe\n\n\n# Define Type-Safe schema\nclass User(Type_Safe):\n    username: str\n    email: str\n    age: int\n\n\n# Create routes\nclass Routes_Users(Fast_API__Routes):\n    tag = 'users'\n\n    def create_user(self, user: User):\n        # user is automatically converted from BaseModel to Type_Safe\n        return {'created': user.username}\n\n    def get_user__id(self, id: str):  # Becomes /users/get-user/{id}\n        return {'user_id': id}\n\n    def setup_routes(self):\n        self.add_route_post(self.create_user)\n        self.add_route_get(self.get_user__id)\n\n\n# Setup application\nfast_api = Fast_API(enable_cors=True)\nfast_api.setup()\nfast_api.add_routes(Routes_Users)\n\n# Get FastAPI app instance\napp = fast_api.app()\n```\n\n### With Middleware & Authentication\n\n```python\nimport os\n\n# Configure API key authentication\nos.environ['FAST_API__AUTH__API_KEY__NAME'] = 'X-API-Key'\nos.environ['FAST_API__AUTH__API_KEY__VALUE'] = 'your-secret-key'\n\n# Create app with middleware\nfast_api = Fast_API(\n    enable_cors=True,      # Enable CORS support\n    enable_api_key=True,   # Enable API key validation\n    default_routes=True    # Add /status, /version routes\n)\n\n# Configure HTTP event tracking\nfast_api.http_events.max_requests_logged = 100\nfast_api.http_events.clean_data = True  # Sanitize sensitive headers\n\nfast_api.setup()\n```\n\n## \ud83c\udfd7\ufe0f Architecture\n\nOSBot-Fast-API extends FastAPI with a comprehensive Type-Safe layer and monitoring capabilities:\n\n```\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                   Your Application                  \u2502\n\u2502                                                     \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510   \u2502\n\u2502  \u2502 Type-Safe    \u2502  \u2502   Routes     \u2502  \u2502  Events  \u2502   \u2502\n\u2502  \u2502  Schemas     \u2502  \u2502   Classes    \u2502  \u2502 Handlers \u2502   \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518   \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                        \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                  OSBot-Fast-API                     \u2502\n\u2502                                                     \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510     \u2502\n\u2502  \u2502          Type Conversion System            \u2502     \u2502\n\u2502  \u2502   Type_Safe \u2194 BaseModel \u2194 Dataclass        \u2502     \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2502\n\u2502                                                     \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510     \u2502\n\u2502  \u2502           Middleware Pipeline              \u2502     \u2502\n\u2502  \u2502  Disconnect \u2192 Events \u2192 CORS \u2192 API Key      \u2502     \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2502\n\u2502                                                     \u2502\n\u2502  \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510     \u2502\n\u2502  \u2502         HTTP Event Tracking System         \u2502     \u2502\n\u2502  \u2502   Request/Response/Traces/Monitoring       \u2502     \u2502\n\u2502  \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518     \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n                        \u2502\n\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u25bc\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\n\u2502                     FastAPI                         \u2502\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\n```\n\n## \ud83d\udd10 Type-Safe Integration\n\nOSBot-Fast-API automatically converts between Type_Safe classes and Pydantic BaseModels:\n\n```python\nfrom osbot_utils.type_safe.Type_Safe import Type_Safe\nfrom typing import List, Optional\n\n# Define Type-Safe schemas (not Pydantic!)\nclass Address(Type_Safe):\n    street: str\n    city: str\n    country: str\n\nclass Person(Type_Safe):\n    name: str\n    age: int\n    email: Optional[str] = None\n    addresses: List[Address] = []\n\n# Use directly in routes - automatic conversion happens\nclass Routes_People(Fast_API__Routes):\n    tag = 'people'\n    \n    def create_person(self, person: Person):\n        # person is Type_Safe instance, not BaseModel\n        # Full type validation and conversion handled automatically\n        return person  # Automatically converted back to JSON\n    \n    def setup_routes(self):\n        self.add_route_post(self.create_person)\n```\n\n## \ud83d\udcca HTTP Event Tracking\n\nBuilt-in comprehensive request/response tracking:\n\n```python\n# Configure event tracking\nfast_api.http_events.max_requests_logged = 100\nfast_api.http_events.clean_data = True  # Sanitize sensitive data\nfast_api.http_events.trace_calls = True  # Enable execution tracing (debug)\n\n# Add event callbacks\ndef on_request(event):\n    print(f\"Request: {event.http_event_request.path}\")\n\ndef on_response(response, event):\n    print(f\"Response: {event.http_event_response.status_code}\")\n    print(f\"Duration: {event.http_event_request.duration}s\")\n\nfast_api.http_events.callback_on_request = on_request\nfast_api.http_events.callback_on_response = on_response\n```\n\n## \ud83d\udee1\ufe0f Middleware Stack\n\nBuilt-in middleware pipeline (in execution order):\n\n1. **Detect_Disconnect**: Monitor client disconnections\n2. **Http_Request**: Event tracking and logging\n3. **CORS**: Cross-origin resource sharing\n4. **API_Key_Check**: Header/cookie API key validation\n\n### Custom Middleware\n\n```python\nclass Custom_Fast_API(Fast_API):\n    def setup_middlewares(self):\n        super().setup_middlewares()  # Add default middleware\n        \n        @self.app().middleware(\"http\")\n        async def add_process_time(request: Request, call_next):\n            import time\n            start = time.time()\n            response = await call_next(request)\n            response.headers[\"X-Process-Time\"] = str(time.time() - start)\n            return response\n```\n\n## \ud83e\uddea Testing\n\nBuilt-in test server with Type-Safe support:\n\n```python\nfrom osbot_fast_api.utils.Fast_API_Server import Fast_API_Server\n\ndef test_api():\n    fast_api = Fast_API()\n    fast_api.setup()\n    fast_api.add_routes(Routes_Users)\n    \n    with Fast_API_Server(app=fast_api.app()) as server:\n        # Test with Type-Safe object\n        user_data = {'username': 'alice', 'email': 'alice@example.com', 'age': 30}\n        response = server.requests_post('/users/create-user', data=user_data)\n        \n        assert response.status_code == 200\n        assert response.json()['created'] == 'alice'\n```\n\n## \ud83d\ude80 AWS Lambda Deployment\n\n```python\nfrom mangum import Mangum\nfrom osbot_fast_api.api.Fast_API import Fast_API\n\n# Create and setup application\nfast_api = Fast_API()\nfast_api.setup()\nfast_api.add_routes(Routes_Users)\n\n# Create Lambda handler\napp = fast_api.app()\nhandler = Mangum(app)\n\ndef lambda_handler(event, context):\n    return handler(event, context)\n```\n\n## \ud83d\udcc1 Project Structure\n\n```\nosbot_fast_api/\n\u251c\u2500\u2500 api/\n\u2502   \u251c\u2500\u2500 Fast_API.py                 # Main FastAPI wrapper\n\u2502   \u251c\u2500\u2500 Fast_API__Routes.py          # Route organization base class\n\u2502   \u251c\u2500\u2500 Fast_API__Http_Event*.py    # Event tracking components\n\u2502   \u2514\u2500\u2500 middlewares/                # Built-in middleware\n\u251c\u2500\u2500 utils/\n\u2502   \u251c\u2500\u2500 type_safe/                  # Type conversion system\n\u2502   \u2502   \u251c\u2500\u2500 Type_Safe__To__BaseModel.py\n\u2502   \u2502   \u251c\u2500\u2500 BaseModel__To__Type_Safe.py\n\u2502   \u2502   \u2514\u2500\u2500 ...\n\u2502   \u251c\u2500\u2500 Fast_API_Server.py          # Test server\n\u2502   \u2514\u2500\u2500 Fast_API_Utils.py           # Utilities\n\u2514\u2500\u2500 examples/                        # Usage examples\n```\n\n## \ud83d\udcda Documentation\n\nComprehensive documentation is available in the [`/docs`](./docs) folder:\n\n- [\ud83d\udcd6 Main Documentation](./docs/README.md)\n- [\ud83c\udfd7\ufe0f Architecture Overview](./docs/architecture/osbot-fast-api-architecture.md)\n- [\ud83d\udd10 Type-Safe Integration](./docs/type-safe/type-safe-integration.md)\n- [\ud83d\udcca HTTP Events System](./docs/features/http-events-system.md)\n- [\ud83d\udee1\ufe0f Middleware Stack](./docs/features/middleware-stack.md)\n- [\ud83d\ude80 Quick Start Guide](./docs/guides/quick-start.md)\n- [\ud83e\udd16 LLM Prompts](./docs/guides/llm-prompts.md)\n- [\ud83e\uddea Testing Guide](./docs/guides/testing.md)\n\n## \ud83c\udfaf Key Benefits\n\n### For Developers\n- **Type Safety**: Catch errors at development time with Type_Safe validation\n- **Less Boilerplate**: Convention over configuration approach\n- **Auto-conversion**: Seamless type conversions at API boundaries\n- **Built-in Testing**: Integrated test server and utilities\n\n### For Production\n- **Monitoring**: Comprehensive HTTP event tracking\n- **Security**: Built-in API key validation and header sanitization\n- **Performance**: Cached type conversions and efficient middleware\n- **AWS Ready**: Direct Lambda integration with Mangum\n\n### For Teams\n- **Organized Code**: Clear separation with Fast_API__Routes classes\n- **Consistent Patterns**: Standardized route naming and structure\n- **Easy Testing**: Type-Safe test utilities\n- **Documentation**: Auto-generated OpenAPI/Swagger docs\n\n## \ud83d\udd27 Advanced Features\n\n### Route Path Generation\n- `get_users()` \u2192 `/get-users`\n- `get_user__id()` \u2192 `/get-user/{id}`\n- `user__id_posts__post_id()` \u2192 `/user/{id}/posts/{post_id}`\n\n### Type-Safe Primitives\n```python\nfrom osbot_utils.type_safe.Type_Safe__Primitive import Type_Safe__Primitive\n\nclass Email(Type_Safe__Primitive, str):\n    def __new__(cls, value):\n        if '@' not in value:\n            raise ValueError(\"Invalid email\")\n        return super().__new__(cls, value)\n```\n\n### Event Access in Routes\n```python\nfrom fastapi import Request\n\ndef get_request_info(self, request: Request):\n    return {\n        'event_id': str(request.state.request_id),\n        'thread_id': request.state.request_data.http_event_info.thread_id\n    }\n```\n\n## \ud83e\udd1d Contributing\n\nContributions are welcome! Please check the [documentation](./docs) for architecture details and patterns.\n\n## \ud83d\udcc4 License\n\nThis project is licensed under the Apache 2.0 License.\n\n## \ud83d\udd17 Related Projects\n\n- [OSBot-Utils](https://github.com/owasp-sbot/OSBot-Utils) - Core Type-Safe implementation\n- [OSBot-AWS](https://github.com/owasp-sbot/OSBot-AWS) - AWS utilities\n- [OSBot-Fast-API-Serverless](https://github.com/owasp-sbot/OSBot-Fast-API-Serverless) - Serverless extensions\n\n## \ud83d\udca1 Examples\n\nFor more examples, see:\n- [Basic FastAPI application](./docs/guides/quick-start.md)\n- [Type-Safe schemas](./docs/type-safe/type-safe-integration.md)\n- [Testing patterns](./docs/guides/testing.md)\n- [Real-world usage in MGraph-AI](./docs/type-safe/type-safe-integration.md#real-world-example-mgraph-ai-service)\n\n---\n\n**Built with \u2764\ufe0f using Type-Safe principles for robust, maintainable APIs**\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "OWASP Security Bot - Fast API",
    "version": "0.23.0",
    "project_urls": {
        "Homepage": "https://github.com/owasp-sbot/OSBot-Fast-API",
        "Repository": "https://github.com/owasp-sbot/OSBot-Fast-API"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ea93a4bbf41dffca26991889e9e67246827ae3e44db02bbdb83f1041ec70b676",
                "md5": "cb3702b16f042ea9e5f7b06ec884e77c",
                "sha256": "64cbfe1439a3541fedc7ccf4b1b8e536310600a276ae828d63b7bcaaa5fa5e86"
            },
            "downloads": -1,
            "filename": "osbot_fast_api-0.23.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cb3702b16f042ea9e5f7b06ec884e77c",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.11",
            "size": 892188,
            "upload_time": "2025-09-01T21:29:18",
            "upload_time_iso_8601": "2025-09-01T21:29:18.281555Z",
            "url": "https://files.pythonhosted.org/packages/ea/93/a4bbf41dffca26991889e9e67246827ae3e44db02bbdb83f1041ec70b676/osbot_fast_api-0.23.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "388710cf28d7fafdb2fbf185cdebd277dcf5b828458391c2d5e3c8ea6a5490a6",
                "md5": "04216bac0edec7017dce1aec122119f8",
                "sha256": "dee0ab1889e33e87322baa39d490683765d0034bdcb42ec2a22f7dfe799bf190"
            },
            "downloads": -1,
            "filename": "osbot_fast_api-0.23.0.tar.gz",
            "has_sig": false,
            "md5_digest": "04216bac0edec7017dce1aec122119f8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.11",
            "size": 850247,
            "upload_time": "2025-09-01T21:29:19",
            "upload_time_iso_8601": "2025-09-01T21:29:19.707631Z",
            "url": "https://files.pythonhosted.org/packages/38/87/10cf28d7fafdb2fbf185cdebd277dcf5b828458391c2d5e3c8ea6a5490a6/osbot_fast_api-0.23.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-01 21:29:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "owasp-sbot",
    "github_project": "OSBot-Fast-API",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "osbot-fast-api"
}
        
Elapsed time: 1.33587s