Skip to content

Commit

Permalink
Merge pull request #341 from hearchco/as/fix/suggestions-api
Browse files Browse the repository at this point in the history
fix(suggestions): x-suggestions api
  • Loading branch information
aleksasiriski authored Jun 29, 2024
2 parents 511c742 + abbcc28 commit 4ab4996
Showing 1 changed file with 10 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)

Expand Down

0 comments on commit 4ab4996

Please sign in to comment.