mirror of
https://github.com/Sea-Haven-Industries/meal-order-manager.git
synced 2026-10-07 00:01:58 +00:00
fix(api): return JSON objects and stop logging job payloads
Flask now jsonify-s handler dicts so API responses are not HTML, and the worker logs only event and status.
This commit is contained in:
parent
d7ee6a3b74
commit
af2743f23c
3 changed files with 34 additions and 7 deletions
|
|
@ -9,6 +9,8 @@ from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
from flask import Flask, Response, jsonify, request, send_file
|
from flask import Flask, Response, jsonify, request, send_file
|
||||||
|
|
||||||
from server import http_api
|
from server import http_api
|
||||||
|
|
@ -94,11 +96,19 @@ def create_app() -> Flask:
|
||||||
"queryStringParameters": qs or None,
|
"queryStringParameters": qs or None,
|
||||||
}
|
}
|
||||||
result = http_api.lambda_handler(event, None)
|
result = http_api.lambda_handler(event, None)
|
||||||
return Response(
|
try:
|
||||||
result["body"],
|
payload = json.loads(result["body"])
|
||||||
status=result["statusCode"],
|
except (TypeError, KeyError, json.JSONDecodeError):
|
||||||
headers=result.get("headers") or {"Content-Type": "application/json"},
|
resp = jsonify({"error": "Internal error"})
|
||||||
)
|
resp.status_code = 500
|
||||||
|
return resp
|
||||||
|
if not isinstance(payload, dict):
|
||||||
|
resp = jsonify({"error": "Internal error"})
|
||||||
|
resp.status_code = 500
|
||||||
|
return resp
|
||||||
|
resp = jsonify(payload)
|
||||||
|
resp.status_code = int(result.get("statusCode") or 500)
|
||||||
|
return resp
|
||||||
|
|
||||||
@app.route("/api/<path:rest>", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
@app.route("/api/<path:rest>", methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"])
|
||||||
def api(rest: str):
|
def api(rest: str):
|
||||||
|
|
|
||||||
|
|
@ -48,8 +48,9 @@ def main() -> None:
|
||||||
try:
|
try:
|
||||||
payload = json.loads(msg["Body"])
|
payload = json.loads(msg["Body"])
|
||||||
result = run_job(payload)
|
result = run_job(payload)
|
||||||
logger.info("job result %s", result)
|
status = result.get("status") if isinstance(result, dict) else None
|
||||||
if result.get("status") == "skipped":
|
logger.info("job event=%s status=%s", payload.get("event"), status)
|
||||||
|
if status == "skipped":
|
||||||
# Leave the message visible after timeout so a later
|
# Leave the message visible after timeout so a later
|
||||||
# receive can run it. Deleting here dropped the only
|
# receive can run it. Deleting here dropped the only
|
||||||
# weekly close/reminder when delivery landed late.
|
# weekly close/reminder when delivery landed late.
|
||||||
|
|
|
||||||
|
|
@ -29,3 +29,19 @@ def test_options_preflight_from_portal_origin():
|
||||||
== "https://internal.seahaven.com"
|
== "https://internal.seahaven.com"
|
||||||
)
|
)
|
||||||
assert "Authorization" in response.headers["Access-Control-Allow-Headers"]
|
assert "Authorization" in response.headers["Access-Control-Allow-Headers"]
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_dispatch_jsonifies_handler_body():
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
with patch("server.http_api.lambda_handler") as mock_handler:
|
||||||
|
mock_handler.return_value = {
|
||||||
|
"statusCode": 400,
|
||||||
|
"headers": {"Content-Type": "application/json"},
|
||||||
|
"body": '{"error": "Bad request"}',
|
||||||
|
}
|
||||||
|
client = create_app().test_client()
|
||||||
|
response = client.get("/api/menu")
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.get_json() == {"error": "Bad request"}
|
||||||
|
assert response.content_type.startswith("application/json")
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue