FastAPI Service Layer Implementation Summary

Overview

Added a complete FastAPI service layer to pyreporter, providing REST API endpoints for all pipeline stages.

Files Created

1. /pyreporter/api.py (13.6 KB)

Main FastAPI application with the following endpoints:

  • GET / - API root with service information
  • GET /health - Health check endpoint
  • POST /api/v1/raw-data - Fetch raw survey data
  • POST /api/v1/prepared-data - Prepare plot-ready data
  • POST /api/v1/plot - Generate a single plot (returns PDF)
  • POST /api/v1/report - Create complete PDF report
  • GET /api/v1/plots/list - List available plots for a configuration

2. /API_README.md (8.2 KB)

Comprehensive API documentation including: - Quick start guide - Detailed endpoint documentation - Request/response examples - cURL and Python examples - Production deployment instructions - Docker configuration - Error handling guide

3. /test_api_setup.py (3.0 KB)

Automated test script to verify: - All required packages are installed - API module imports correctly - All endpoints are properly registered

4. /example_api_client.py (5.5 KB)

Example client demonstrating: - Health check - Listing available plots - Fetching raw data - Preparing data - Generating plots - Creating reports

5. Updated /Makefile

Added API-related commands:

make api      # Start production server
make api-dev  # Start development server with auto-reload

6. Updated /README.md

Added API documentation section with: - Quick start for API usage - Link to detailed API_README.md - Common API workflows - Testing instructions

7. Updated /pyproject.toml

Added dependencies: - fastapi (>=0.115.0,<0.116.0) - uvicorn[standard] (>=0.32.0,<0.33.0) - pydantic (>=2.10.0,<3.0.0)

API Architecture

Client Request
    ↓
FastAPI Layer (api.py)
    ↓
┌────────────────────────────────────┐
│  Existing pyreporter modules       │
├────────────────────────────────────┤
│  fetch.py     → Raw data           │
│  prepare.py   → Plot-ready data    │
│  plot.py      → Chart generation   │
│  render_pdf.py → PDF assembly      │
└────────────────────────────────────┘
    ↓
Response (JSON or PDF)

Key Features

1. Request Validation

  • Uses Pydantic models for type-safe request validation
  • Automatic parameter validation and error messages
  • Clear field descriptions and examples

2. Response Models

  • Structured JSON responses for data endpoints
  • File downloads for plot/report endpoints
  • Consistent error handling

3. Caching Support

  • Reuses existing cache infrastructure
  • use_cache parameter on all endpoints
  • Cache invalidation via make clean-cache

4. Error Handling

  • Proper HTTP status codes (200, 404, 500)
  • Detailed error messages
  • Exception handling at all levels

5. Documentation

  • Auto-generated OpenAPI/Swagger docs at /docs
  • ReDoc alternative at /redoc
  • Interactive API testing interface

Usage Examples

Start Server

# Development mode (auto-reload)
make api-dev

# Production mode
make api

Access Documentation

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

Example Requests

Fetch Raw Data

curl -X POST http://localhost:8000/api/v1/raw-data \
  -H "Content-Type: application/json" \
  -d '{"snr": "0001", "stype": "gy", "audience": "sus"}'

Generate Plot

curl -X POST http://localhost:8000/api/v1/plot \
  -H "Content-Type: application/json" \
  -d '{"snr": "0001", "stype": "gy", "audience": "sus", "plot_name": "A12"}' \
  --output plot.pdf

Create Report

curl -X POST http://localhost:8000/api/v1/report \
  -H "Content-Type: application/json" \
  -d '{"snr": "0001", "stype": "gy", "audience": "sus", "year": "2025"}' \
  --output report.pdf

Testing

Automated Tests

# Verify API setup
poetry run python test_api_setup.py

# Run example client
poetry run python example_api_client.py

Manual Testing

  1. Start server: make api-dev
  2. Open: http://localhost:8000/docs
  3. Click “Try it out” on any endpoint
  4. Fill parameters and click “Execute”

Integration Points

The API is a thin wrapper around existing pipeline functions:

No changes were made to existing pipeline code - the API reuses everything as-is.

Production Deployment

Using Gunicorn

poetry run gunicorn pyreporter.api:app \
  --workers 4 \
  --worker-class uvicorn.workers.UvicornWorker \
  --bind 0.0.0.0:8000

Docker

See API_README.md for complete Dockerfile

Environment

Requires same .env configuration as CLI:

LIME_API_URL=https://your-limesurvey.com/admin/remotecontrol
LIME_USERNAME=your_username
LIME_PASSWORD=your_password

Benefits

  1. Programmatic Access: Use pyreporter from any language
  2. Web Integration: Embed in web applications
  3. Microservice Architecture: Deploy as independent service
  4. API-First: Build UIs on top of the API
  5. Testing: Easier automated testing via HTTP
  6. Monitoring: Standard HTTP metrics and logging

Future Enhancements

Potential additions for future versions:

  1. Authentication: JWT or API key authentication
  2. Rate Limiting: Prevent abuse
  3. Async Processing: Background job queue for long reports
  4. WebSockets: Real-time progress updates
  5. Batch Operations: Generate multiple reports in one request
  6. Caching Headers: HTTP-level caching
  7. Metrics: Prometheus endpoints
  8. Admin API: Cache management, system status

Compatibility

  • ✅ Python 3.14+
  • ✅ All existing CLI functionality preserved
  • ✅ Same caching system
  • ✅ Same metadata repository
  • ✅ Same environment configuration

Installation Verified

 FastAPI installed successfully
 Uvicorn installed successfully
 Pydantic installed successfully
 API module imported successfully
 All 7 endpoints registered

Next Steps

  1. Start the server: make api-dev
  2. Visit: http://localhost:8000/docs
  3. Try the interactive API documentation
  4. Run example client: poetry run python example_api_client.py
  5. Integrate with your applications!