Implement OpenSearch api.

This commit is contained in:
FireMaskterK 2021-10-25 23:55:29 +01:00
parent b63786a15e
commit f731308dcc
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 17 additions and 0 deletions

View file

@ -145,6 +145,7 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
// TODO: Replace with opensearch, below, for caching reasons.
})).map(GET, "/suggestions", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.suggestionsResponse(request.getQueryParameter("query")),
@ -152,6 +153,14 @@ public class ServerLauncher extends MultithreadedHttpServerLauncher {
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/opensearch/suggestions", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(
ResponseHelper.opensearchSuggestionsResponse(request.getQueryParameter("query")),
"public, max-age=600");
} catch (Exception e) {
return getErrorResponse(e, request.getPath());
}
})).map(GET, "/search", AsyncServlet.ofBlocking(executor, request -> {
try {
return getJsonResponse(ResponseHelper.searchResponse(request.getQueryParameter("q"),

View file

@ -382,6 +382,14 @@ public class ResponseHelper {
}
public static final byte[] opensearchSuggestionsResponse(String query)
throws JsonProcessingException, IOException, ExtractionException {
return Constants.mapper.writeValueAsBytes(
Arrays.asList(query, YOUTUBE_SERVICE.getSuggestionExtractor().suggestionList(query)));
}
public static final byte[] searchResponse(String q, String filter)
throws IOException, ExtractionException, InterruptedException {