cmvr-visual/scripts/export_api_openapi.py

31 lines
1.0 KiB
Python
Raw Normal View History

"""Publish the backend's OpenAPI contract for sites proxying only /api/*.
Run with the backend Python dependencies. Importing the app does not run its
lifespan, open a database connection or start model services.
"""
import argparse
import json
from pathlib import Path
import sys
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--check", action="store_true", help="Check the published contract")
args = parser.parse_args()
root = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(root / "backend"))
from app.main import app
path = root / "frontend/public/help/openapi.json"
contract = json.dumps(app.openapi(), ensure_ascii=False, indent=2) + "\n"
if args.check:
if not path.exists() or path.read_text(encoding="utf-8") != contract:
raise SystemExit("OpenAPI has changed. Re-run scripts/export_api_openapi.py.")
else:
path.write_text(contract, encoding="utf-8", newline="\n")
if __name__ == "__main__":
main()