Files
ai-video-fullstack/backend/scripts/export_contracts.py
T
Xin Wang 2649d64b55
Quality gate / backend (push) Canceled after 0s
Quality gate / frontend (push) Canceled after 0s
feat: harden public OpenAI API contracts
2026-09-07 14:25:48 +08:00

52 lines
1.3 KiB
Python

"""Generate deterministic public API contract snapshots.
Run from the repository root with:
make contract-snapshots
The files are reviewed and committed like source code. Tests fail when runtime
schemas drift from these snapshots.
"""
from __future__ import annotations
import json
from pathlib import Path
from app import app
from services.openai_realtime.schemas import (
realtime_client_event_json_schema,
realtime_server_event_json_schema,
)
CONTRACT_DIRECTORY = Path(__file__).resolve().parents[1] / "contracts"
def contract_documents() -> dict[str, dict]:
return {
"openapi.json": app.openapi(),
"openai-realtime-client-events.schema.json": (
realtime_client_event_json_schema()
),
"openai-realtime-server-events.schema.json": (
realtime_server_event_json_schema()
),
}
def main() -> None:
CONTRACT_DIRECTORY.mkdir(parents=True, exist_ok=True)
for name, document in contract_documents().items():
destination = CONTRACT_DIRECTORY / name
destination.write_text(
json.dumps(document, ensure_ascii=False, indent=2, sort_keys=True)
+ "\n",
encoding="utf-8",
)
print(destination.relative_to(CONTRACT_DIRECTORY.parent))
if __name__ == "__main__":
main()