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:
65
events/actions.sx
Normal file
65
events/actions.sx
Normal file
@@ -0,0 +1,65 @@
|
||||
;; Events service — inter-service action endpoints
|
||||
;;
|
||||
;; Each defaction replaces a Python handler in bp/actions/routes.py.
|
||||
;; The (service ...) primitive calls the registered CalendarService method
|
||||
;; with g.s (async session) + keyword args.
|
||||
|
||||
(defaction adjust-ticket-quantity (&key entry-id count user-id session-id ticket-type-id)
|
||||
"Add or remove tickets for a calendar entry."
|
||||
(do
|
||||
(service "calendar" "adjust-ticket-quantity"
|
||||
:entry-id entry-id :count count
|
||||
:user-id user-id :session-id session-id
|
||||
:ticket-type-id ticket-type-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction claim-entries-for-order (&key order-id user-id session-id page-post-id)
|
||||
"Claim pending calendar entries for an order."
|
||||
(do
|
||||
(service "calendar" "claim-entries-for-order"
|
||||
:order-id order-id :user-id user-id
|
||||
:session-id session-id :page-post-id page-post-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction claim-tickets-for-order (&key order-id user-id session-id page-post-id)
|
||||
"Claim pending tickets for an order."
|
||||
(do
|
||||
(service "calendar" "claim-tickets-for-order"
|
||||
:order-id order-id :user-id user-id
|
||||
:session-id session-id :page-post-id page-post-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction confirm-entries-for-order (&key order-id user-id session-id)
|
||||
"Confirm calendar entries after payment."
|
||||
(do
|
||||
(service "calendar" "confirm-entries-for-order"
|
||||
:order-id order-id :user-id user-id :session-id session-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction confirm-tickets-for-order (&key order-id)
|
||||
"Confirm tickets after payment."
|
||||
(do
|
||||
(service "calendar" "confirm-tickets-for-order" :order-id order-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction toggle-entry-post (&key entry-id content-type content-id)
|
||||
"Toggle association between a calendar entry and a content item."
|
||||
(let ((is-associated (service "calendar" "toggle-entry-post"
|
||||
:entry-id entry-id
|
||||
:content-type content-type
|
||||
:content-id content-id)))
|
||||
{"is_associated" is-associated}))
|
||||
|
||||
(defaction adopt-entries-for-user (&key user-id session-id)
|
||||
"Transfer anonymous calendar entries to a logged-in user."
|
||||
(do
|
||||
(service "calendar" "adopt-entries-for-user"
|
||||
:user-id user-id :session-id session-id)
|
||||
{"ok" true}))
|
||||
|
||||
(defaction adopt-tickets-for-user (&key user-id session-id)
|
||||
"Transfer anonymous tickets to a logged-in user."
|
||||
(do
|
||||
(service "calendar" "adopt-tickets-for-user"
|
||||
:user-id user-id :session-id session-id)
|
||||
{"ok" true}))
|
||||
@@ -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
|
||||
|
||||
@@ -1,148 +1,14 @@
|
||||
"""Events app data endpoints.
|
||||
|
||||
Exposes read-only JSON queries at ``/internal/data/<query_name>`` for
|
||||
cross-app callers via the internal data client.
|
||||
All queries are defined in ``events/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.contracts.dtos import dto_to_dict
|
||||
from shared.services.registry import services
|
||||
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)
|
||||
|
||||
# --- pending-entries ---
|
||||
async def _pending_entries():
|
||||
user_id = request.args.get("user_id", type=int)
|
||||
session_id = request.args.get("session_id")
|
||||
entries = await services.calendar.pending_entries(
|
||||
g.s, user_id=user_id, session_id=session_id,
|
||||
)
|
||||
return [dto_to_dict(e) for e in entries]
|
||||
|
||||
_handlers["pending-entries"] = _pending_entries
|
||||
|
||||
# --- pending-tickets ---
|
||||
async def _pending_tickets():
|
||||
user_id = request.args.get("user_id", type=int)
|
||||
session_id = request.args.get("session_id")
|
||||
tickets = await services.calendar.pending_tickets(
|
||||
g.s, user_id=user_id, session_id=session_id,
|
||||
)
|
||||
return [dto_to_dict(t) for t in tickets]
|
||||
|
||||
_handlers["pending-tickets"] = _pending_tickets
|
||||
|
||||
# --- entries-for-page ---
|
||||
async def _entries_for_page():
|
||||
page_id = request.args.get("page_id", type=int)
|
||||
user_id = request.args.get("user_id", type=int)
|
||||
session_id = request.args.get("session_id")
|
||||
entries = await services.calendar.entries_for_page(
|
||||
g.s, page_id, user_id=user_id, session_id=session_id,
|
||||
)
|
||||
return [dto_to_dict(e) for e in entries]
|
||||
|
||||
_handlers["entries-for-page"] = _entries_for_page
|
||||
|
||||
# --- tickets-for-page ---
|
||||
async def _tickets_for_page():
|
||||
page_id = request.args.get("page_id", type=int)
|
||||
user_id = request.args.get("user_id", type=int)
|
||||
session_id = request.args.get("session_id")
|
||||
tickets = await services.calendar.tickets_for_page(
|
||||
g.s, page_id, user_id=user_id, session_id=session_id,
|
||||
)
|
||||
return [dto_to_dict(t) for t in tickets]
|
||||
|
||||
_handlers["tickets-for-page"] = _tickets_for_page
|
||||
|
||||
# --- entries-for-order ---
|
||||
async def _entries_for_order():
|
||||
order_id = request.args.get("order_id", type=int)
|
||||
entries = await services.calendar.get_entries_for_order(g.s, order_id)
|
||||
return [dto_to_dict(e) for e in entries]
|
||||
|
||||
_handlers["entries-for-order"] = _entries_for_order
|
||||
|
||||
# --- tickets-for-order ---
|
||||
async def _tickets_for_order():
|
||||
order_id = request.args.get("order_id", type=int)
|
||||
tickets = await services.calendar.get_tickets_for_order(g.s, order_id)
|
||||
return [dto_to_dict(t) for t in tickets]
|
||||
|
||||
_handlers["tickets-for-order"] = _tickets_for_order
|
||||
|
||||
# --- entry-ids-for-content ---
|
||||
async def _entry_ids_for_content():
|
||||
content_type = request.args.get("content_type", "")
|
||||
content_id = request.args.get("content_id", type=int)
|
||||
ids = await services.calendar.entry_ids_for_content(g.s, content_type, content_id)
|
||||
return list(ids)
|
||||
|
||||
_handlers["entry-ids-for-content"] = _entry_ids_for_content
|
||||
|
||||
# --- associated-entries ---
|
||||
async def _associated_entries():
|
||||
content_type = request.args.get("content_type", "")
|
||||
content_id = request.args.get("content_id", type=int)
|
||||
page = request.args.get("page", 1, type=int)
|
||||
entries, has_more = await services.calendar.associated_entries(
|
||||
g.s, content_type, content_id, page,
|
||||
)
|
||||
return {"entries": [dto_to_dict(e) for e in entries], "has_more": has_more}
|
||||
|
||||
_handlers["associated-entries"] = _associated_entries
|
||||
|
||||
# --- calendars-for-container ---
|
||||
async def _calendars_for_container():
|
||||
container_type = request.args.get("type", "")
|
||||
container_id = request.args.get("id", type=int)
|
||||
calendars = await services.calendar.calendars_for_container(
|
||||
g.s, container_type, container_id,
|
||||
)
|
||||
return [dto_to_dict(c) for c in calendars]
|
||||
|
||||
_handlers["calendars-for-container"] = _calendars_for_container
|
||||
|
||||
# --- visible-entries-for-period ---
|
||||
async def _visible_entries_for_period():
|
||||
from datetime import datetime
|
||||
calendar_id = request.args.get("calendar_id", type=int)
|
||||
period_start = datetime.fromisoformat(request.args.get("period_start", ""))
|
||||
period_end = datetime.fromisoformat(request.args.get("period_end", ""))
|
||||
user_id = request.args.get("user_id", type=int)
|
||||
session_id = request.args.get("session_id")
|
||||
# is_admin determined server-side, never from client params
|
||||
is_admin = False
|
||||
entries = await services.calendar.visible_entries_for_period(
|
||||
g.s, calendar_id, period_start, period_end,
|
||||
user_id=user_id, is_admin=is_admin, session_id=session_id,
|
||||
)
|
||||
return [dto_to_dict(e) for e in entries]
|
||||
|
||||
_handlers["visible-entries-for-period"] = _visible_entries_for_period
|
||||
|
||||
bp, _handlers = create_data_blueprint("events")
|
||||
return bp
|
||||
|
||||
57
events/queries.sx
Normal file
57
events/queries.sx
Normal file
@@ -0,0 +1,57 @@
|
||||
;; Events service — inter-service data queries
|
||||
;;
|
||||
;; Each defquery replaces a Python handler in bp/data/routes.py.
|
||||
;; The (service ...) primitive calls the registered CalendarService method
|
||||
;; with g.s (async session) + keyword args, and auto-converts DTOs to dicts.
|
||||
|
||||
(defquery pending-entries (&key user-id session-id)
|
||||
"Calendar entries in pending state for a user or session."
|
||||
(service "calendar" "pending-entries"
|
||||
:user-id user-id :session-id session-id))
|
||||
|
||||
(defquery pending-tickets (&key user-id session-id)
|
||||
"Tickets in pending state for a user or session."
|
||||
(service "calendar" "pending-tickets"
|
||||
:user-id user-id :session-id session-id))
|
||||
|
||||
(defquery entries-for-page (&key page-id user-id session-id)
|
||||
"Calendar entries for a specific page."
|
||||
(service "calendar" "entries-for-page"
|
||||
:page-id page-id :user-id user-id :session-id session-id))
|
||||
|
||||
(defquery tickets-for-page (&key page-id user-id session-id)
|
||||
"Tickets for a specific page."
|
||||
(service "calendar" "tickets-for-page"
|
||||
:page-id page-id :user-id user-id :session-id session-id))
|
||||
|
||||
(defquery entries-for-order (&key order-id)
|
||||
"Calendar entries claimed by an order."
|
||||
(service "calendar" "get-entries-for-order" :order-id order-id))
|
||||
|
||||
(defquery tickets-for-order (&key order-id)
|
||||
"Tickets claimed by an order."
|
||||
(service "calendar" "get-tickets-for-order" :order-id order-id))
|
||||
|
||||
(defquery entry-ids-for-content (&key content-type content-id)
|
||||
"Entry IDs associated with a content item."
|
||||
(service "calendar" "entry-ids-for-content"
|
||||
:content-type content-type :content-id content-id))
|
||||
|
||||
(defquery associated-entries (&key content-type content-id page)
|
||||
"Entries associated with content, paginated."
|
||||
(let ((result (service "calendar" "associated-entries"
|
||||
:content-type content-type :content-id content-id :page page)))
|
||||
{"entries" (nth result 0) "has_more" (nth result 1)}))
|
||||
|
||||
(defquery calendars-for-container (&key type id)
|
||||
"Calendars attached to a container (page, marketplace, etc)."
|
||||
(service "calendar" "calendars-for-container"
|
||||
:container-type type :container-id id))
|
||||
|
||||
(defquery visible-entries-for-period (&key calendar-id period-start period-end user-id session-id)
|
||||
"Visible entries within a date range for a calendar."
|
||||
(service "calendar" "visible-entries-for-period"
|
||||
:calendar-id calendar-id
|
||||
:period-start (parse-datetime period-start)
|
||||
:period-end (parse-datetime period-end)
|
||||
:user-id user-id :is-admin false :session-id session-id))
|
||||
Reference in New Issue
Block a user