first
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
"""FastAPI dependency injection helpers.
|
||||
|
||||
Use these as ``Depends(...)`` in your route handlers. They provide:
|
||||
* validated request context (tenant + user id)
|
||||
* Mongo collections
|
||||
* Event publisher
|
||||
* Settings
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import AsyncIterator
|
||||
|
||||
from fastapi import Depends
|
||||
from motor.motor_asyncio import AsyncIOMotorCollection
|
||||
|
||||
from .config import Settings, get_settings
|
||||
from .db.mongo import get_collection, MongoCollections
|
||||
from .middleware.correlation import RequestContext, get_current_context
|
||||
from .messaging.rabbitmq import EventPublisher, get_event_publisher
|
||||
|
||||
|
||||
def get_req_ctx(ctx: RequestContext = Depends(get_current_context)) -> RequestContext:
|
||||
"""Return the current authenticated request context.
|
||||
|
||||
Raises 401 if there is no tenant context — so the handler can rely on
|
||||
``ctx.tenant_id`` being non-None.
|
||||
"""
|
||||
ctx.require_tenant()
|
||||
return ctx
|
||||
|
||||
|
||||
async def coll_orgs() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.ORGANIZATIONS)
|
||||
|
||||
|
||||
async def coll_projects() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.PROJECTS)
|
||||
|
||||
|
||||
async def coll_artifacts() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.ARTIFACTS)
|
||||
|
||||
|
||||
async def coll_relationships() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.ARTIFACT_RELATIONSHIPS)
|
||||
|
||||
|
||||
async def coll_jobs() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.JOBS)
|
||||
|
||||
|
||||
async def coll_audit() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.AUDIT_EVENTS)
|
||||
|
||||
|
||||
async def coll_ai_exec() -> AsyncIterator[AsyncIOMotorCollection]:
|
||||
yield get_collection(MongoCollections.AI_EXECUTIONS)
|
||||
|
||||
|
||||
async def publisher() -> AsyncIterator[EventPublisher]:
|
||||
yield await get_event_publisher()
|
||||
|
||||
|
||||
__all__ = [
|
||||
"Settings",
|
||||
"get_settings",
|
||||
"RequestContext",
|
||||
"get_req_ctx",
|
||||
"coll_orgs",
|
||||
"coll_projects",
|
||||
"coll_artifacts",
|
||||
"coll_relationships",
|
||||
"coll_jobs",
|
||||
"coll_audit",
|
||||
"coll_ai_exec",
|
||||
"publisher",
|
||||
]
|
||||
Reference in New Issue
Block a user