feat: proxy PreZero priority endpoints to FastAPI
ci / check (push) Successful in 58s
ci / check (pull_request) Successful in 55s

This commit is contained in:
Noel Sabia committed 2026-11-14 17:50:53 +01:00
1 parent a0ad60ec53
commit 7e9c2d9663
5 files changed
+564 -11

No files matched your search

+97
View File
@@ -0,0 +1,97 @@
package api
import (
"log/slog"
"net/http"
"strconv"
"pulsecast/api/internal/model"
)
// PreZero query defaults and bounds from PREZERO_API_CONTRACT.md §3.
const (
defaultPreZeroLimit = 50
maxPreZeroLimit = 500
)
// PreZeroPriorities are the accepted tier filters.
var PreZeroPriorities = []string{"high", "medium", "low"}
// handlePreZeroSummary proxies GET /api/prezero/summary to FastAPI.
func (s *Server) handlePreZeroSummary(w http.ResponseWriter, r *http.Request) {
if s.prezero == nil {
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI PreZero client is not configured")
return
}
status, body, err := s.prezero.PreZeroSummary(r.Context())
if err != nil {
slog.Warn("prezero summary proxy failed", "error", err)
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
return
}
relayMLResponse(w, status, body)
}
// handlePreZeroPriority proxies GET /api/prezero/priority. The query is
// validated here so bad input never reaches FastAPI.
func (s *Server) handlePreZeroPriority(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query()
priority := query.Get("priority")
if priority != "" && !model.Contains(PreZeroPriorities, priority) {
writeError(w, http.StatusBadRequest, "bad_request", "priority must be high, medium or low")
return
}
limit := defaultPreZeroLimit
if raw := query.Get("limit"); raw != "" {
parsed, err := strconv.Atoi(raw)
if err != nil || parsed < 1 || parsed > maxPreZeroLimit {
writeError(w, http.StatusBadRequest, "bad_request", "limit must be an integer between 1 and 500")
return
}
limit = parsed
}
offset := 0
if raw := query.Get("offset"); raw != "" {
parsed, err := strconv.Atoi(raw)
if err != nil || parsed < 0 {
writeError(w, http.StatusBadRequest, "bad_request", "offset must be an integer greater than or equal to 0")
return
}
offset = parsed
}
if s.prezero == nil {
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI PreZero client is not configured")
return
}
status, body, err := s.prezero.PreZeroPriority(r.Context(), priority, limit, offset)
if err != nil {
slog.Warn("prezero priority proxy failed", "priority", priority, "error", err)
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
return
}
relayMLResponse(w, status, body)
}
// handlePreZeroContainer proxies GET /api/prezero/containers/{location_id}.
func (s *Server) handlePreZeroContainer(w http.ResponseWriter, r *http.Request) {
locationID := r.PathValue("location_id")
if locationID == "" {
writeError(w, http.StatusNotFound, "not_found", "container not found")
return
}
if s.prezero == nil {
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI PreZero client is not configured")
return
}
status, body, err := s.prezero.PreZeroContainer(r.Context(), locationID)
if err != nil {
slog.Warn("prezero container proxy failed", "location_id", locationID, "error", err)
writeError(w, http.StatusBadGateway, "ml_unavailable", "FastAPI is unavailable")
return
}
relayMLResponse(w, status, body)
}