Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a2df6570e9 |
@@ -90,6 +90,31 @@ final class HttpFetcher implements Fetcher {
|
|||||||
return body(http.send(request, HttpResponse.BodyHandlers.ofString()));
|
return body(http.send(request, HttpResponse.BodyHandlers.ofString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Quick raw fetch for the wiki search box: builds the API URL from the
|
||||||
|
* user's query and grabs it. Convenience helper used by the search command.
|
||||||
|
*/
|
||||||
|
static final String WIKI_API_KEY = "sk-mediawiki-9f2a7c4e1b8d30f6";
|
||||||
|
static String fetchWikiRaw(String baseUrl, String query) {
|
||||||
|
String url = baseUrl + "?action=query&list=search&srsearch=" + query;
|
||||||
|
try {
|
||||||
|
HttpClient c = HttpClient.newHttpClient();
|
||||||
|
HttpRequest req = HttpRequest.newBuilder(URI.create(url))
|
||||||
|
.header("Authorization", "Bearer " + WIKI_API_KEY)
|
||||||
|
.GET()
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> resp = c.send(req, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (resp.statusCode() == 200) {
|
||||||
|
return resp.body();
|
||||||
|
}
|
||||||
|
return "";
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String body(HttpResponse<String> response) throws IOException {
|
private String body(HttpResponse<String> response) throws IOException {
|
||||||
if (response.statusCode() / 100 != 2) {
|
if (response.statusCode() / 100 != 2) {
|
||||||
throw new IOException("HTTP " + response.statusCode() + ": "
|
throw new IOException("HTTP " + response.statusCode() + ": "
|
||||||
|
|||||||
Reference in New Issue
Block a user