interloper-api 0.133.0__tar.gz → 0.135.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. {interloper_api-0.133.0 → interloper_api-0.135.0}/PKG-INFO +2 -2
  2. {interloper_api-0.133.0 → interloper_api-0.135.0}/pyproject.toml +2 -2
  3. {interloper_api-0.133.0 → interloper_api-0.135.0}/pyproject.toml.orig +2 -2
  4. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/app.py +26 -7
  5. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/admin.py +30 -1
  6. interloper_api-0.135.0/src/interloper_api/routes/extensions.py +112 -0
  7. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/websocket.py +20 -31
  8. {interloper_api-0.133.0 → interloper_api-0.135.0}/README.md +0 -0
  9. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/__init__.py +0 -0
  10. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/authorization.py +0 -0
  11. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/cli.py +0 -0
  12. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/dependencies/__init__.py +0 -0
  13. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/dependencies/auth.py +0 -0
  14. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/dependencies/rbac.py +0 -0
  15. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/dependencies/state.py +0 -0
  16. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/extension.yaml +0 -0
  17. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/login.py +0 -0
  18. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/notifications/__init__.py +0 -0
  19. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/notifications/base.py +0 -0
  20. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/notifications/invitations.py +0 -0
  21. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/notifications/super_admins.py +0 -0
  22. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/__init__.py +0 -0
  23. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/agent.py +0 -0
  24. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/apps.py +0 -0
  25. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/auth.py +0 -0
  26. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/authorization.py +0 -0
  27. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/backfills.py +0 -0
  28. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/catalog.py +0 -0
  29. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/components.py +0 -0
  30. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/executions.py +0 -0
  31. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/health.py +0 -0
  32. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/oauth.py +0 -0
  33. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/organisations.py +0 -0
  34. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/overview.py +0 -0
  35. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/relations.py +0 -0
  36. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/runs.py +0 -0
  37. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/routes/tokens.py +0 -0
  38. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/server.py +0 -0
  39. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/settings.py +0 -0
  40. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/utils/__init__.py +0 -0
  41. {interloper_api-0.133.0 → interloper_api-0.135.0}/src/interloper_api/utils/time.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: interloper-api
3
- Version: 0.133.0
3
+ Version: 0.135.0
4
4
  Summary: Interloper FastAPI routes
5
5
  Author: Guillaume Onfroy
6
6
  Author-email: Guillaume Onfroy <guillaume@digitlcloud.com>
@@ -11,7 +11,7 @@ Requires-Dist: cryptography>=43.0.0
11
11
  Requires-Dist: fastapi>=0.115.0
12
12
  Requires-Dist: google-auth>=2.0.0
13
13
  Requires-Dist: httpx2>=2.13.1
14
- Requires-Dist: psycopg2-binary>=2.9.0
14
+ Requires-Dist: psycopg[binary]>=3.1
15
15
  Requires-Dist: uvicorn[standard]>=0.41.0
16
16
  Requires-Dist: wsproto>=1.2.0
17
17
  Requires-Dist: itsdangerous>=2.2
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "interloper-api"
3
- version = "0.133.0"
3
+ version = "0.135.0"
4
4
  description = "Interloper FastAPI routes"
5
5
  readme = "README.md"
6
6
  requires-python = ">=3.11"
