35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
#!/usr/bin/env python3
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
|
|
from app.main import app
|
|
|
|
|
|
TARGET = Path(__file__).with_name("openapi.json")
|
|
|
|
|
|
def rendered_contract() -> str:
|
|
return json.dumps(app.openapi(), ensure_ascii=False, indent=2, sort_keys=True) + "\n"
|
|
|
|
|
|
def main() -> int:
|
|
parser = argparse.ArgumentParser(description="Generate or verify the RF4 Spotter OpenAPI contract")
|
|
parser.add_argument("--check", action="store_true")
|
|
args = parser.parse_args()
|
|
rendered = rendered_contract()
|
|
if args.check:
|
|
if not TARGET.exists() or TARGET.read_text(encoding="utf-8") != rendered:
|
|
parser.exit(1, "OpenAPI contract is stale; run apps/api/export_openapi.py\n")
|
|
print(f"OpenAPI contract is current: {len(app.openapi()['paths'])} paths")
|
|
return 0
|
|
TARGET.write_text(rendered, encoding="utf-8")
|
|
print(f"Wrote {TARGET}: {len(app.openapi()['paths'])} paths")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|