Repository navigation
Expand file tree
/
Copy pathapi.py
More file actions
80 lines (61 loc) · 2.43 KB
/
Copy pathapi.py
File metadata and controls
80 lines (61 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""HTTP surface — two routers, two prefixes.
Rule 1 of plugin views: the page is mounted on the PUBLIC prefix (an iframe
navigation can't carry a bearer token), and everything it reads lives under the
GATED ``/api`` prefix, where the operator bearer applies. Keeping them in separate
routers is what makes that split real rather than aspirational.
"""
from __future__ import annotations
import json
def build_view_router():
"""The public page router — mounted at /plugins/social."""
from fastapi import APIRouter
from fastapi.responses import HTMLResponse
from .view import PAGE
router = APIRouter()
@router.get("/view", response_class=HTMLResponse)
async def _view() -> HTMLResponse:
return HTMLResponse(PAGE)
return router
def build_data_router():
"""The gated data router — mounted at /api/plugins/social."""
from fastapi import APIRouter
from fastapi.responses import JSONResponse
from . import brandkit, store
router = APIRouter()
@router.get("/queue")
async def _queue() -> JSONResponse:
brand = ""
try:
brand = brandkit.brand_name(brandkit.load())
except Exception: # noqa: BLE001 — a broken kit shouldn't blank the board
brand = ""
return JSONResponse(
{
"counts": store.counts(),
"posts": store.list_posts(limit=200),
"pillars": store.pillar_balance(),
"brand": brand,
"hold": store.hold_state(),
}
)
@router.get("/brand-kit")
async def _brand_kit() -> JSONResponse:
try:
kit = brandkit.load() or {}
return JSONResponse(
{
"exists": brandkit.exists(),
"kit": kit,
"visual": brandkit.visual(kit),
"visual_warnings": [w[5:].strip() for w in brandkit.validate_visual(kit)],
}
)
except Exception as e: # noqa: BLE001 — report the parse error to the panel
return JSONResponse(
{"exists": brandkit.exists(), "kit": {}, "visual": {}, "visual_warnings": [], "error": str(e)}
)
return router
def queue_json() -> str:
"""The board snapshot as JSON — handy for tests and headless callers."""
from . import store
return json.dumps({"counts": store.counts(), "posts": store.list_posts(limit=200)})