Getting Started with AquaGen API
This guide will help you set up your development environment and get the AquaGen API running on your local machine.
๐ Prerequisitesโ
Before you begin, ensure you have the following installed:
- Python 3.8+ (Python 3.10+ recommended)
- pip (Python package manager)
- Git (for cloning the repository)
- Azure Account (for cloud services)
- Virtual Environment (recommended)
- OS: macOS, Linux, or Windows with WSL2
- RAM: Minimum 4GB (8GB recommended)
- Disk Space: At least 2GB free space
๐ง Installationโ
Step 1: Clone the Repositoryโ
git clone https://github.com/Fluxgentech/aquagenapi.git
cd aquagenapi
Step 2: Create a Virtual Environmentโ
It's recommended to use a Python virtual environment to isolate dependencies:
# Create virtual environment
python -m venv .venv
# Activate virtual environment
# On macOS/Linux:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate
Using a virtual environment prevents package conflicts and keeps your global Python installation clean.
Step 3: Install Dependenciesโ
pip install -r requirements.txt
If you encounter errors with PDF generation libraries, install these specific versions:
pip install xhtml2pdf==0.2.13
pip install reportlab==4.0.7
Step 4: Configure Azure Servicesโ
AquaGen API requires several Azure services. Create a .env file or set environment variables:
# Azure Cosmos DB
COSMOSDBENDPOINT=https://your-cosmos-account.documents.azure.com:443/
COSMOSDBKEY=your-cosmos-key
# Azure Key Vault
AZURE_CLIENT_ID=your-client-id
AZURE_TENANT_ID=your-tenant-id
AZURE_CLIENT_SECRET=your-client-secret
# Application Insights
APPLICATIONINSIGHTS_CONNECTION_STRING=your-insights-connection-string
# Environment
ENVIRONMENT=development
Never commit secrets or API keys to version control. Always use Azure Key Vault in production.
Step 5: Run the Applicationโ
python app.py
The application will start on port 5001:
* Running on http://127.0.0.1:5001
* Debug mode: on
๐งช Verify Installationโ
Test that the API is running correctly:
# Health check (if available)
curl http://localhost:5001/health
# Or test a public endpoint
curl http://localhost:5001/api/docs
๐ Authentication Setupโ
Getting Your First JWT Tokenโ
- Admin Login (for testing):
POST http://localhost:5001/api/admin/login
Content-Type: application/json
{
"username": "admin@example.com",
"password": "your-password"
}
- Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"userId": "user-123",
"industryId": "industry-456",
"role": "admin"
}
}
- Use the Token:
Include the token in subsequent requests:
GET http://localhost:5001/api/user/report?reportType=daily&reportFormat=html
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Access tokens expire after 4 hours. Refresh tokens last for 14 days.
๐ Generate Your First Reportโ
Let's generate a simple daily water consumption report:
curl -X GET "http://localhost:5001/api/user/report?\
reportType=daily&\
reportFormat=html&\
service=water&\
startDate=09/11/2024&\
jwt=YOUR_JWT_TOKEN"
Query Parameters Explainedโ
| Parameter | Description | Example |
|---|---|---|
reportType | Type of report | daily, monthly, hourly |
reportFormat | Output format | html, pdf, xlsx, csv |
service | Service type | water, energy, level, quality |
startDate | Report start date | 09/11/2024 (DD/MM/YYYY) |
jwt | Authentication token | Your JWT token |
๐จ API Documentationโ
Once the application is running, you can access the interactive API documentation:
- Swagger UI: http://localhost:5001/api/docs
- User API: http://localhost:5001/api/user/
- Admin API: http://localhost:5001/api/admin/
๐๏ธ Project Structure Overviewโ
aquagenapi/
โโโ app/
โ โโโ __init__.py # Flask app initialization
โ โโโ config.py # Configuration with Azure Key Vault
โ โโโ apis/ # API blueprints
โ โ โโโ user.py # User API (31 namespaces)
โ โ โโโ admin.py # Admin API (13 namespaces)
โ โ โโโ external.py # External integrations
โ โโโ routes/ # Route handlers (33 files)
โ โ โโโ report.py # Report generation
โ โ โโโ device_data.py # Device data endpoints
โ โ โโโ alerts_routes.py # Alert management
โ โโโ services/ # Business logic (40+ services)
โ โ โโโ report/ # Report services
โ โ โโโ alerts/ # Alert processing
โ โ โโโ admin/ # Admin services
โ โโโ formatters/ # Data formatters (52 files)
โ โ โโโ report/ # Report formatters
โ โ โโโ device_data_v2/ # Device data formatters
โ โโโ database/ # Database layer
โ โ โโโ database_config.py
โ โ โโโ database_supporter.py
โ โโโ models/ # Data models (30 files)
โ โโโ templates/ # Jinja2 HTML templates (55 files)
โ โโโ util/ # Utility functions
โโโ scripts/
โ โโโ auto_reports.sh # Automated report generation
โโโ docs/ # Documentation
โโโ requirements.txt # Python dependencies
โโโ app.py # Application entry point
๐ Key Componentsโ
1. Routes Layerโ
Handles HTTP requests and validation:
- User Routes: Report generation, device data, alerts
- Admin Routes: Industry management, user management, automated reports
- External Routes: Third-party integrations
2. Services Layerโ
Contains business logic:
- Report Services: 18 different report types
- Device Data Services: IoT data processing
- Alert Services: Alert rule evaluation and triggering
3. Formatters Layerโ
Transforms data for output:
- Report Formatters: HTML, PDF, XLSX, CSV generation
- Device Data Formatters: Polymorphic formatters for different device types
4. Database Layerโ
Manages data persistence:
- Azure Cosmos DB: NoSQL database with 15+ containers
- Database Supporter: 200+ query methods
๐ฆ Next Stepsโ
Now that you have AquaGen API running, explore these topics:
- Architecture Overview - Understand the system design
- Report Generation Guide - Learn to generate reports
- API Reference - Explore all available endpoints
- Device Data Guide - Work with IoT device data
- Alert System - Set up notifications and alerts
๐ Troubleshootingโ
Port Already in Useโ
If port 5001 is already in use:
# Find and kill the process using port 5001
lsof -ti:5001 | xargs kill -9
# Or run on a different port
flask run --port 5002
Azure Connection Issuesโ
If you can't connect to Azure services:
- Verify your Azure credentials are correct
- Check network connectivity
- Ensure your IP is allowed in Azure Firewall rules
- Verify the service principal has proper permissions
Package Installation Errorsโ
If you encounter errors during pip install:
# Upgrade pip
pip install --upgrade pip
# Install packages one by one to identify the problematic package
pip install -r requirements.txt --verbose
ImportError or ModuleNotFoundErrorโ
Ensure your virtual environment is activated:
# Check which Python is being used
which python
# Should point to .venv/bin/python
๐ Additional Resourcesโ
- CLAUDE.md - Project guidelines and best practices
- Requirements - Complete list of dependencies
- Azure Documentation - Azure services documentation
- Flask Documentation - Flask framework documentation
Your AquaGen API is now running. Head to the Architecture section to learn more about how it works!