The inter-service data layer (fetch_data/call_action) was the least structured part of the codebase — Python _handlers dicts with ad-hoc param extraction scattered across 16 route files. This replaces them with declarative .sx query/action definitions that make the entire inter-service protocol self-describing and greppable. Infrastructure: - defquery/defaction special forms in the sx evaluator - Query/action registry with load, lookup, and schema introspection - Query executor using async_eval with I/O primitives - Blueprint factories (create_data_blueprint/create_action_blueprint) with sx-first dispatch and Python fallback - /internal/schema endpoint on every service - parse-datetime and split-ids primitives for type coercion Service extractions: - LikesService (toggle, is_liked, liked_slugs, liked_ids) - PageConfigService (ensure, get_by_container, get_by_id, get_batch, update) - RelationsService (wraps module-level functions) - AccountDataService (user_by_email, newsletters) - CartItemsService, MarketDataService (raw SQLAlchemy lookups) 50 of 54 handlers converted to sx, 4 Python fallbacks remain (ghost-sync/push-member, clear-cart-for-order, create-order). Net: -1,383 lines Python, +251 lines modified. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
"""Protocol manifest — aggregates /internal/schema from all services.
|
|
|
|
Can be used as a CLI tool or imported for dev-mode inspection.
|
|
|
|
Usage::
|
|
|
|
python -m shared.infrastructure.protocol_manifest
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import json
|
|
from typing import Any
|
|
|
|
from shared.infrastructure.data_client import fetch_data
|
|
|
|
|
|
# Service names that have inter-service protocols
|
|
_SERVICES = [
|
|
"blog", "market", "cart", "events", "account",
|
|
"likes", "relations", "orders",
|
|
]
|
|
|
|
|
|
async def fetch_service_schema(service: str) -> dict[str, Any] | None:
|
|
"""Fetch /internal/schema from a single service."""
|
|
try:
|
|
from shared.infrastructure.urls import service_url
|
|
import aiohttp
|
|
url = service_url(service, "/internal/schema")
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(url, timeout=aiohttp.ClientTimeout(total=3)) as resp:
|
|
if resp.status == 200:
|
|
return await resp.json()
|
|
except Exception:
|
|
return None
|
|
return None
|
|
|
|
|
|
async def generate_manifest() -> dict[str, Any]:
|
|
"""Fetch schemas from all services and produce a unified protocol map."""
|
|
results = await asyncio.gather(
|
|
*(fetch_service_schema(s) for s in _SERVICES),
|
|
return_exceptions=True,
|
|
)
|
|
manifest = {"services": {}}
|
|
for service, result in zip(_SERVICES, results):
|
|
if isinstance(result, dict):
|
|
manifest["services"][service] = result
|
|
else:
|
|
manifest["services"][service] = {"error": str(result) if isinstance(result, Exception) else "unavailable"}
|
|
return manifest
|
|
|
|
|
|
if __name__ == "__main__":
|
|
m = asyncio.run(generate_manifest())
|
|
print(json.dumps(m, indent=2))
|