diff options
| author | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-07-30 02:19:32 +1000 |
|---|---|---|
| committer | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-07-30 02:19:32 +1000 |
| commit | d4445fe189480e272ad247860255194d413095dd (patch) | |
| tree | e08ffbc07d27ebd6f6af677c7ee8ef7347ac1792 /src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java | |
| parent | 6115246bf36fb98037ca0df2faac869eba7845c3 (diff) | |
| download | notenoughupdates-d4445fe189480e272ad247860255194d413095dd.tar.gz notenoughupdates-d4445fe189480e272ad247860255194d413095dd.tar.bz2 notenoughupdates-d4445fe189480e272ad247860255194d413095dd.zip | |
1.0.3
Diffstat (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java')
| -rw-r--r-- | src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java | 35 |
1 files changed, 26 insertions, 9 deletions
diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java index 2769e18e..ca6c30af 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java @@ -17,13 +17,9 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.function.Consumer; +import java.util.zip.GZIPInputStream; public class HypixelApi { - - /** - * Not currently used as of BETA-1.6. - */ - private Gson gson = new Gson(); private ExecutorService es = Executors.newFixedThreadPool(3); @@ -32,20 +28,30 @@ public class HypixelApi { } public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer, Runnable error) { - getHypixelApiAsync(generateApiUrl(apiKey.trim(), method, args), consumer, error); + getApiAsync(generateApiUrl(apiKey.trim(), method, args), consumer, error); + } + + public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiSync(urlS)); + } catch(IOException e) { + error.run(); + } + }); } - public void getHypixelApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + public void getApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { es.submit(() -> { try { - consumer.accept(getHypixelApiSync(urlS)); + consumer.accept(getApiGZIPSync(urlS)); } catch(IOException e) { error.run(); } }); } - public JsonObject getHypixelApiSync(String urlS) throws IOException { + public JsonObject getApiSync(String urlS) throws IOException { URL url = new URL(urlS); URLConnection connection = url.openConnection(); connection.setConnectTimeout(3000); @@ -56,6 +62,17 @@ public class HypixelApi { return json; } + public JsonObject getApiGZIPSync(String urlS) throws IOException { + URL url = new URL(urlS); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(3000); + + String response = IOUtils.toString(new GZIPInputStream(connection.getInputStream()), StandardCharsets.UTF_8); + + JsonObject json = gson.fromJson(response, JsonObject.class); + return json; + } + public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) { StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method + "?key=" + apiKey); for(Map.Entry<String, String> entry : args.entrySet()) { |
