This directory contains tools for automating the generation and validation of BMRS API client code based on the official OpenAPI specification.
Downloads the latest OpenAPI specification from the Elexon BMRS API.
python tools/download_schema.pyWhat it does:
- Attempts to download the OpenAPI spec from known BMRS API documentation URLs
- Validates that the downloaded file is a valid OpenAPI specification
- Saves the spec to
schema/bmrs_openapi.json - Prints a summary of available endpoints and data models
Generates Python client methods from the OpenAPI specification.
python tools/generate_client.pyWhat it does:
- Reads the OpenAPI specification from
schema/bmrs_openapi.json - Generates Python methods for each API endpoint
- Creates proper type hints and docstrings
- Saves generated code to
elexon_bmrs/generated_client.py
Features:
- Automatic method naming based on endpoint paths and operations
- Type-safe parameter handling
- Complete docstrings with parameter descriptions
- Handles path parameters, query parameters, and headers
Validates and compares the existing client with the OpenAPI spec.
python tools/validate_client.pyWhat it does:
- Compares manually written client methods with spec endpoints
- Identifies missing endpoints that should be implemented
- Finds undocumented methods
- Compares existing client with generated client
- Suggests improvements and updates
Validation checks:
- ✓ Missing endpoints from the spec
- ✓ Methods without docstrings
- ✓ Differences between manual and generated code
- ✓ Parameter mismatches
-
Download the API specification:
python tools/download_schema.py
-
Generate client code:
python tools/generate_client.py
-
Validate existing implementation:
python tools/validate_client.py
Run these scripts periodically to keep your client up-to-date:
# Download latest spec
python tools/download_schema.py
# Regenerate client
python tools/generate_client.py
# Check for conflicts
python tools/validate_client.py- Review generated code in
elexon_bmrs/generated_client.py - Compare with existing code in
elexon_bmrs/client.py - Identify new endpoints or changes
- Manually integrate changes, adding custom logic as needed
- Update tests accordingly
- Initial client creation
- Adding support for new endpoints
- Ensuring completeness of API coverage
- Maintaining consistency with API specification
- Custom error handling
- Data transformation and validation
- Business logic specific to your use case
- Convenience methods combining multiple API calls
- Adding caching or rate limiting
- Keep generated code separate: Don't edit
generated_client.pydirectly - Use inheritance or composition: Extend generated methods in your main client
- Version control: Commit both manual and generated code
- Document customizations: Clearly mark custom code in your client
- Regular updates: Run validation weekly or after API updates
- Check your internet connection
- Verify the BMRS API documentation URL is correct
- Try manually downloading from: https://bmrs.elexon.co.uk/api-documentation/
- Check if the API requires authentication
- Ensure the schema file is valid JSON
- Check OpenAPI version compatibility (supports 3.0+)
- Review error messages for specific issues
- This is normal if the API has changed
- Review each difference to determine if it's:
- A new endpoint to add
- A deprecated endpoint to remove
- A parameter change to update
Required Python packages:
pip install requests pyyaml # For schema downloadAll other functionality uses Python standard library.
╔══════════════════════════════════════════════════════════╗
║ BMRS OpenAPI Specification Downloader ║
╚══════════════════════════════════════════════════════════╝
Attempting to download from: https://bmrs.elexon.co.uk/api-documentation/openapi.json
✓ Successfully downloaded OpenAPI spec from https://bmrs.elexon.co.uk/api-documentation/openapi.json
✓ Saved OpenAPI specification to: schema/bmrs_openapi.json
============================================================
OpenAPI Specification Summary
============================================================
Title: Elexon BMRS API
Version: 1.0.0
Description: Balancing Mechanism Reporting Service API
Endpoints: 45
Sample endpoints:
- /balancing/settlement/system-prices [get]
- /datasets/B1620 [get]
- /generation/actual/per-type [get]
... and 42 more
Data Models/Schemas: 23
============================================================
When adding new tools:
- Follow the existing code structure
- Add comprehensive docstrings
- Include error handling
- Update this README
- Add examples of usage
These tools are part of the elexon-bmrs package and share the same MIT license.