Add the bounded OpenFIGI v3 adapter, authenticated search route, and server-side configuration. Refs #3 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/httputil"
|
|
"github.com/tradarr/backend/internal/indices"
|
|
)
|
|
|
|
func (h *Handler) SetIndexSearcher(searcher indices.IndexSearcher) { h.indexSearcher = searcher }
|
|
func (h *Handler) SearchIndices(c *gin.Context) {
|
|
query := strings.TrimSpace(c.Query("q"))
|
|
limit := 10
|
|
if values, exists := c.Request.URL.Query()["limit"]; exists {
|
|
if len(values) != 1 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid search parameters"})
|
|
return
|
|
}
|
|
value := values[0]
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid search parameters"})
|
|
return
|
|
}
|
|
limit = parsed
|
|
}
|
|
if !indices.ValidQuery(query, limit) {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid search parameters"})
|
|
return
|
|
}
|
|
if h.indexSearcher == nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": "index search unavailable"})
|
|
return
|
|
}
|
|
result, err := h.indexSearcher.Search(c.Request.Context(), query, limit)
|
|
if err != nil {
|
|
status := http.StatusBadGateway
|
|
message := "index search unavailable"
|
|
if errors.Is(err, indices.ErrRateLimited) {
|
|
status = http.StatusTooManyRequests
|
|
message = "index search rate limited"
|
|
} else if errors.Is(err, context.DeadlineExceeded) {
|
|
status = http.StatusGatewayTimeout
|
|
message = "index search timed out"
|
|
}
|
|
c.JSON(status, gin.H{"error": message})
|
|
return
|
|
}
|
|
httputil.OK(c, result)
|
|
}
|