"""Generate a Postman v2.1 collection at docs/postman_collection.json. Reads docs/apispec.json for the route inventory + parameter schemas, then attaches hand-curated sample responses for every documented status code. Re-run after editing the spec: PYTHONPATH=. .venv/bin/python scripts/gen_postman.py The WebSocket endpoint is exported as a Postman WebSocket request (url.protocol = "ws", no HTTP method) — Postman 10+ recognises this and opens it in the WebSocket tab instead of a regular GET form.""" from __future__ import annotations import json import sys import uuid as _uuid from pathlib import Path from typing import Any ROOT = Path(__file__).resolve().parents[1] SPEC_PATH = ROOT / "docs" / "apispec.json" OUT_PATH = ROOT / "docs" / "Cakra Intelligence - YOLO Rest.postman_collection.json" API_PREFIX = "/api/v1" SAMPLE_REQUEST_ID = "019e5870-f3c4-74a2-9695-5e9f758d5105" SAMPLE_CAMERA_ID = "cmppqd1670000abcd" SAMPLE_LOCATION = "01 Tugu Muda" SAMPLE_LAT = -6.9847 SAMPLE_LNG = 110.4097 def _id(name: str) -> str: return str(_uuid.uuid5(_uuid.NAMESPACE_URL, "cakra-cctv-yolo:" + name)) # -------------------------------------------------------------------- # Sample bodies — one per endpoint + per status code. # -------------------------------------------------------------------- def _cam_embed(camera_id: str) -> dict: return { "hls": f"http://localhost:8000{API_PREFIX}/cameras/{camera_id}/hls/playlist.m3u8", } def _cam_metrics(camera_id: str) -> dict: return { "websocket": f"ws://localhost:8000{API_PREFIX}/cameras/{camera_id}/metrics/ws", } SAMPLE_CAMERA_INFO: dict[str, Any] = { "camera_id": SAMPLE_CAMERA_ID, "name": SAMPLE_LOCATION, "stream_url": "https://livepantau.semarangkota.go.id/171b8761.../index.m3u8", "status": "online", "owner": "KECAMATAN SEMARANG TENGAH", "city": "Semarang", "lat": SAMPLE_LAT, "lng": SAMPLE_LNG, "is_active": True, "connected": True, "last_error": None, "capture_fps": 22.6, "embed": _cam_embed(SAMPLE_CAMERA_ID), "metrics": _cam_metrics(SAMPLE_CAMERA_ID), } SAMPLE_CAMERA_INACTIVE = { **SAMPLE_CAMERA_INFO, "is_active": False, "connected": None, "capture_fps": None, "last_error": None, } # Realistic WS push payload — what hits the client every # METRICS_WS_INTERVAL seconds. Used as the saved sample for the # WebSocket request item. SAMPLE_WS_FRAME: dict[str, Any] = { "camera_id": SAMPLE_CAMERA_ID, "location": SAMPLE_LOCATION, "timestamp": "2026-06-02T13:07:36", "vehicle_count": 6, "person_count": 1, "traffic_status": "SMOOTH", "crowd_status": "NORMAL", "fps_capture": 22.6, "fps_process": 5.2, "connected": True, "frame_size": [1920, 1080], "camera": { "lat": SAMPLE_LAT, "lng": SAMPLE_LNG, "heading_deg": 0.0, "fov_deg": 70.0, "max_distance_m": 80.0, "coverage_polygon": [ [SAMPLE_LAT, SAMPLE_LNG], [SAMPLE_LAT + 0.00018, SAMPLE_LNG - 0.00026], [SAMPLE_LAT + 0.00023, SAMPLE_LNG - 0.00015], [SAMPLE_LAT + 0.00025, SAMPLE_LNG + 0.00000], [SAMPLE_LAT + 0.00023, SAMPLE_LNG + 0.00015], [SAMPLE_LAT + 0.00018, SAMPLE_LNG + 0.00026], ], }, "detections": [ { "id": 11, "class": "truck", "class_id": 7, "conf": 0.69, "bbox": [820.4, 320.1, 1060.0, 580.7], "geo": {"lat": SAMPLE_LAT + 0.00007, "lng": SAMPLE_LNG + 0.00002, "distance_m": 18.2, "bearing_deg": 1.1, "confidence": 0.51}, }, { "id": 596, "class": "motorcycle", "class_id": 3, "conf": 0.49, "bbox": [400.0, 700.0, 510.0, 880.0], "geo": {"lat": SAMPLE_LAT + 0.00003, "lng": SAMPLE_LNG - 0.00010, "distance_m": 8.0, "bearing_deg": 343.5, "confidence": 0.62}, }, ], "session": { "started_at": "2026-05-30T08:12:00+00:00", "last_captured_at": "2026-06-02T13:07:36+00:00", "samples_count": 214883, "duration_seconds": 277536.0, "avg_vehicles": 4.21, "avg_people": 0.85, "max_vehicles": 19, "max_people": 7, }, } SAMPLE_ROOT = { "service": "cakra-cctv-yolo-analytics", "version": "0.1.0", "docs": "/docs", "openapi": "/openapi.json", "api_prefix": API_PREFIX, } SAMPLE_HEALTH = {"status": "ok", "version": "0.1.0", "cameras": 12} def _envelope(code: str, message: str, details: dict[str, str]) -> dict: return { "success": False, "error": { "code": code, "message": message, "details": details, "request_id": SAMPLE_REQUEST_ID, }, } ERROR_SAMPLES: dict[str, tuple[str, dict]] = { "400": ("Bad Request", _envelope( "bad_request", "malformed request", {})), "404": ("Not Found", _envelope( "not_found", "camera_id not found", {"camera_id": "unknown"})), "422": ("Unprocessable Entity", _envelope( "validation_error", "request validation failed", {"page_size": "Input should be less than or equal to 100"})), "500": ("Internal Server Error", _envelope( "internal_server_error", "internal server error", {})), "503": ("Service Unavailable (not_activated)", _envelope( "not_activated", "camera exists in catalog but no worker is running", {"camera_id": SAMPLE_CAMERA_ID, "active_workers": "12", "max_streams": "50"})), } # Path-suffix → status overrides. The connect endpoint's 503 is pool_full, # not not_activated, so swap the sample to match. ERROR_OVERRIDES: dict[tuple[str, str], tuple[str, dict]] = { ("/connect", "503"): ("Service Unavailable (pool_full)", _envelope( "pool_full", "active worker pool is full", {"camera_id": SAMPLE_CAMERA_ID, "active_workers": "50", "max_streams": "50"})), } # Catalog-list item shape — lean projection (no status/runtime/embed). def _catalog_item(camera_id: str, name: str) -> dict: return { "camera_id": camera_id, "name": name, "stream_url": SAMPLE_CAMERA_INFO["stream_url"], "owner": SAMPLE_CAMERA_INFO["owner"], "city": SAMPLE_CAMERA_INFO["city"], "lat": SAMPLE_LAT, "lng": SAMPLE_LNG, } # 200 sample per route → keyed by the OpenAPI path. SUCCESS_BODY: dict[str, Any] = { "/": SAMPLE_ROOT, "/health": SAMPLE_HEALTH, f"{API_PREFIX}/cameras": { "pagination": {"page": 1, "page_size": 20, "total": 2351, "total_pages": 118}, "count": 2, "items": [ _catalog_item(SAMPLE_CAMERA_ID, "01 Tugu Muda"), _catalog_item("cmppqd1670000xyz", "02 Pandanaran"), ], }, f"{API_PREFIX}/cameras/{{camera_id}}": SAMPLE_CAMERA_INFO, f"{API_PREFIX}/cameras/{{camera_id}}/connect": SAMPLE_CAMERA_INFO, f"{API_PREFIX}/cameras/{{camera_id}}/disconnect": SAMPLE_CAMERA_INACTIVE, } # Endpoints whose 200 body is binary / streamed — describe instead of dump. BINARY_BODY: dict[str, tuple[str, str]] = { f"{API_PREFIX}/cameras/{{camera_id}}/hls/playlist.m3u8": ( "application/vnd.apple.mpegurl", "HLS playlist (m3u8) for the camera's annotated stream. Bboxes " "are baked into the encoded video — plug into hls.js +