| Versions | |
| CI | |
| Status | |
| Meta |
A lightweight, modular FastAPI application with Swagger UI, Docker support, Keycloak authentication, and Gunicorn + Uvicorn for production.
- Overview
- Compatibility
- Quick Start
- Configuration
- Authentication
- Plugins
- Running with Docker
- Deployment (Gunicorn + Uvicorn)
- API Endpoints
- Project Structure
- Testing & Linting
- Releasing
- License
Giswater API exposes a clean HTTP surface to query and operate a Giswater database. It wraps existing GIS and network operations behind FastAPI endpoints, so desktop tools, mobile apps, and integrations can run the same workflows without directly touching the database.
How it works
- Each route validates input with Pydantic models, builds a standard payload, and calls Postgres procedures in the Giswater schema.
- Modules are toggled via environment variables, so you only expose the domains you need (Basic, OM, CRM, Routing, Water Balance).
- Optional Keycloak authentication protects routes while keeping Swagger/OpenAPI usable.
Why it is useful
- Centralizes complex GIS/business logic behind stable HTTP endpoints.
- Supports automated workflows (field operations, routing) without QGIS client coupling.
- Keeps API surface modular and controllable per deployment.
How to use it
- Base path:
/api/v1(Swagger docs at/api/v1/docs). - Enable/disable modules in
.env(API_*toggles). - Use REST endpoints directly or via the autogenerated OpenAPI client.
This service is versioned alongside the Giswater database. Use matching ranges to avoid subtle schema/API mismatches.
giswater-api version |
Supported Giswater DB versions |
|---|---|
| 0.1 β 0.7 | 4.0 β 4.7 |
| 0.8 | 4.8+ |
git clone https://github.com/Giswater/giswater-api.git
cd giswater-apipython3 -m venv venv
source venv/bin/activate # for Windows PowerShell: .\venv\Scripts\activate
pip install -e ".[dev]"uvicorn app.main:app --reloadπ API Docs available at: http://127.0.0.1:8000/api/v1/docs
Copy the env template and customize:
cp .env.example .envAPI toggles (enable/disable modules):
API_BASIC=true
API_PROFILE=true
API_MINCUT=true
API_MAPZONES=true
API_ROUTING=true
API_CRM=true
Database:
DB_HOST=localhost
DB_PORT=5432
DB_NAME=giswater_db
DB_USER=postgres
DB_PASSWORD=postgres
DB_SCHEMA=ws
# DATABASE_URL=postgresql://user:pass@host:5432/dbname
Keycloak (optional):
KEYCLOAK_ENABLED=false
KEYCLOAK_URL=https://keycloak.example.com
KEYCLOAK_REALM=your-realm
KEYCLOAK_CLIENT_ID=giswater-api
KEYCLOAK_CLIENT_SECRET=your-secret
KEYCLOAK_ADMIN_CLIENT_SECRET=your-admin-secret
KEYCLOAK_CALLBACK_URI=http://localhost:8000/callback
Keycloak integration is optional. When enabled:
- All endpoints require a valid JWT token
- Swagger UI shows "Authorize" button for OAuth2 flow
- User info available in routes via
get_current_user()dependency
When disabled, endpoints are public and return an anonymous user context.
Custom plugins can extend the API. Place plugin folders in plugins/:
plugins/
βββ my-plugin/
βββ ...
Plugins are auto-loaded at startup. See example plugin.
docker build -t giswater-api .docker run -d -p 8000:8000 giswater-apidocker run -d -p 8000:8000 \
--env-file .env \
-v /var/log/giswater-api:/app/logs \
giswater-apiSet the variables listed in the Configuration section (or via --env-file).
For production, run:
gunicorn -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 app.main:app| Endpoint | Method | Description |
|---|---|---|
/ |
GET | Root endpoint (version info) |
/health |
GET | Health check (DB connection status) |
| Endpoint | Method | Description |
|---|---|---|
/basic/getfeaturechanges |
GET | Fetch GIS features modified since specified date |
/basic/getinfofromcoordinates |
GET | Fetch GIS features info from coordinates |
/basic/getfeaturesfrompolygon |
GET | Fetch GIS features inside a polygon |
/basic/getselectors |
GET | Fetch current selectors |
/basic/getsearch |
GET | Search features |
/basic/getarcauditvalues |
GET | Fetch arc audit values |
/basic/getlist |
GET | Generic list query with paging and filters |
| Endpoint | Method | Description |
|---|---|---|
/crm/hydrometers |
POST | Insert hydrometers (single or bulk) |
/crm/hydrometers/{code} |
PATCH | Update single hydrometer by code |
/crm/hydrometers |
PATCH | Update multiple hydrometers in bulk |
/crm/hydrometers/{code} |
DELETE | Delete single hydrometer by code |
/crm/hydrometers |
DELETE | Delete multiple hydrometers in bulk |
/crm/hydrometers |
PUT | Replace all hydrometers (full sync mode) |
| Endpoint | Method | Description |
|---|---|---|
/om/mincuts |
GET | List mincuts |
/om/mincuts/{mincut_id} |
GET | Get mincut dialog data |
/om/mincuts |
POST | Create a new mincut |
/om/mincuts/{mincut_id} |
PATCH | Update an existing mincut |
/om/mincuts/{mincut_id} |
DELETE | Delete a mincut |
/om/mincuts/{mincut_id}/valves |
GET | List valves associated to a mincut |
/om/mincuts/{mincut_id}/valves/{valve_id}/toggle-unaccess |
POST | Toggle valve unaccess status and recalculate mincut |
/om/mincuts/{mincut_id}/valves/{valve_id}/toggle-status |
POST | Update valve status in a mincut |
/om/mincuts/{mincut_id}/start |
POST | Start mincut and interrupt supply |
/om/mincuts/{mincut_id}/end |
POST | End mincut and restore supply |
/om/mincuts/{mincut_id}/cancel |
POST | Cancel mincut and keep issue recorded |
| Endpoint | Method | Description |
|---|---|---|
/om/profiles |
POST | Create a new profile |
| Endpoint | Method | Description |
|---|---|---|
/om/flow |
POST | Calculate upstream/downstream flow trace |
| Endpoint | Method | Description |
|---|---|---|
/om/dmas |
GET | Returns collection of DMAs |
/om/macrodmas |
GET | Returns collection of macro DMAs |
/om/dmas/{dma_id}/hydrometers |
GET | Retrieve DMA hydrometers data with location, status, and measurements |
/om/dmas/{dma_id}/parameters |
GET | Retrieves DMA parameters for performance analysis |
/om/dmas/{dma_id}/flowmeters |
GET | Retrieve DMA flowmeters data with location, status, and flow |
/om/dmas/{dma_id}/connecs |
GET | Retrieve DMA connecs data from ve_connec |
| Endpoint | Method | Description |
|---|---|---|
/om/macrosectors |
GET | Returns collection of macrosectors |
/om/sectors |
GET | Returns collection of sectors |
/om/presszones |
GET | Returns collection of presszones |
/om/macrodqas |
GET | Returns collection of macro DQAs |
/om/dqas |
GET | Returns collection of DQAs |
/om/macroomzones |
GET | Returns collection of macro OM zones |
/om/omzones |
GET | Returns collection of OM zones |
/om/omunits |
GET | Returns collection of OM units |
| Endpoint | Method | Description |
|---|---|---|
/routing/getobjectoptimalpathorder |
GET | Get optimal path through network points using Valhalla routing engine |
/routing/getobjectparameterorder |
GET | Get features ordered by parameter |
giswater-api/
βββ app/
β βββ .env.example # Environment variable template
β β
β βββ models/ # Pydantic models (organized by module)
β β βββ basic/ # Basic module models
β β βββ crm/ # CRM module models
β β βββ om/ # OM (mincut, dma, mapzones) models
β β βββ routing/ # Routing module models
β β βββ util_models.py # Shared utility models
β β
β βββ routers/ # API endpoints (organized by module)
β β βββ basic/ # GIS feature queries
β β βββ crm/ # Hydrometer CRUD
β β βββ om/ # OM operations (mincut, profile, flow, dma, mapzones)
β β βββ routing/ # Optimal path routing
β β
β βββ utils/ # Utilities and helpers
β β βββ utils.py # General utilities
β β βββ routing_utils.py # Valhalla routing helpers
β β
β βββ config.py # Configuration loader
β βββ database.py # Database connection manager
β βββ dependencies.py # FastAPI dependencies
β βββ keycloak.py # Keycloak OAuth2/OIDC integration
β βββ main.py # FastAPI app entry point
β βββ static/ # Static files (favicon, etc.)
β
βββ plugins/ # Plugin directory (see plugins/readme.md)
βββ tests/ # Unit and integration tests
βββ .github/workflows/ # CI/CD (flake8 lint, pytest)
βββ Dockerfile # Docker build config
βββ gunicorn.conf.py # Gunicorn production config
βββ pyproject.toml # Project metadata, dependencies, and tooling config
βββ scripts/
β βββ release.sh # Release script (bash)
β βββ release.ps1 # Release script (PowerShell)
βββ README.md
pytestruff check .CI/CD runs both on push via GitHub Actions (.github/workflows/).
- Update
CHANGELOG.mdwith the new version's changes and commit. - Run the release script:
# Bash
./scripts/release.sh 0.8.0
# PowerShell
.\scripts\release.ps1 0.8.0The script will:
- Abort if there are uncommitted changes
- Update the version in
pyproject.toml - Commit, tag
vX.Y.Z, create branchrelease/X.Y - Push everything to origin
This project is free software, licensed under the GNU General Public License (GPL) version 3 or later. Refer to the LICENSE file for details.