Replace inter-service _handlers dicts with declarative sx defquery/defaction
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m5s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 2m5s
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>
This commit is contained in:
36
relations/actions.sx
Normal file
36
relations/actions.sx
Normal file
@@ -0,0 +1,36 @@
|
||||
;; Relations service — inter-service action endpoints
|
||||
|
||||
(defaction attach-child (&key parent-type parent-id child-type child-id
|
||||
label sort-order relation-type metadata)
|
||||
"Create or revive a container relation."
|
||||
(service "relations" "attach-child"
|
||||
:parent-type parent-type :parent-id parent-id
|
||||
:child-type child-type :child-id child-id
|
||||
:label label :sort-order sort-order
|
||||
:relation-type relation-type :metadata metadata))
|
||||
|
||||
(defaction detach-child (&key parent-type parent-id child-type child-id relation-type)
|
||||
"Soft-delete a container relation."
|
||||
(let ((deleted (service "relations" "detach-child"
|
||||
:parent-type parent-type :parent-id parent-id
|
||||
:child-type child-type :child-id child-id
|
||||
:relation-type relation-type)))
|
||||
{"deleted" deleted}))
|
||||
|
||||
(defaction relate (&key relation-type from-id to-id label sort-order metadata)
|
||||
"Create a typed relation with registry validation and cardinality enforcement."
|
||||
(service "relations" "relate"
|
||||
:relation-type relation-type :from-id from-id :to-id to-id
|
||||
:label label :sort-order sort-order :metadata metadata))
|
||||
|
||||
(defaction unrelate (&key relation-type from-id to-id)
|
||||
"Remove a typed relation with registry validation."
|
||||
(let ((deleted (service "relations" "unrelate"
|
||||
:relation-type relation-type
|
||||
:from-id from-id :to-id to-id)))
|
||||
{"deleted" deleted}))
|
||||
|
||||
(defaction can-relate (&key relation-type from-id)
|
||||
"Check if a relation can be created (cardinality, registry validation)."
|
||||
(service "relations" "can-relate"
|
||||
:relation-type relation-type :from-id from-id))
|
||||
@@ -1,194 +1,14 @@
|
||||
"""Relations app action endpoints."""
|
||||
"""Relations app action endpoints.
|
||||
|
||||
All actions are defined in ``relations/actions.sx``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
from quart import Blueprint
|
||||
|
||||
from shared.infrastructure.actions import ACTION_HEADER
|
||||
from shared.infrastructure.query_blueprint import create_action_blueprint
|
||||
|
||||
|
||||
def register() -> Blueprint:
|
||||
bp = Blueprint("actions", __name__, url_prefix="/internal/actions")
|
||||
|
||||
@bp.before_request
|
||||
async def _require_action_header():
|
||||
if not request.headers.get(ACTION_HEADER):
|
||||
return jsonify({"error": "forbidden"}), 403
|
||||
from shared.infrastructure.internal_auth import validate_internal_request
|
||||
if not validate_internal_request():
|
||||
return jsonify({"error": "forbidden"}), 403
|
||||
|
||||
_handlers: dict[str, object] = {}
|
||||
|
||||
@bp.post("/<action_name>")
|
||||
async def handle_action(action_name: str):
|
||||
handler = _handlers.get(action_name)
|
||||
if handler is None:
|
||||
return jsonify({"error": "unknown action"}), 404
|
||||
try:
|
||||
result = await handler()
|
||||
return jsonify(result)
|
||||
except Exception as exc:
|
||||
import logging
|
||||
logging.getLogger(__name__).exception("Action %s failed", action_name)
|
||||
return jsonify({"error": str(exc)}), 500
|
||||
|
||||
# --- attach-child ---
|
||||
async def _attach_child():
|
||||
"""Create or revive a ContainerRelation."""
|
||||
from shared.services.relationships import attach_child
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
rel = await attach_child(
|
||||
g.s,
|
||||
parent_type=data["parent_type"],
|
||||
parent_id=data["parent_id"],
|
||||
child_type=data["child_type"],
|
||||
child_id=data["child_id"],
|
||||
label=data.get("label"),
|
||||
sort_order=data.get("sort_order"),
|
||||
relation_type=data.get("relation_type"),
|
||||
metadata=data.get("metadata"),
|
||||
)
|
||||
return {
|
||||
"id": rel.id,
|
||||
"parent_type": rel.parent_type,
|
||||
"parent_id": rel.parent_id,
|
||||
"child_type": rel.child_type,
|
||||
"child_id": rel.child_id,
|
||||
"sort_order": rel.sort_order,
|
||||
"relation_type": rel.relation_type,
|
||||
}
|
||||
|
||||
_handlers["attach-child"] = _attach_child
|
||||
|
||||
# --- detach-child ---
|
||||
async def _detach_child():
|
||||
"""Soft-delete a ContainerRelation."""
|
||||
from shared.services.relationships import detach_child
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
deleted = await detach_child(
|
||||
g.s,
|
||||
parent_type=data["parent_type"],
|
||||
parent_id=data["parent_id"],
|
||||
child_type=data["child_type"],
|
||||
child_id=data["child_id"],
|
||||
relation_type=data.get("relation_type"),
|
||||
)
|
||||
return {"deleted": deleted}
|
||||
|
||||
_handlers["detach-child"] = _detach_child
|
||||
|
||||
# --- relate (registry-aware) ---
|
||||
async def _relate():
|
||||
"""Create a typed relation with registry validation and cardinality enforcement."""
|
||||
from shared.services.relationships import attach_child, get_children
|
||||
from shared.sx.relations import get_relation
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
rel_type = data.get("relation_type")
|
||||
if not rel_type:
|
||||
return {"error": "relation_type is required"}, 400
|
||||
|
||||
defn = get_relation(rel_type)
|
||||
if defn is None:
|
||||
return {"error": f"unknown relation_type: {rel_type}"}, 400
|
||||
|
||||
from_id = data["from_id"]
|
||||
to_id = data["to_id"]
|
||||
|
||||
# Cardinality enforcement
|
||||
if defn.cardinality == "one-to-one":
|
||||
existing = await get_children(
|
||||
g.s,
|
||||
parent_type=defn.from_type,
|
||||
parent_id=from_id,
|
||||
child_type=defn.to_type,
|
||||
relation_type=rel_type,
|
||||
)
|
||||
if existing:
|
||||
return {"error": "one-to-one relation already exists", "existing_id": existing[0].child_id}, 409
|
||||
|
||||
rel = await attach_child(
|
||||
g.s,
|
||||
parent_type=defn.from_type,
|
||||
parent_id=from_id,
|
||||
child_type=defn.to_type,
|
||||
child_id=to_id,
|
||||
label=data.get("label"),
|
||||
sort_order=data.get("sort_order"),
|
||||
relation_type=rel_type,
|
||||
metadata=data.get("metadata"),
|
||||
)
|
||||
return {
|
||||
"id": rel.id,
|
||||
"relation_type": rel.relation_type,
|
||||
"parent_type": rel.parent_type,
|
||||
"parent_id": rel.parent_id,
|
||||
"child_type": rel.child_type,
|
||||
"child_id": rel.child_id,
|
||||
"sort_order": rel.sort_order,
|
||||
}
|
||||
|
||||
_handlers["relate"] = _relate
|
||||
|
||||
# --- unrelate (registry-aware) ---
|
||||
async def _unrelate():
|
||||
"""Remove a typed relation with registry validation."""
|
||||
from shared.services.relationships import detach_child
|
||||
from shared.sx.relations import get_relation
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
rel_type = data.get("relation_type")
|
||||
if not rel_type:
|
||||
return {"error": "relation_type is required"}, 400
|
||||
|
||||
defn = get_relation(rel_type)
|
||||
if defn is None:
|
||||
return {"error": f"unknown relation_type: {rel_type}"}, 400
|
||||
|
||||
deleted = await detach_child(
|
||||
g.s,
|
||||
parent_type=defn.from_type,
|
||||
parent_id=data["from_id"],
|
||||
child_type=defn.to_type,
|
||||
child_id=data["to_id"],
|
||||
relation_type=rel_type,
|
||||
)
|
||||
return {"deleted": deleted}
|
||||
|
||||
_handlers["unrelate"] = _unrelate
|
||||
|
||||
# --- can-relate (pre-flight check) ---
|
||||
async def _can_relate():
|
||||
"""Check if a relation can be created (cardinality, registry validation)."""
|
||||
from shared.services.relationships import get_children
|
||||
from shared.sx.relations import get_relation
|
||||
|
||||
data = await request.get_json(force=True)
|
||||
rel_type = data.get("relation_type")
|
||||
if not rel_type:
|
||||
return {"error": "relation_type is required"}, 400
|
||||
|
||||
defn = get_relation(rel_type)
|
||||
if defn is None:
|
||||
return {"allowed": False, "reason": f"unknown relation_type: {rel_type}"}
|
||||
|
||||
from_id = data["from_id"]
|
||||
|
||||
if defn.cardinality == "one-to-one":
|
||||
existing = await get_children(
|
||||
g.s,
|
||||
parent_type=defn.from_type,
|
||||
parent_id=from_id,
|
||||
child_type=defn.to_type,
|
||||
relation_type=rel_type,
|
||||
)
|
||||
if existing:
|
||||
return {"allowed": False, "reason": "one-to-one relation already exists"}
|
||||
|
||||
return {"allowed": True}
|
||||
|
||||
_handlers["can-relate"] = _can_relate
|
||||
|
||||
bp, _handlers = create_action_blueprint("relations")
|
||||
return bp
|
||||
|
||||
@@ -1,77 +1,14 @@
|
||||
"""Relations app data endpoints."""
|
||||
"""Relations app data endpoints.
|
||||
|
||||
All queries are defined in ``relations/queries.sx``.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
from quart import Blueprint
|
||||
|
||||
from shared.infrastructure.data_client import DATA_HEADER
|
||||
from shared.infrastructure.query_blueprint import create_data_blueprint
|
||||
|
||||
|
||||
def register() -> Blueprint:
|
||||
bp = Blueprint("data", __name__, url_prefix="/internal/data")
|
||||
|
||||
@bp.before_request
|
||||
async def _require_data_header():
|
||||
if not request.headers.get(DATA_HEADER):
|
||||
return jsonify({"error": "forbidden"}), 403
|
||||
from shared.infrastructure.internal_auth import validate_internal_request
|
||||
if not validate_internal_request():
|
||||
return jsonify({"error": "forbidden"}), 403
|
||||
|
||||
_handlers: dict[str, object] = {}
|
||||
|
||||
@bp.get("/<query_name>")
|
||||
async def handle_query(query_name: str):
|
||||
handler = _handlers.get(query_name)
|
||||
if handler is None:
|
||||
return jsonify({"error": "unknown query"}), 404
|
||||
result = await handler()
|
||||
return jsonify(result)
|
||||
|
||||
# --- get-children ---
|
||||
async def _get_children():
|
||||
"""Return ContainerRelation children for a parent."""
|
||||
from shared.services.relationships import get_children
|
||||
|
||||
parent_type = request.args.get("parent_type", "")
|
||||
parent_id = request.args.get("parent_id", type=int)
|
||||
child_type = request.args.get("child_type")
|
||||
relation_type = request.args.get("relation_type")
|
||||
if not parent_type or parent_id is None:
|
||||
return []
|
||||
rels = await get_children(g.s, parent_type, parent_id, child_type, relation_type=relation_type)
|
||||
return [_serialize_rel(r) for r in rels]
|
||||
|
||||
_handlers["get-children"] = _get_children
|
||||
|
||||
# --- get-parents ---
|
||||
async def _get_parents():
|
||||
"""Return ContainerRelation parents for a child."""
|
||||
from shared.services.relationships import get_parents
|
||||
|
||||
child_type = request.args.get("child_type", "")
|
||||
child_id = request.args.get("child_id", type=int)
|
||||
parent_type = request.args.get("parent_type")
|
||||
relation_type = request.args.get("relation_type")
|
||||
if not child_type or child_id is None:
|
||||
return []
|
||||
rels = await get_parents(g.s, child_type, child_id, parent_type, relation_type=relation_type)
|
||||
return [_serialize_rel(r) for r in rels]
|
||||
|
||||
_handlers["get-parents"] = _get_parents
|
||||
|
||||
bp, _handlers = create_data_blueprint("relations")
|
||||
return bp
|
||||
|
||||
|
||||
def _serialize_rel(r):
|
||||
"""Serialize a ContainerRelation to a dict."""
|
||||
return {
|
||||
"id": r.id,
|
||||
"parent_type": r.parent_type,
|
||||
"parent_id": r.parent_id,
|
||||
"child_type": r.child_type,
|
||||
"child_id": r.child_id,
|
||||
"sort_order": r.sort_order,
|
||||
"label": r.label,
|
||||
"relation_type": r.relation_type,
|
||||
"metadata": r.metadata_,
|
||||
}
|
||||
|
||||
13
relations/queries.sx
Normal file
13
relations/queries.sx
Normal file
@@ -0,0 +1,13 @@
|
||||
;; Relations service — inter-service data queries
|
||||
|
||||
(defquery get-children (&key parent-type parent-id child-type relation-type)
|
||||
"Return child relations for a parent."
|
||||
(service "relations" "get-children"
|
||||
:parent-type parent-type :parent-id parent-id
|
||||
:child-type child-type :relation-type relation-type))
|
||||
|
||||
(defquery get-parents (&key child-type child-id parent-type relation-type)
|
||||
"Return parent relations for a child."
|
||||
(service "relations" "get-parents"
|
||||
:child-type child-type :child-id child-id
|
||||
:parent-type parent-type :relation-type relation-type))
|
||||
@@ -4,3 +4,6 @@ from __future__ import annotations
|
||||
|
||||
def register_domain_services() -> None:
|
||||
"""Register services for the relations app."""
|
||||
from shared.services.registry import services
|
||||
from shared.services.relations_impl import SqlRelationsService
|
||||
services.register("relations", SqlRelationsService())
|
||||
|
||||
Reference in New Issue
Block a user