Python client for DEFRA's environment.data.gov.uk APIs (typed, async-friendly)
Project description
DEFRA Environment Client 🌿
Python client for DEFRA’s environment.data.gov.uk APIs — fast, typed, and async‑friendly. 🌟
Installation 🧰
uv pip install environment-client
Usage 🐍
import asyncio
from environment.flood_monitoring import FloodClient
from environment.public_register import PublicRegisterClient
async def main():
"""
An example of how to use the clients to get data from the APIs.
"""
async with (
FloodClient() as flood_client,
PublicRegisterClient() as public_register_client,
):
# Get flood warnings
flood_warnings = await flood_client.get_flood_warnings()
print(f"Found {len(flood_warnings)} flood warnings.")
# Search for waste operations registrations
waste_operations = await public_register_client.get_waste_operations(limit=5)
print(f"Found {len(waste_operations.items)} waste operations.")
# Search across all registers
all_registrations = await public_register_client.search_all_registers(name_search="Limited", limit=5)
print(f"Found {len(all_registrations.items)} registrations with 'Limited' in the name.")
if __name__ == "__main__":
asyncio.run(main())
Supported APIs 🌐
- Real-time Flood Monitoring (flood warnings, areas, stations, measures, readings)
- Bathing Waters
- Asset Management
- Hydrology
- Rainfall
- Water Quality Data Archive (WQA)
- Public Register (waste operations, end-of-life vehicles, industrial installations, water discharges, radioactive substances, waste carriers/brokers, waste exemptions, water discharge exemptions, scrap metal dealers, enforcement actions, flood risk exemptions)
Package Name vs Import Path 🔤
- Distribution (PyPI):
environment-client - Import path (Python):
environment
Example:
from environment.flood_monitoring import FloodClient
Note: The distribution is named environment-client while the import path is environment. This keeps imports concise but clarifies the project scope on PyPI.
⚠️ Important: WQA API Replacement
Note: The Water Quality Archive (WQA) APIs will be replaced later this year, meaning that the existing APIs will no longer work after Spring/Summer 2025. As of now, many water-quality/view endpoints return HTTP 404. We’ve:
- Added a
DeprecationWarningwhen instantiatingWaterQualityDataArchiveClient. - Marked WQA tests as
skippeduntil the replacement API is available.
For updates, see DEFRA’s support pages: https://environment.data.gov.uk/apiportal/support
Implementation Status 📊
-
Flood Monitoring
- Base:
https://environment.data.gov.uk/flood-monitoring - Implemented:
get_flood_warnings(/id/floods),get_flood_areas(/id/floodAreas),get_stations(/id/stations),get_station_by_id,get_measures(/id/measures),get_measure_by_id,get_readings(/data/readings),get_reading_by_id. - Notes: Uses canonical
/idfor entities and/datafor readings. Integration tests use VCR.
- Base:
-
Rainfall
- Base: Flood Monitoring (parameterised)
- Implemented: Stations and measures filtered with
parameter=rainfall; readings via/data/readings?parameter=rainfall; reading-by-id via/data/readings/{measure_id}/{timestamp}. - Notes: Rainfall is part of Flood Monitoring; not a separate base path.
-
Tide Gauge
- Base: Flood Monitoring (typed)
- Implemented: Stations via
/id/stations?type=TideGauge, station-by-id, readings via/data/readings?stationType=TideGauge, reading-by-id via/data/readings/{measure_id}/{timestamp}.
-
Hydrology
- Base:
https://environment.data.gov.uk/hydrology - Implemented: Stations, station-by-id, measures, measure-by-id, readings per-measure via
/id/measures/{id}/readings(lists do not expose a global/id/readings). - Notes: Some fields (e.g.,
status,riverName,station,unit) are normalised for model compatibility.
- Base:
-
Bathing Waters
- Base:
https://environment.data.gov.uk - Implemented:
get_bathing_waters(/doc/bathing-water.json), plus related entity lookups under/id/*.
- Base:
-
Asset Management
- Base:
https://environment.data.gov.uk/asset-management - Implemented: Assets, maintenance activities/tasks/plans, capital schemes (JSON endpoints under
/id/*.json).
- Base:
-
Catchment Planning (Catchment Data)
- Base:
https://environment.data.gov.uk/catchment-planning - Status: Placeholder only (
get_catchment_datareturns[]until the correct endpoint is confirmed).
- Base:
-
Water Quality Data Archive (WQA)
- Base:
https://environment.data.gov.uk/water-quality/view - Status: Being replaced by DEFRA; many endpoints currently return HTTP 404. Client issues a
DeprecationWarning. Tests are skipped until the replacement API is available.
- Base:
Testing & VCR 🧪
- Tests are recorded/replayed with
pytest-vcr(record mode: once). - Cassettes are stored under
tests/cassettes/with per-module subfolders (e.g.,rainfall/,hydrology/,tide_gauge/,integration/). - To re-record a cassette, delete the corresponding YAML file and re-run the specific test.
- Integration tests also use VCR to avoid live network dependency.
Development 🛠️
Contributing? See AGENTS.md for full repository guidelines (structure, style, testing, and PR conventions).
This project uses uv for dependency management.
- Install dependencies:
uv syncorjust install - Run tests:
just test - Run integration tests:
just test-integration - Lint (fix):
just lint - Format:
just format - Example script:
just run-main
Contributing 🤝
- Start with AGENTS.md for repository structure, coding style, testing, and PR conventions.
- Open an issue for larger changes; link issues in PRs.
- Follow commit prefixes (e.g.,
feat:,fix:,docs:) and keep messages concise. - Run
just lint,just format, andjust testbefore pushing. Update or re-record VCR cassettes when tests change network interactions.
Releasing 🚀
Automatic Releases (Default)
Patch versions are released automatically! Simply merge your PR to main:
- Merge PR to
main - GitHub Actions automatically:
- Bumps patch version (e.g., 0.5.11 → 0.5.12)
- Creates git tag and GitHub release
- Publishes to PyPI via Trusted Publishing
No manual steps required! The workflow uses uv version --bump patch automatically.
Manual Releases (Minor/Major versions)
For minor or major version bumps, use the Justfile recipe:
just release minor # bump minor (0.5.x → 0.6.0)
just release major "Notes" # bump major (0.5.x → 1.0.0)
Requirements:
ghCLI authenticated (gh auth status)- The recipe runs
uv version --bump, commits changes, creates tag, and publishes
Alternatively, run manually:
uv version --bump minor # or major
git add pyproject.toml uv.lock
git commit -m "publish: bump to vX.Y.Z"
git push origin main
git tag vX.Y.Z && git push origin vX.Y.Z
gh release create vX.Y.Z --title "vX.Y.Z" --notes "Release notes"
How It Works
- Automatic workflow:
.github/workflows/auto-version.ymlbumps patch on every merge to main - Publishing workflow:
.github/workflows/publish.ymlpublishes to PyPI when releases are created - Trusted Publishing: Uses GitHub's OIDC for PyPI (no manual tokens)
Setup (First Time Only)
To enable automatic PyPI publishing, you need a Personal Access Token (PAT):
- Create a fine-grained PAT: GitHub Settings → Developer settings → Personal access tokens
- Grant it
Contents: Read and writepermission for this repository - Add it as a repository secret named
PAT_TOKEN
Why? GitHub's default token doesn't trigger other workflows. The PAT allows the version bump to trigger PyPI publishing.
Without PAT: Version bump still works, but you'll need to manually trigger the publish workflow.
Links:
- PyPI project: https://pypi.org/project/environment-client/
- Workflow setup guide:
.github/workflows/README.md
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file environment_client-0.5.15.tar.gz.
File metadata
- Download URL: environment_client-0.5.15.tar.gz
- Upload date:
- Size: 19.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ebd96a4897274169c44846aeb5f71c939568533a3cac05ec1fa251628cf1e329
|
|
| MD5 |
c2fd8863e4bd6afe6d360891c2e8f256
|
|
| BLAKE2b-256 |
e2572e4b04555a124a9e3f6cef7ff7ba4deb34ad8e30d94379bd4cd87ae3d7f0
|
Provenance
The following attestation bundles were made for environment_client-0.5.15.tar.gz:
Publisher:
publish.yml on cogna-public/environment-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
environment_client-0.5.15.tar.gz -
Subject digest:
ebd96a4897274169c44846aeb5f71c939568533a3cac05ec1fa251628cf1e329 - Sigstore transparency entry: 736703789
- Sigstore integration time:
-
Permalink:
cogna-public/environment-client@8ddf3ffcd274ddf5830a008a95d1f10d5bec0e35 -
Branch / Tag:
refs/tags/v0.5.15 - Owner: https://github.com/cogna-public
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ddf3ffcd274ddf5830a008a95d1f10d5bec0e35 -
Trigger Event:
release
-
Statement type:
File details
Details for the file environment_client-0.5.15-py3-none-any.whl.
File metadata
- Download URL: environment_client-0.5.15-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c999b00840fb28dd72ee68115bff41e48552409800f6075a2bc979171c274325
|
|
| MD5 |
cd80dd4d97743bed91f80b31dcc9e211
|
|
| BLAKE2b-256 |
5416a6f50956a02c8f5e98fdd07d7b2f70389ed96082867b09b89f8ea7a9a5a2
|
Provenance
The following attestation bundles were made for environment_client-0.5.15-py3-none-any.whl:
Publisher:
publish.yml on cogna-public/environment-client
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
environment_client-0.5.15-py3-none-any.whl -
Subject digest:
c999b00840fb28dd72ee68115bff41e48552409800f6075a2bc979171c274325 - Sigstore transparency entry: 736703790
- Sigstore integration time:
-
Permalink:
cogna-public/environment-client@8ddf3ffcd274ddf5830a008a95d1f10d5bec0e35 -
Branch / Tag:
refs/tags/v0.5.15 - Owner: https://github.com/cogna-public
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@8ddf3ffcd274ddf5830a008a95d1f10d5bec0e35 -
Trigger Event:
release
-
Statement type: