An async Python wrapper for the iRail API, designed to make interacting with iRail simple and efficient. Built with aiohttp, it provides non-blocking I/O operations for optimal performance in async applications.
pyRail is a Python library that provides a convenient interface for interacting with the iRail API. It supports various endpoints such as stations, liveboard, vehicle, connections, and disturbances. The library includes features like caching and rate limiting to optimize API usage.
- Async handling
- Retrieve real-time train information, including liveboards and vehicle details.
- Access train station data, connections, and disturbances.
- Supports API endpoints: stations, liveboard, vehicle, connections, and disturbances.
- Caching and conditional GET requests using ETags.
- Rate limiting to handle API request limits efficiently.
To install pyRail, use pip:
pip install pyrail
Here is an example of how to use pyRail asynchronously:
import asyncio
from pyrail.irail import iRail
async def main():
# Sequential requests example
async with iRail() as api:
try:
# Get the total number of stations
stations = await api.get_stations()
if stations:
print(f"Total stations: {len(stations)}")
# Example output: Total stations: 691
# stations = [
# {"name": "Brussels-South", "id": "BE.NMBS.008814001", ...},
# ...
# ]
# Get the liveboard for a specific station
liveboard = await api.get_liveboard(station='Brussels-South')
if liveboard:
print(f"Liveboard for Brussels-South: {liveboard}")
except Exception as e:
print(f"Error occurred: {e}")
# Parallel requests example
async with iRail() as api:
try:
connections, vehicle_info = await asyncio.gather(
# Get connections between stations
api.get_connections(
from_station='Antwerpen-Centraal',
to_station='Brussel-Centraal'
),
# Get vehicle information
api.get_vehicle("BE.NMBS.IC1832")
)
print("Parallel results:")
print(f"Connections from Antwerpen-Centraal to Brussel-Centraal: {connections}")
print(f"Vehicle information for BE.NMBS.IC1832: {vehicle_info}")
except Exception as e:
print(f"Error occurred in parallel requests: {e}")
# Run the async code
if __name__ == "__main__":
asyncio.run(main())
You can configure the language for the API requests:
api = iRail(lang='nl')
Supported languages are:
en
(English, default)fr
(French)de
(German)nl
(Dutch)
If no language is specified or an invalid value is provided, English (en
) will be used as the default language.
You can provide an external aiohttp ClientSession:
from aiohttp import ClientSession
async def main():
# Using an external session
async with ClientSession() as session:
async with iRail(session=session) as api:
stations = await api.get_stations()
print(f"Total stations: {len(stations)}")
# Or let iRail manage its own session
async with iRail() as api:
stations = await api.get_stations()
You can clear the ETag cache when needed:
async with iRail() as api:
# Clear the ETag cache
api.clear_etag_cache()
# Subsequent requests will fetch fresh data
stations = await api.get_stations()
pyRail implements rate limiting to comply with iRail API's guidelines:
- Maximum of 3 requests per second per source IP address
- 5 burst requests available, allowing up to 8 requests in 1 second
The library automatically handles rate limiting:
# Rate limiting is handled automatically
async with iRail() as api:
# These requests will be rate-limited if needed
for station in ['Brussels-South', 'Antwerp-Central', 'Ghent-Sint-Pieters']:
liveboard = await api.get_liveboard(station=station)
Exceeding the request limit will cause the server to return 429 responses. You can monitor rate limiting through debug logs.
The devcontainer setup includes all necessary dependencies and tools for development.
- Docker
- Visual Studio Code
- Remote - Containers extension
- Clone the repository:
git clone https://github.com/tjorim/pyrail.git
- Open the project in a devcontainer:
cd pyrail code .
- Once VS Code opens, it will detect the devcontainer configuration and prompt you to reopen the project in a container. Click "Reopen in Container" to start the development environment.
To run the tests, use the following command in the terminal within the devcontainer:
pytest
We use ruff for code formatting and linting. To check your code style, run:
ruff check .
To automatically fix style issues, run:
ruff check . --fix
pyRail uses Python's built-in logging module. You can set the logging level at runtime to get detailed logs.
import logging
# Set the logging level to DEBUG
logging.basicConfig(level=logging.DEBUG)
Contributions are welcome! Here's how you can contribute to pyRail:
- Use the GitHub issue tracker to report bugs or suggest features.
- Check existing issues before opening a new one.
- Provide as much detail as possible, including steps to reproduce for bugs.
- Fork the repository and create your branch from
main
. - Ensure your code adheres to the project's style guidelines (run
ruff check .
). - Add or update tests as necessary.
- Update documentation to reflect your changes.
- Submit a pull request with a clear title and description.
- Your pull request will be automatically reviewed by CodeRabbit for code quality and consistency.
- @tjorim
- @jcoetsie
- @lgnap
This project is licensed under the Apache 2.0 License. See the LICENSE file for details.