Add transactional plugin runtime and PostgreSQL-first schema synchronization
This commit is contained in:
+16
-20
@@ -1,14 +1,13 @@
|
||||
"""JSON HTTP API, exposed as a WSGI application."""
|
||||
|
||||
from contextlib import closing
|
||||
import hmac
|
||||
from http import HTTPStatus
|
||||
import json
|
||||
import re
|
||||
from urllib.parse import parse_qs
|
||||
|
||||
from . import store
|
||||
from .core import discover
|
||||
from .core import Runtime
|
||||
from .publishing import bundled_plugins
|
||||
|
||||
|
||||
class RequestError(Exception):
|
||||
@@ -17,7 +16,7 @@ class RequestError(Exception):
|
||||
super().__init__(message)
|
||||
|
||||
|
||||
def read_draft(env, types):
|
||||
def read_draft(env):
|
||||
if env.get("CONTENT_TYPE", "").split(";")[0].strip().lower() != "application/json":
|
||||
raise RequestError(415, "expected application/json")
|
||||
length = int(env.get("CONTENT_LENGTH") or 0)
|
||||
@@ -28,20 +27,17 @@ def read_draft(env, types):
|
||||
payload = json.loads(env["wsgi.input"].read(length).decode("utf-8"))
|
||||
if not isinstance(payload, dict) or payload.keys() != {"type", "fields"}:
|
||||
raise ValueError("expected type and fields")
|
||||
kind = payload["type"]
|
||||
if not isinstance(kind, str) or kind not in types:
|
||||
raise ValueError("unknown content type")
|
||||
types[kind].validate(payload["fields"])
|
||||
return kind, payload["fields"]
|
||||
return payload["type"], payload["fields"]
|
||||
|
||||
|
||||
def create_app(database, write_token):
|
||||
def create_app(database, write_token, *, enabled=None):
|
||||
if not write_token or not write_token.isascii() or any(c.isspace() for c in write_token):
|
||||
raise ValueError("MICA_WRITE_TOKEN must be a non-empty ASCII token without spaces")
|
||||
types = discover()
|
||||
store.initialize(database)
|
||||
runtime = Runtime(database, bundled_plugins(), enabled=enabled)
|
||||
|
||||
def dispatch(env, db):
|
||||
def dispatch(env):
|
||||
if "content" not in runtime.registry.snippets:
|
||||
raise RequestError(404, "not found")
|
||||
method = env["REQUEST_METHOD"]
|
||||
path = env["PATH_INFO"]
|
||||
item = re.fullmatch(r"/content/([0-9a-f]{32})(/publish)?", path)
|
||||
@@ -55,9 +51,9 @@ def create_app(database, write_token):
|
||||
if not hmac.compare_digest(actual, ("Bearer " + write_token).encode("ascii")):
|
||||
return 401, {"error": "writer token required"}, [("WWW-Authenticate", "Bearer")]
|
||||
if item is None:
|
||||
kind, fields = read_draft(env, types)
|
||||
return 201, store.draft(db, kind, fields), []
|
||||
result = store.publish(db, item[1])
|
||||
kind, fields = read_draft(env)
|
||||
return 201, runtime.call("content.draft", kind=kind, fields=fields), []
|
||||
result = runtime.call("content.publish", identifier=item[1])
|
||||
elif item is None:
|
||||
query = parse_qs(env.get("QUERY_STRING", ""))
|
||||
kind = query.get("type", [None])[0]
|
||||
@@ -65,17 +61,16 @@ def create_app(database, write_token):
|
||||
offset = int(query.get("offset", ["0"])[0])
|
||||
if not (1 <= limit <= 100 and 0 <= offset <= 2**63 - 1):
|
||||
raise ValueError("invalid pagination")
|
||||
return 200, store.list_public(db, kind, limit, offset), []
|
||||
return 200, runtime.call("content.list", kind=kind, limit=limit, offset=offset), []
|
||||
else:
|
||||
result = store.get_public(db, item[1])
|
||||
result = runtime.call("content.get", identifier=item[1])
|
||||
if result is None:
|
||||
raise RequestError(404, "not found")
|
||||
return 200, result, []
|
||||
|
||||
def application(env, start_response):
|
||||
try:
|
||||
with closing(store.connect(database)) as db, db:
|
||||
status, payload, headers = dispatch(env, db)
|
||||
status, payload, headers = dispatch(env)
|
||||
except RequestError as error:
|
||||
status, payload, headers = error.status, {"error": str(error)}, []
|
||||
except (ValueError, RecursionError):
|
||||
@@ -86,4 +81,5 @@ def create_app(database, write_token):
|
||||
])
|
||||
return [body]
|
||||
|
||||
application.close = runtime.close
|
||||
return application
|
||||
|
||||
Reference in New Issue
Block a user