HUB
Sign inSign up

stephengpope/ncatk2.0

By stephengpope

•Updated about 1 year ago

Image
0

543

stephengpope/ncatk2.0 repository overview

⁠FastAPI + CLI Hybrid Architecture

A proof-of-concept for write once, deploy everywhere: service functions automatically become both API endpoints AND CLI commands with zero boilerplate.

⁠Features

  • Auto-registration: Single @register_endpoint decorator creates both API route and CLI command
  • Unified JSON payloads: API and CLI accept identical JSON structures
  • Built-in webhooks: Automatic success/failure notifications
  • API key authentication: Secures API endpoints (CLI bypasses auth)
  • Type-safe: Pydantic validation for both API and CLI

⁠Installation

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env and set your API_KEY

⁠Usage

⁠API Mode

Start the FastAPI server:

uvicorn ncatoolkit.main:app --reload

Make API calls with authentication:

# Health check
curl http://localhost:8000/

# List all endpoints
curl http://localhost:8000/endpoints

# Transcribe media (no webhook)
curl -X POST http://localhost:8000/v2/media/transcribe \
  -H "X-API-Key: test-api-key-12345" \
  -H "Content-Type: application/json" \
  -d '{"media_file": "audio.mp3", "language": "en"}'

# Transcribe media with webhook notification
curl -X POST http://localhost:8000/v2/media/transcribe \
  -H "X-API-Key: test-api-key-12345" \
  -H "Content-Type: application/json" \
  -d '{
    "media_file": "audio.mp3",
    "language": "en",
    "webhook_url": "https://webhook.site/your-unique-url"
  }'
⁠CLI Mode

Run commands directly (no API key required):

# List available commands
python -m ncatoolkit.cli list-commands

# Transcribe using JSON payload (primary method)
python -m ncatoolkit.cli transcribe --json '{
  "media_file": "audio.mp3",
  "language": "en",
  "webhook_url": "https://webhook.site/your-unique-url"
}'

# Transcribe using individual flags (optional method)
python -m ncatoolkit.cli transcribe \
  --media-file audio.mp3 \
  --language en \
  --webhook-url https://webhook.site/your-unique-url

⁠Architecture

⁠Adding a New Endpoint

Simply create a service function with the decorator:

# ncatoolkit/services/your_service.py
from ncatoolkit.core.registry import register_endpoint

@register_endpoint(route="/v2/your/endpoint", method="POST")
async def your_function(param1: str, param2: str = None):
    # Your business logic
    return {"result": "success"}

This automatically creates:

  1. API route: POST /v2/your/endpoint (with auth)
  2. CLI command: python -m ncatoolkit.cli endpoint --json '{"param1":"value"}'
  3. Webhook notifications (if webhook_url provided)
⁠Project Structure
/ncatoolkit
  /core
    config.py       # Environment settings (API_KEY)
    auth.py         # API key validation
    webhook.py      # Webhook notifications
    registry.py     # Auto-registration decorator
  /services
    transcription.py  # Example service
  /schemas
    transcription.py  # Pydantic models
  main.py          # FastAPI app
  cli.py           # Typer CLI app
⁠Webhook Payload Format

Webhooks send standardized JSON:

Success:

{
  "status": "success",
  "result": {
    "transcription": "...",
    "language": "en",
    "duration": 42.5
  }
}

Error:

{
  "status": "error",
  "error": "Error message here"
}

⁠Testing

# Test webhook notifications at:
https://webhook.site

# Example test flow:
1. Get a unique webhook URL from webhook.site
2. Call API/CLI with webhook_url parameter
3. Check webhook.site for the notification

⁠Key Technologies

  • FastAPI: Modern async web framework
  • Typer: CLI framework (from FastAPI creator)
  • Pydantic: Data validation
  • httpx: Async HTTP client for webhooks
  • uvicorn: ASGI server

⁠Authentication

  • API: Requires X-API-Key header matching API_KEY in .env
  • CLI: No authentication (assumes trusted execution environment)

⁠Development

The registry pattern enables adding new endpoints with minimal code:

  1. Write async service function in /ncatoolkit/services/
  2. Add @register_endpoint decorator with route and method
  3. Import in main.py (auto-registers API route)
  4. Add CLI command in cli.py that uses the registry

Current Implementation:

  • API: Fully auto-registered from ENDPOINT_REGISTRY
  • CLI: Manual command definition (uses registry for logic)

Why CLI isn't fully auto-registered: Typer's dynamic command registration has limitations with current Python/Typer versions. The current approach provides explicit CLI commands while still leveraging the shared registry for business logic and validation.

Benefits achieved:

  • Single source of truth for business logic (@register_endpoint)
  • Shared Pydantic validation (no duplicate schemas)
  • No duplicate request/response handling code
  • Webhook logic embedded once in decorator
  • Identical JSON payloads for API and CLI
  • Type-safe validation on both entry points
Tag summary
Recent tags
Content type
Image
Digest
sha256:7f0485eb1…
Size
426 MB
Last updated
about 1 year ago
docker pull stephengpope/ncatk2.0:latest