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:
@@ -1,139 +1,15 @@
|
||||
"""Events app action endpoints.
|
||||
|
||||
Exposes write operations at ``/internal/actions/<action_name>`` for
|
||||
cross-app callers (cart, blog) via the internal action client.
|
||||
All actions are defined declaratively in ``events/actions.sx`` and
|
||||
dispatched via the sx query registry. No Python fallbacks needed.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
from shared.infrastructure.query_blueprint import create_action_blueprint
|
||||
|
||||
from shared.infrastructure.actions import ACTION_HEADER
|
||||
from shared.services.registry import services
|
||||
from quart import 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
|
||||
|
||||
# --- adjust-ticket-quantity ---
|
||||
async def _adjust_ticket_quantity():
|
||||
data = await request.get_json()
|
||||
await services.calendar.adjust_ticket_quantity(
|
||||
g.s,
|
||||
data["entry_id"],
|
||||
data["count"],
|
||||
user_id=data.get("user_id"),
|
||||
session_id=data.get("session_id"),
|
||||
ticket_type_id=data.get("ticket_type_id"),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["adjust-ticket-quantity"] = _adjust_ticket_quantity
|
||||
|
||||
# --- claim-entries-for-order ---
|
||||
async def _claim_entries():
|
||||
data = await request.get_json()
|
||||
await services.calendar.claim_entries_for_order(
|
||||
g.s,
|
||||
data["order_id"],
|
||||
data.get("user_id"),
|
||||
data.get("session_id"),
|
||||
data.get("page_post_id"),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["claim-entries-for-order"] = _claim_entries
|
||||
|
||||
# --- claim-tickets-for-order ---
|
||||
async def _claim_tickets():
|
||||
data = await request.get_json()
|
||||
await services.calendar.claim_tickets_for_order(
|
||||
g.s,
|
||||
data["order_id"],
|
||||
data.get("user_id"),
|
||||
data.get("session_id"),
|
||||
data.get("page_post_id"),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["claim-tickets-for-order"] = _claim_tickets
|
||||
|
||||
# --- confirm-entries-for-order ---
|
||||
async def _confirm_entries():
|
||||
data = await request.get_json()
|
||||
await services.calendar.confirm_entries_for_order(
|
||||
g.s,
|
||||
data["order_id"],
|
||||
data.get("user_id"),
|
||||
data.get("session_id"),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["confirm-entries-for-order"] = _confirm_entries
|
||||
|
||||
# --- confirm-tickets-for-order ---
|
||||
async def _confirm_tickets():
|
||||
data = await request.get_json()
|
||||
await services.calendar.confirm_tickets_for_order(
|
||||
g.s, data["order_id"],
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["confirm-tickets-for-order"] = _confirm_tickets
|
||||
|
||||
# --- toggle-entry-post ---
|
||||
async def _toggle_entry_post():
|
||||
data = await request.get_json()
|
||||
is_associated = await services.calendar.toggle_entry_post(
|
||||
g.s,
|
||||
data["entry_id"],
|
||||
data["content_type"],
|
||||
data["content_id"],
|
||||
)
|
||||
return {"is_associated": is_associated}
|
||||
|
||||
_handlers["toggle-entry-post"] = _toggle_entry_post
|
||||
|
||||
# --- adopt-entries-for-user ---
|
||||
async def _adopt_entries():
|
||||
data = await request.get_json()
|
||||
await services.calendar.adopt_entries_for_user(
|
||||
g.s, data["user_id"], data["session_id"],
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["adopt-entries-for-user"] = _adopt_entries
|
||||
|
||||
# --- adopt-tickets-for-user ---
|
||||
async def _adopt_tickets():
|
||||
data = await request.get_json()
|
||||
await services.calendar.adopt_tickets_for_user(
|
||||
g.s, data["user_id"], data["session_id"],
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
_handlers["adopt-tickets-for-user"] = _adopt_tickets
|
||||
|
||||
bp, _handlers = create_action_blueprint("events")
|
||||
return bp
|
||||
|
||||
Reference in New Issue
Block a user