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-reload6. 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_cacheparameter 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 apiAccess 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.pdfCreate 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.pdfTesting
Automated Tests
# Verify API setup
poetry run python test_api_setup.py
# Run example client
poetry run python example_api_client.pyManual Testing
- Start server:
make api-dev - Open: http://localhost:8000/docs
- Click “Try it out” on any endpoint
- Fill parameters and click “Execute”
Integration Points
The API is a thin wrapper around existing pipeline functions:
fetch_raw_data()fromfetch.pyprepare_data()fromprepare.py- export_plot() and create_plotlist() from
plot.py render_pdf()fromrender_pdf.py- create_directories() from
utils.py - MetaRepository from
meta_repository.py
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:8000Docker
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_passwordBenefits
- Programmatic Access: Use pyreporter from any language
- Web Integration: Embed in web applications
- Microservice Architecture: Deploy as independent service
- API-First: Build UIs on top of the API
- Testing: Easier automated testing via HTTP
- Monitoring: Standard HTTP metrics and logging
Future Enhancements
Potential additions for future versions:
- Authentication: JWT or API key authentication
- Rate Limiting: Prevent abuse
- Async Processing: Background job queue for long reports
- WebSockets: Real-time progress updates
- Batch Operations: Generate multiple reports in one request
- Caching Headers: HTTP-level caching
- Metrics: Prometheus endpoints
- 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 registeredNext Steps
- Start the server:
make api-dev - Visit: http://localhost:8000/docs
- Try the interactive API documentation
- Run example client:
poetry run python example_api_client.py - Integrate with your applications!