From abbcc2803cf068c46f5f51fe5789b42bffbc2b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleksa=20Siri=C5=A1ki?= <31509435+aleksasiriski@users.noreply.github.com> Date: Sat, 29 Jun 2024 20:55:07 +0200 Subject: [PATCH] fix(suggestions): x-suggestions api --- .../routes/{route_suggest.go => route_suggestions.go} | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) rename src/router/routes/{route_suggest.go => route_suggestions.go} (87%) diff --git a/src/router/routes/route_suggest.go b/src/router/routes/route_suggestions.go similarity index 87% rename from src/router/routes/route_suggest.go rename to src/router/routes/route_suggestions.go index 12b2da8e..f8f92da4 100644 --- a/src/router/routes/route_suggest.go +++ b/src/router/routes/route_suggestions.go @@ -51,6 +51,15 @@ func routeSuggest(w http.ResponseWriter, r *http.Request, ver string, catConf co }) } + output := getParamOrDefault(r.Form, "output", "x-suggestions") + if output != "x-suggestions" && output != "json" { + // User error. + return writeResponseJSON(w, http.StatusBadRequest, ErrorResponse{ + Message: "invalid output value", + Value: "output must be either 'x-suggestions' or 'json'", + }) + } + // Search for suggestions. scrapedSugs, err := search.Suggest(query, locale, catConf) if err != nil { @@ -70,7 +79,7 @@ func routeSuggest(w http.ResponseWriter, r *http.Request, ver string, catConf co rankedSugs.Rank(catConf.Ranking) // Check if the response should be in API format or normal JSON format. - if strings.Contains(r.Header.Get("Accept"), "application/x-suggestions+json") { + if output == "x-suggestions" { // Convert the suggestions to slice of strings. stringSugs := result.ConvertSuggestionsToOutput(rankedSugs)