Integrate Antirion admin with FastAPI and htmx
Add the shared document API, constructor and expert editors, isolated preview, and fragment catalog metadata with typed block IDs. Include design references, specification 001, QA scripts, project guidance, and cached Docker dependency installation. Leave example sites for a separate commit.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
"""Request-scoped in-process API credentials never reach browser HTML."""
|
||||
|
||||
import secrets
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
class ApiError(Exception):
|
||||
def __init__(self, status: int, data: dict):
|
||||
self.status, self.data = status, data
|
||||
super().__init__(data.get("error", {}).get("message", "Ошибка API"))
|
||||
|
||||
|
||||
class AdminApi:
|
||||
def __init__(self, client):
|
||||
self.client = client
|
||||
|
||||
async def call(self, method, path, data=None):
|
||||
response = await self.client.request(method, "/admin/api/v1" + path, json=data)
|
||||
if response.status_code >= 400:
|
||||
raise ApiError(response.status_code, response.json())
|
||||
return response.json()
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def api_for(request, cookie, ip):
|
||||
token = secrets.token_urlsafe(32)
|
||||
request.app.state.api_credentials[token] = (cookie, ip)
|
||||
try:
|
||||
async with httpx.AsyncClient(
|
||||
transport=httpx.ASGITransport(app=request.app),
|
||||
base_url="http://internal",
|
||||
headers={"Authorization": "Bearer " + token},
|
||||
) as client:
|
||||
yield AdminApi(client)
|
||||
finally:
|
||||
request.app.state.api_credentials.pop(token, None)
|
||||
Reference in New Issue
Block a user