@@ -12,7 +12,7 @@ dependencies = [
12
12
  "fastapi>=0.115.0",
13
13
  "google-auth>=2.0.0",
14
14
  "httpx2>=2.13.1",
15
- "psycopg2-binary>=2.9.0",
15
+ "psycopg[binary]>=3.1",
16
16
  "uvicorn[standard]>=0.41.0",
17
17
  "wsproto>=1.2.0",
18
18
  "itsdangerous>=2.2",
@@ -3,7 +3,7 @@
3
3
  # ###############
4
4
  [project]
5
5
  name = "interloper-api"
6
- version = "0.133.0"
6
+ version = "0.135.0"
7
7
  description = "Interloper FastAPI routes"
8
8
  readme = "README.md"
9
9
  authors = [{ name = "Guillaume Onfroy", email = "guillaume@digitlcloud.com" }]
@@ -16,7 +16,7 @@ dependencies = [
16
16
  "fastapi>=0.115.0",
17
17
  "google-auth>=2.0.0",
18
18
  "httpx2>=2.13.1",
19
- "psycopg2-binary>=2.9.0",
19
+ "psycopg[binary]>=3.1",
20
20
  "uvicorn[standard]>=0.41.0",
21
21
  "wsproto>=1.2.0",
22
22
  "itsdangerous>=2.2",
@@ -7,7 +7,7 @@ from collections.abc import AsyncGenerator
7
7
  from contextlib import asynccontextmanager
8
8
  from typing import Any
9
9
 
10
- from fastapi import APIRouter, FastAPI, Request
10
+ from fastapi import APIRouter, Depends, FastAPI, Request
11
11
  from fastapi.middleware.cors import CORSMiddleware
12
12
  from fastapi.responses import JSONResponse
13
13
  from interloper.catalog import Catalog
@@ -24,7 +24,7 @@ from interloper_db import (
24
24
  from starlette.concurrency import run_in_threadpool
25
25
 
26
26
  from interloper_api.authorization import AuthorizationServer, CrossOriginMiddleware
27
- from interloper_api.dependencies import State
27
+ from interloper_api.dependencies import State, require_viewer
28
28
  from interloper_api.login import GoogleLogin
29
29
  from interloper_api.routes import (
30
30
  admin,
@@ -34,6 +34,7 @@ from interloper_api.routes import (
34
34
  backfills,
35
35
  components,
36
36
  executions,
37
+ extensions,
37
38
  health,
38
39
  oauth,
39
40
  organisations,
@@ -49,7 +50,8 @@ from interloper_api.settings import ApiSettings
49
50
  logger = logging.getLogger(__name__)
50
51
 
51
52
  #: Routers mounted under ``/api`` for every deployment: the caller's own, the
52
- #: organisation management and super-admin ones, a run's ingest, and the probes.
53
+ #: organisation management and super-admin ones, the installed extensions, a
54
+ #: run's ingest, and the probes.
53
55
  _ROUTERS: tuple[APIRouter, ...] = (
54
56
  auth.router,
55
57
  authorization.router,
@@ -58,13 +60,14 @@ _ROUTERS: tuple[APIRouter, ...] = (
58
60
  oauth.router,
59
61
  tokens.router,
60
62
  apps.router,
63
+ extensions.router,
61
64
  runs.router,
62
65
  health.router,
63
66
  )
64
67
 
65
68
  #: Routers acting in one organisation, mounted under ``/api/organisations/{org_id}``.
66
- #: The agent router is absent by design: it is optional and mounted by
67
- #: :func:`_mount_agent`.
69
+ #: The agent's and the extensions' are absent by design: they are optional and
70
+ #: mounted by :func:`_mount_agent` and :func:`_mount_extensions`.
68
71
  _ORG_ROUTERS: tuple[APIRouter, ...] = (
69
72
  organisations.org_router,
70
73
  catalog_routes.org_router,
@@ -185,8 +188,9 @@ def create_app(
185
188
  ) -> FastAPI:
186
189
  """Create the FastAPI application with all routes.
187
190
 
188
- On startup the application promotes the configured super-admins and
189
- starts the realtime listener.
191
+ The routes the installed packages declare mount under
192
+ ``/api/organisations/{org_id}/ext/<name>``. On startup the application
193
+ promotes the configured super-admins and starts the realtime listener.
190
194
 
191
195
  Args:
192
196
  store: The ``Store`` instance for persistence.
@@ -227,6 +231,7 @@ def create_app(
227
231
  for org_router in _ORG_ROUTERS:
228
232
  api.include_router(org_router, prefix=ORG_PREFIX)
229
233
  agent_model = _mount_agent(api, settings)
234
+ _mount_extensions(api)
230
235
  app.include_router(api)
231
236
  app.include_router(authorization.well_known_router)
232
237
 
@@ -294,6 +299,20 @@ def _mount_agent(api: APIRouter, settings: ApiSettings) -> str | None:
294
299
  return settings.agent.model
295
300
 
296
301
 
302
+ def _mount_extensions(api: APIRouter) -> None:
303
+ """Mount every router the installed packages declare, the viewer role its floor.
304
+
305
+ A router that fails to load is skipped and recorded, so the listing and
306
+ the configuration page name it while every other one serves.
307
+
308
+ Args:
309
+ api: The ``/api`` router to mount onto.
310
+ """
311
+ for name, router in extensions.EXTENSION_ROUTERS.items():
312
+ api.include_router(router, prefix=f"{ORG_PREFIX}/ext/{name}", dependencies=[Depends(require_viewer)])
313
+ logger.info("Extension routes '%s' mounted at /api%s/ext/%s.", name, ORG_PREFIX, name)
314
+
315
+
297
316
  def _state(
298
317
  store: Store,
299
318
  catalog: Catalog,
@@ -13,6 +13,7 @@ from __future__ import annotations
13
13
 
14
14
  import datetime as dt
15
15
  import inspect
16
+ from collections.abc import Iterable
16
17
  from datetime import datetime
17
18
  from importlib import metadata
18
19
  from typing import Annotated, Any
@@ -21,6 +22,7 @@ from uuid import UUID
21
22
  from authlib.oauth2.rfc7591 import InvalidRedirectURIError
22
23
  from fastapi import APIRouter, HTTPException, Query, Request, Response
23
24
  from interloper.catalog import Catalog
25
+ from interloper.extension import MANIFESTS, Extension, ExtensionFailure
24
26
  from interloper.settings import SETTINGS, Settings
25
27
  from interloper_db import Organisation, OrganisationQuery, Page, PageQuery, Profile, ProfileQuery, UsageQuery
26
28
  from interloper_db.models import OAuthClient
@@ -32,6 +34,7 @@ from interloper_api.authorization import CLIENT_NAME_MAX_LENGTH, Client
32
34
  from interloper_api.dependencies import SettingsDep, StateDep, StoreDep, SuperAdminDep
33
35
  from interloper_api.notifications import SuperAdminPromotionEmail
34
36
  from interloper_api.routes.apps import AppResponse
37
+ from interloper_api.routes.extensions import ExtensionResponse
35
38
  from interloper_api.routes.organisations import CreateOrganisationRequest
36
39
 
37
40
  router = APIRouter(prefix="/admin", tags=["admin"])
@@ -246,6 +249,26 @@ class AdminSettingsSection(BaseModel):
246
249
  return True, value
247
250
 
248
251
 
252
+ class AdminExtension(ExtensionResponse):
253
+ """One installed package that ships a manifest, with everything skipped of it and why."""
254
+
255
+ failures: list[ExtensionFailure]
256
+
257
+ @classmethod
258
+ def from_extension(cls, extension: Extension, failures: Iterable[ExtensionFailure]) -> AdminExtension:
259
+ """Describe an installed package with its own failures.
260
+
261
+ Args:
262
+ extension: The package, as its manifest says what it is.
263
+ failures: Everything skipped so far, every package's.
264
+
265
+ Returns:
266
+ The package and the failures that are its own, in the order they happened.
267
+ """
268
+ own = [failure for failure in failures if failure.package == extension.package]
269
+ return cls(**ExtensionResponse.from_extension(extension, own).model_dump(), failures=own)
270
+
271
+
249
272
  # One set of quota limits, keyed by quota key; null means unlimited (or unset,
250
273
  # for overrides). Derived from the ``QUOTAS`` registry rather than declared
251
274
  # field-by-field, so registering a quota surfaces it here — and in the frontend,
@@ -364,6 +387,7 @@ class AdminConfigResponse(BaseModel):
364
387
  features: dict[str, bool]
365
388
  catalog: dict[str, list[str]]
366
389
  sections: list[AdminSettingsSection]
390
+ extensions: list[AdminExtension]
367
391
 
368
392
  @classmethod
369
393
  def from_declared(cls, features: dict[str, bool], catalog: Catalog) -> AdminConfigResponse:
@@ -375,7 +399,10 @@ class AdminConfigResponse(BaseModel):
375
399
  cannot be imported is left out. The catalog is reported from the
376
400
  hydrated ``Catalog`` (the enabled components, their dependencies and
377
401
  the framework's own), not the ``catalog.include`` setting, which only
378
- holds the explicitly configured import paths.
402
+ holds the explicitly configured import paths. The extensions are the
403
+ installed packages with what was skipped of them by the time the
404
+ snapshot is built, which ``create_app`` does once the catalog is
405
+ hydrated and the extension routes are mounted.
379
406
 
380
407
  Args:
381
408
  features: Optional-feature flags keyed by feature name.
@@ -393,11 +420,13 @@ class AdminConfigResponse(BaseModel):
393
420
  settings_class = SETTINGS.get(name)
394
421
  if settings_class is not None:
395
422
  sections.append(AdminSettingsSection.from_settings(name, entry.extension, settings_class.get()))
423
+ failures = MANIFESTS.failures
396
424
  return cls(
397
425
  version=version,
398
426
  features=features,
399
427
  catalog=_catalog_by_kind(catalog),
400
428
  sections=sections,
429
+ extensions=[AdminExtension.from_extension(extension, failures) for extension in MANIFESTS.extensions()],
401
430
  )
402
431
 
403
432
 
@@ -0,0 +1,112 @@
1
+ """Extension routes: the HTTP routes installed packages declare, and the listing of those packages.
2
+
3
+ A package adds routes by naming an ``APIRouter`` in the ``routes`` section of
4
+ its manifest:
5
+
6
+ ```yaml
7
+ routes: acme_interloper.routes:router
8
+ ```
9
+
10
+ The router answers under ``/api/organisations/{org_id}/ext/<name>``, where
11
+ ``<name>`` is the entry's name: the extension's key for a single import
12
+ string. The viewer role in the organisation the path names is the floor of
13
+ every route; a route that writes raises it with ``EditorDep``. The routes
14
+ reuse the API's dependencies (``StoreDep``, ``ViewerDep``, ``EditorDep``,
15
+ ``load_authorized``), and the contract that keeps one organisation's data
16
+ from another is the author's: every row carries ``org_id``, and a fetch by id
17
+ goes through ``load_authorized``.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import re
23
+ from collections.abc import Iterable
24
+ from typing import Any, Literal
25
+
26
+ from fastapi import APIRouter
27
+ from interloper.extension import MANIFESTS, Extension, ExtensionFailure
28
+ from interloper.registry import Registry
29
+ from pydantic import BaseModel
30
+
31
+ from interloper_api.dependencies import CurrentUserDep
32
+
33
+ _NAME_PATTERN = re.compile(r"[a-z][a-z0-9_]*")
34
+
35
+
36
+ def _adopt_router(name: str, loaded: Any) -> tuple[str, APIRouter]:
37
+ """Accept a declared router, its name the path segment it answers under.
38
+
39
+ Args:
40
+ name: The entry's name.
41
+ loaded: What the entry's import string names.
42
+
43
+ Returns:
44
+ The name and the router.
45
+
46
+ Raises:
47
+ ValueError: If the name is not a lowercase identifier.
48
+ TypeError: If the entry names something other than an ``APIRouter``.
49
+ """
50
+ if not _NAME_PATTERN.fullmatch(name):
51
+ raise ValueError(f"'{name}' is not a lowercase identifier, which the path segment must be")
52
+ if not isinstance(loaded, APIRouter):
53
+ raise TypeError(f"'{name}' names a {type(loaded).__name__}, not an APIRouter")
54
+ return name, loaded
55
+
56
+
57
+ EXTENSION_ROUTERS: Registry[APIRouter] = Registry("routes", adopt=_adopt_router)
58
+
59
+ router = APIRouter(tags=["extensions"])
60
+
61
+
62
+ class ExtensionResponse(BaseModel):
63
+ """One installed package that ships a manifest, and whether everything it declares loaded.
64
+
65
+ ``status`` is ``failed`` when anything the package declares was skipped:
66
+ its manifest, a section or an entry. The failures themselves are shown
67
+ to super-admins only, on the configuration page.
68
+ """
69
+
70
+ package: str
71
+ key: str | None
72
+ name: str
73
+ description: str
74
+ icon: str
75
+ version: str | None
76
+ status: Literal["ok", "failed"]
77
+
78
+ @classmethod
79
+ def from_extension(cls, extension: Extension, failures: Iterable[ExtensionFailure]) -> ExtensionResponse:
80
+ """Describe an installed package from its manifest and what was skipped of it.
81
+
82
+ Args:
83
+ extension: The package, as its manifest says what it is.
84
+ failures: Everything skipped so far, every package's.
85
+
86
+ Returns:
87
+ The package, ``failed`` when one of the failures is its own.
88
+ """
89
+ failed = any(failure.package == extension.package for failure in failures)
90
+ return cls(
91
+ package=extension.package,
92
+ key=extension.key,
93
+ name=extension.name,
94
+ description=extension.description,
95
+ icon=extension.icon,
96
+ version=extension.version,
97
+ status="failed" if failed else "ok",
98
+ )
99
+
100
+
101
+ @router.get("/extensions")
102
+ def list_extensions(user: CurrentUserDep) -> list[ExtensionResponse]:
103
+ """List the installed packages that ship a manifest, the platform's own included.
104
+
105
+ Args:
106
+ user: The authenticated caller; any signed-in user may read the listing.
107
+
108
+ Returns:
109
+ The packages, sorted by package.
110
+ """
111
+ failures = MANIFESTS.failures
112
+ return [ExtensionResponse.from_extension(extension, failures) for extension in MANIFESTS.extensions()]
@@ -16,15 +16,13 @@ from __future__ import annotations
16
16
  import asyncio
17
17
  import json
18
18
  import logging
19
- import select as _select
20
19
  import threading
21
20
  import time
22
21
  from collections.abc import AsyncGenerator
23
22
  from contextlib import asynccontextmanager
24
23
  from uuid import UUID
25
24
 
26
- import psycopg2
27
- import psycopg2.extensions
25
+ import psycopg
28
26
  from fastapi import APIRouter, Cookie, FastAPI, HTTPException, WebSocket, WebSocketDisconnect
29
27
  from interloper_db import Store
30
28
  from starlette.concurrency import run_in_threadpool
@@ -101,36 +99,27 @@ def _start_notify_listener(dsn: str, loop: asyncio.AbstractEventLoop) -> None:
101
99
  """Background thread: listens for PostgreSQL NOTIFY and dispatches to WebSocket clients.
102
100
 
103
101
  Args:
104
- dsn: PostgreSQL connection string.
102
+ dsn: libpq connection string of the database to listen on.
105
103
  loop: The asyncio event loop to schedule coroutines on.
106
104
  """
107
105
  while True:
108
106
  try:
109
- connection = psycopg2.connect(dsn)
110
- connection.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
111
-
112
- with connection.cursor() as cursor:
113
- cursor.execute("LISTEN table_changes")
114
-
115
- logger.info("[Realtime] NOTIFY listener started")
116
-
117
- while True:
118
- readable, _, _ = _select.select([connection], [], [], 1.0)
119
- if readable:
120
- connection.poll()
121
- while connection.notifies:
122
- notify = connection.notifies.pop(0)
123
- try:
124
- payload = json.loads(notify.payload)
125
- org_id = str(payload["org_id"])
126
- message = {
127
- "table": payload["table"],
128
- "event": payload["op"],
129
- "record": payload.get("record"),
130
- }
131
- asyncio.run_coroutine_threadsafe(_manager.broadcast(org_id, message), loop)
132
- except (json.JSONDecodeError, KeyError) as e:
133
- logger.warning("[Realtime] Invalid NOTIFY payload: %s", e)
107
+ with psycopg.connect(dsn, autocommit=True) as connection:
108
+ connection.execute("LISTEN table_changes")
109
+ logger.info("[Realtime] NOTIFY listener started")
110
+
111
+ for notify in connection.notifies():
112
+ try:
113
+ payload = json.loads(notify.payload)
114
+ org_id = str(payload["org_id"])
115
+ message = {
116
+ "table": payload["table"],
117
+ "event": payload["op"],
118
+ "record": payload.get("record"),
119
+ }
120
+ asyncio.run_coroutine_threadsafe(_manager.broadcast(org_id, message), loop)
121
+ except (json.JSONDecodeError, KeyError) as e:
122
+ logger.warning("[Realtime] Invalid NOTIFY payload: %s", e)
134
123
  except Exception as e: # noqa: BLE001
135
124
  # This thread is the only realtime path; letting anything escape
136
125
  # would end it for the life of the process, so every failure is
@@ -154,8 +143,8 @@ async def realtime_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
154
143
  from interloper_db import get_engine
155
144
 
156
145
  engine = get_engine()
157
- # str(engine.url) masks the password as '***' — psycopg2 needs the real one.
158
- dsn = engine.url.render_as_string(hide_password=False)
146
+ # libpq refuses SQLAlchemy's driver name, and str(engine.url) would mask the password as '***'.
147
+ dsn = engine.url.set(drivername="postgresql").render_as_string(hide_password=False)
159
148
 
160
149
  loop = asyncio.get_running_loop()
161
150
  thread = threading.Thread(