askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
http_api_contract.py · 10.7 KB · 218 lines
| 1 | """Compatibility constants for the DBBASIC Object HTTP API. |
| 2 | |
| 3 | This is not a router. It records the response fields and paths existing clients |
| 4 | expect so future server code can be tested against the same contract. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | from collections.abc import Mapping |
| 10 | from typing import Any |
| 11 | |
| 12 | OBJECTS_PATH = "/objects" |
| 13 | OBJECT_PATH = "/objects/{object_id}" |
| 14 | OBJECT_STATION_PATH = "/objects/{object_id}@{station_id}" |
| 15 | ADMIN_OBJECTS_PATH = "/admin/objects" |
| 16 | ADMIN_OBJECT_PATH = "/admin/objects/{object_id}" |
| 17 | ADMIN_COLLECTIONS_PATH = "/admin/collections" |
| 18 | ADMIN_COLLECTION_PATH = "/admin/collections/{collection}" |
| 19 | ADMIN_COLLECTION_RECORDS_PATH = "/admin/collections/{collection}/records" |
| 20 | ADMIN_COLLECTION_RECORD_PATH = "/admin/collections/{collection}/records/{record_id}" |
| 21 | ADMIN_COLLECTION_CHANGES_PATH = "/admin/collections/{collection}/changes" |
| 22 | ADMIN_COLLECTION_RECORD_CHANGES_PATH = ( |
| 23 | "/admin/collections/{collection}/records/{record_id}/changes" |
| 24 | ) |
| 25 | ADMIN_SCHEMAS_PATH = "/admin/schemas" |
| 26 | ADMIN_SCHEMA_PATH = "/admin/schemas/{collection}" |
| 27 | ADMIN_IDENTITY_ACCOUNTS_PATH = "/admin/identity/accounts" |
| 28 | ADMIN_IDENTITY_ACCOUNT_PATH = "/admin/identity/accounts/{account_id}" |
| 29 | ADMIN_IDENTITY_USERS_PATH = "/admin/identity/users" |
| 30 | ADMIN_IDENTITY_USER_PATH = "/admin/identity/users/{user_id}" |
| 31 | ADMIN_IDENTITY_USER_PASSWORD_PATH = "/admin/identity/users/{user_id}/password" |
| 32 | ADMIN_IDENTITY_SOURCES_FORGET_PATH = "/admin/identity/sources/forget" |
| 33 | ADMIN_IDENTITY_SESSIONS_PATH = "/admin/identity/sessions" |
| 34 | ADMIN_IDENTITY_SESSION_PATH = "/admin/identity/sessions/{session_id}" |
| 35 | COLLECTIONS_PATH = "/collections" |
| 36 | COLLECTION_PATH = "/collections/{collection}" |
| 37 | COLLECTION_RECORDS_PATH = "/collections/{collection}/records" |
| 38 | COLLECTION_RECORD_PATH = "/collections/{collection}/records/{record_id}" |
| 39 | COLLECTION_CHANGES_PATH = "/collections/{collection}/changes" |
| 40 | COLLECTION_RECORD_CHANGES_PATH = "/collections/{collection}/records/{record_id}/changes" |
| 41 | SCHEMAS_PATH = "/schemas" |
| 42 | SCHEMA_PATH = "/schemas/{collection}" |
| 43 | EVENTS_PATH = "/events" |
| 44 | EVENT_DELIVERIES_PATH = "/events/deliveries" |
| 45 | EVENT_SUBSCRIPTIONS_PATH = "/events/subscriptions" |
| 46 | PACKAGES_PATH = "/packages" |
| 47 | PACKAGE_PATH = "/packages/{package_id}" |
| 48 | PACKAGE_INSTALL_PATH = "/packages/{package_id}/install" |
| 49 | PACKAGE_RESTORE_PATH = "/packages/{package_id}/restore" |
| 50 | PACKAGE_CHANGES_PATH = "/packages/{package_id}/changes" |
| 51 | ADMIN_STATUS_PATH = "/admin/status" |
| 52 | ADMIN_STORAGE_PATH = "/admin/storage" |
| 53 | ADMIN_COLLECTION_COMPACT_PATH = "/admin/collections/{collection}/compact" |
| 54 | ADMIN_CHANGES_PATH = "/admin/changes" |
| 55 | ADMIN_OPS_PATH = "/admin/ops" |
| 56 | ADMIN_BACKUPS_PATH = "/admin/backups" |
| 57 | # Scheduled backups to a destination off the box (object_backup_schedule): |
| 58 | # status, "back up now" and "restore-test the newest snapshot now". |
| 59 | ADMIN_SCHEDULED_BACKUPS_PATH = "/admin/backups/scheduled" |
| 60 | ADMIN_SCHEDULED_BACKUPS_RUN_PATH = "/admin/backups/scheduled/run" |
| 61 | ADMIN_SCHEDULED_BACKUPS_TEST_PATH = "/admin/backups/scheduled/test" |
| 62 | ADMIN_FILES_PATH = "/admin/files" |
| 63 | ADMIN_OBJECT_FILES_PATH = "/admin/files/{object_id}" |
| 64 | ADMIN_RECONCILES_PATH = "/admin/reconciles" |
| 65 | ADMIN_RECONCILE_PATH = "/admin/reconciles/{id}" |
| 66 | ADMIN_RECONCILE_RESOLVE_PATH = "/admin/reconciles/{id}/resolve" |
| 67 | WEBHOOKS_PATH = "/webhooks" |
| 68 | # The box's public exchange profile (docs/box-to-box-exchange.md), served by |
| 69 | # the app-exchange package's site_exchange_profile object when installed. |
| 70 | EXCHANGE_PROFILE_PATH = "/.well-known/dbbasic" |
| 71 | EXCHANGE_PROFILE_OBJECT_ID = "site_exchange_profile" |
| 72 | DAEMON_STATUS_PATH = "/daemon/status" |
| 73 | DAEMON_SCHEDULER_TASKS_PATH = "/daemon/scheduler/tasks" |
| 74 | DAEMON_SCHEDULER_TASK_PATH = "/daemon/scheduler/tasks/{task_id}" |
| 75 | DAEMON_QUEUE_MESSAGES_PATH = "/daemon/queue/messages" |
| 76 | DAEMON_QUEUE_MESSAGE_PATH = "/daemon/queue/messages/{message_id}" |
| 77 | IDENTITY_PATH = "/identity" |
| 78 | IDENTITY_ACCOUNTS_PATH = "/identity/accounts" |
| 79 | IDENTITY_ACCOUNT_PATH = "/identity/accounts/{account_id}" |
| 80 | IDENTITY_ACCOUNT_STATUS_PATH = "/identity/accounts/{account_id}/status" |
| 81 | IDENTITY_ACCOUNT_SUBSCRIPTIONS_PATH = "/identity/accounts/{account_id}/subscriptions" |
| 82 | IDENTITY_USERS_PATH = "/identity/users" |
| 83 | IDENTITY_USER_PATH = "/identity/users/{user_id}" |
| 84 | IDENTITY_USER_PASSWORD_PATH = "/identity/users/{user_id}/password" |
| 85 | IDENTITY_USER_STATUS_PATH = "/identity/users/{user_id}/status" |
| 86 | IDENTITY_USER_ROLES_PATH = "/identity/users/{user_id}/roles" |
| 87 | IDENTITY_USER_OFFBOARD_PATH = "/identity/users/{user_id}/offboard" |
| 88 | IDENTITY_SESSIONS_PATH = "/identity/sessions" |
| 89 | IDENTITY_SESSION_PATH = "/identity/sessions/{session_id}" |
| 90 | IDENTITY_CURRENT_SESSION_PATH = "/identity/session" |
| 91 | LOGIN_PATH = "/login" |
| 92 | LOGOUT_PATH = "/logout" |
| 93 | SIGNUP_PATH = "/signup" |
| 94 | VERIFY_EMAIL_PATH = "/verify" |
| 95 | PASSWORD_RESET_PATH = "/reset" |
| 96 | PASSWORD_RESET_CONFIRM_PATH = "/reset/confirm" |
| 97 | MCP_PATH = "/api/mcp" |
| 98 | SEARCH_PATH = "/api/search" |
| 99 | AI_CHAT_PATH = "/api/ai/chat" |
| 100 | TTS_PATH = "/api/tts" |
| 101 | STT_PATH = "/api/stt" |
| 102 | READ_PATH = "/api/read" |
| 103 | USER_FILES_PATH = "/api/files" |
| 104 | WEBDAV_PATH = "/dav" |
| 105 | SCHEMA_META_PATH = "/api/schema" |
| 106 | FLAGS_PATH = "/api/flags" |
| 107 | ACTIVITY_PATH = "/api/activity" |
| 108 | PREFS_PATH = "/prefs" |
| 109 | PREF_PATH = "/prefs/{key}" |
| 110 | PERMISSIONS_POLICY_PATH = "/permissions/policy" |
| 111 | PERMISSIONS_STATUS_PATH = "/permissions/status" |
| 112 | PERMISSIONS_CHECK_PATH = "/permissions/check" |
| 113 | PERMISSIONS_AUDIT_PATH = "/permissions/audit" |
| 114 | PERMISSIONS_UNSHIPPED_PATH = "/permissions/unshipped" |
| 115 | |
| 116 | SOURCE_QUERY = {"source": "true", "format": "json"} |
| 117 | STATE_QUERY = {"state": "true"} |
| 118 | METADATA_QUERY = {"metadata": "true"} |
| 119 | FILES_QUERY = {"files": "true"} |
| 120 | FILE_QUERY = {"file": "name"} |
| 121 | LOGS_QUERY = {"logs": "true", "format": "json", "limit": "100"} |
| 122 | SOURCE_CHANGES_QUERY = {"source_changes": "true", "limit": "100"} |
| 123 | CHANGES_QUERY = {"changes": "true", "limit": "100"} |
| 124 | VERSIONS_QUERY = {"versions": "true", "limit": "10"} |
| 125 | |
| 126 | RESPONSE_FIELDS: dict[str, frozenset[str]] = { |
| 127 | "object_list": frozenset({"status", "objects", "count"}), |
| 128 | "collection_list": frozenset({"status", "collections", "count"}), |
| 129 | "collection": frozenset({"status", "collection"}), |
| 130 | "record_list": frozenset({"status", "collection", "records", "count", "total"}), |
| 131 | "record": frozenset({"status", "collection", "record"}), |
| 132 | "record_changes": frozenset({"status", "collection", "changes", "count", "total"}), |
| 133 | "schema_list": frozenset({"status", "schemas", "count"}), |
| 134 | "schema": frozenset({"status", "schema"}), |
| 135 | "event_list": frozenset({"status", "events", "count", "total"}), |
| 136 | "event": frozenset({"status", "event"}), |
| 137 | "event_retention": frozenset({"status", "retention"}), |
| 138 | "event_delivery_list": frozenset({"status", "deliveries", "count", "total"}), |
| 139 | "event_subscription_list": frozenset( |
| 140 | {"status", "subscriptions", "count", "total"} |
| 141 | ), |
| 142 | "event_subscription": frozenset({"status", "subscription"}), |
| 143 | "package_list": frozenset({"status", "packages", "count"}), |
| 144 | "package": frozenset({"status", "package"}), |
| 145 | "package_dry_run": frozenset({"status", "dry_run", "change"}), |
| 146 | "package_install": frozenset({"status", "install", "changes", "restore_point"}), |
| 147 | "package_restore": frozenset( |
| 148 | {"status", "restore", "changes", "restore_point", "from_change"} |
| 149 | ), |
| 150 | "package_restore_preview": frozenset( |
| 151 | {"status", "preview", "policy_diff", "policy_diff_hash", "restore_point", "from_change"} |
| 152 | ), |
| 153 | "package_changes": frozenset({"status", "package_id", "changes", "count", "total"}), |
| 154 | "admin_changes": frozenset({"status", "changes", "count", "total"}), |
| 155 | "admin_status": frozenset( |
| 156 | {"status", "timestamp", "version", "health", "inventory", "capabilities", "packages"} |
| 157 | ), |
| 158 | "admin_storage": frozenset({"status", "append_collections"}), |
| 159 | "collection_compact": frozenset( |
| 160 | {"status", "collection", "rows_before", "rows_after", "bytes_before", "bytes_after", "duration_ms"} |
| 161 | ), |
| 162 | "daemon_status": frozenset( |
| 163 | {"status", "timestamp", "daemon", "scheduler", "queue", "events", "cleanup"} |
| 164 | ), |
| 165 | "daemon_scheduler_task_list": frozenset({"status", "tasks", "count", "total"}), |
| 166 | "daemon_scheduler_task": frozenset({"status", "task"}), |
| 167 | "daemon_queue_message_list": frozenset({"status", "messages", "count", "total"}), |
| 168 | "daemon_queue_message": frozenset({"status", "message"}), |
| 169 | "identity": frozenset({"status", "subject", "auth", "permissions"}), |
| 170 | "identity_account_list": frozenset({"status", "accounts", "count"}), |
| 171 | "identity_account": frozenset({"status", "account"}), |
| 172 | "identity_user_list": frozenset({"status", "users", "count"}), |
| 173 | "identity_user": frozenset({"status", "user"}), |
| 174 | "identity_session_list": frozenset({"status", "sessions", "count"}), |
| 175 | "identity_session_create": frozenset({"status", "session", "token"}), |
| 176 | "identity_session": frozenset({"status", "session"}), |
| 177 | "identity_current_session": frozenset({"status", "session"}), |
| 178 | "create_object": frozenset({"status", "object_id", "message"}), |
| 179 | "error": frozenset({"status", "error"}), |
| 180 | "source": frozenset({"status", "object_id", "source"}), |
| 181 | "update_source": frozenset({"status", "message", "version_id", "object_id"}), |
| 182 | "state": frozenset({"status", "object_id", "state"}), |
| 183 | "metadata": frozenset({"status", "object_id", "metadata"}), |
| 184 | "files": frozenset({"status", "object_id", "files", "count"}), |
| 185 | "file_list": frozenset({"status", "files", "count", "total"}), |
| 186 | "logs": frozenset({"status", "object_id", "logs", "count"}), |
| 187 | "source_changes": frozenset({"status", "object_id", "changes", "count", "total"}), |
| 188 | "object_changes": frozenset({"status", "object_id", "changes", "count", "total"}), |
| 189 | "versions": frozenset({"status", "object_id", "versions", "count"}), |
| 190 | "version": frozenset({"status", "object_id", "version"}), |
| 191 | "rollback": frozenset({"status", "message", "version_id", "object_id"}), |
| 192 | "destroy_object": frozenset({"status", "message", "object_id"}), |
| 193 | "permissions_policy": frozenset({"status", "policy"}), |
| 194 | "permissions_status": frozenset( |
| 195 | {"status", "permissions", "identity", "policy", "coverage", "readiness", "warnings"} |
| 196 | ), |
| 197 | "permissions_check": frozenset({"status", "decision"}), |
| 198 | "permissions_audit": frozenset({"status", "entries", "count"}), |
| 199 | "permissions_unshipped": frozenset( |
| 200 | {"status", "rules", "count", "policy_rules", "installed_packages", "warnings"} |
| 201 | ), |
| 202 | "prefs": frozenset({"status", "prefs"}), |
| 203 | "pref": frozenset({"status", "key", "value"}), |
| 204 | "flags": frozenset({"status", "flags"}), |
| 205 | } |
| 206 | |
| 207 | |
| 208 | def required_response_fields(response_name: str) -> frozenset[str]: |
| 209 | """Return required top-level fields for a named compatibility response.""" |
| 210 | try: |
| 211 | return RESPONSE_FIELDS[response_name] |
| 212 | except KeyError as exc: |
| 213 | raise ValueError(f"Unknown HTTP API response shape: {response_name}") from exc |
| 214 | |
| 215 | |
| 216 | def missing_response_fields(response_name: str, payload: Mapping[str, Any]) -> set[str]: |
| 217 | """Return required compatibility fields missing from a response payload.""" |
| 218 | return set(required_response_fields(response_name) - payload.keys()) |