perfect-postcode/generate_tfl_client.py
2026-01-25 21:46:02 +00:00

49 lines
1.2 KiB
Python

#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = ["openapi-python-client"]
# ///
"""Regenerate the TfL Journey API client from the OpenAPI specification."""
# Run it with:
# uv run generate_tfl_client.py
import subprocess
from pathlib import Path
OPENAPI_SPEC = Path("Journey.yaml")
OUTPUT_PATH = Path("tfl_journey_client")
def main() -> None:
if not OPENAPI_SPEC.exists():
raise FileNotFoundError(f"OpenAPI spec not found: {OPENAPI_SPEC}")
# Skip if client already exists
if OUTPUT_PATH.exists():
print(f"TfL client already exists at {OUTPUT_PATH}, skipping")
return
# Generate the client
print(f"Generating client from {OPENAPI_SPEC}")
result = subprocess.run(
[
"openapi-python-client",
"generate",
"--path",
str(OPENAPI_SPEC),
"--output-path",
str(OUTPUT_PATH),
],
check=True,
)
if result.returncode == 0:
print(f"Client generated successfully at {OUTPUT_PATH}")
else:
print("Client generation failed")
raise SystemExit(1)
if __name__ == "__main__":
main()