Skip to main content

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)
System Requirements
  • 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
Virtual Environment Benefits

Using a virtual environment prevents package conflicts and keeps your global Python installation clean.

Step 3: Install Dependenciesโ€‹

pip install -r requirements.txt
macOS Users

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
Security Warning

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โ€‹

  1. Admin Login (for testing):
POST http://localhost:5001/api/admin/login
Content-Type: application/json

{
"username": "admin@example.com",
"password": "your-password"
}
  1. Response:
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"user": {
"userId": "user-123",
"industryId": "industry-456",
"role": "admin"
}
}
  1. Use the Token:

Include the token in subsequent requests:

GET http://localhost:5001/api/user/report?reportType=daily&reportFormat=html
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...
Token Expiration

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โ€‹

ParameterDescriptionExample
reportTypeType of reportdaily, monthly, hourly
reportFormatOutput formathtml, pdf, xlsx, csv
serviceService typewater, energy, level, quality
startDateReport start date09/11/2024 (DD/MM/YYYY)
jwtAuthentication tokenYour JWT token

๐ŸŽจ API Documentationโ€‹

Once the application is running, you can access the interactive API documentation:

๐Ÿ—‚๏ธ 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:

  1. Architecture Overview - Understand the system design
  2. Report Generation Guide - Learn to generate reports
  3. API Reference - Explore all available endpoints
  4. Device Data Guide - Work with IoT device data
  5. 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:

  1. Verify your Azure credentials are correct
  2. Check network connectivity
  3. Ensure your IP is allowed in Azure Firewall rules
  4. 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โ€‹


You're All Set!

Your AquaGen API is now running. Head to the Architecture section to learn more about how it works!