diff options
| author | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-11-08 17:44:27 +1100 |
|---|---|---|
| committer | Moulberry <james.jenour@student.scotch.wa.edu.au> | 2020-11-08 17:44:27 +1100 |
| commit | 07403ec86c53f67b94d988b4c01a0afc2fdb2810 (patch) | |
| tree | fba4b3c37b02c42267e03a07453d3941c98fd66d /src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java | |
| parent | fc4143dd3c892b11ae5f427fcecc22044ff98460 (diff) | |
| parent | ec0660e41145d3d9ed479b3e697afddf8ddc8c4c (diff) | |
| download | notenoughupdates-07403ec86c53f67b94d988b4c01a0afc2fdb2810.tar.gz notenoughupdates-07403ec86c53f67b94d988b4c01a0afc2fdb2810.tar.bz2 notenoughupdates-07403ec86c53f67b94d988b4c01a0afc2fdb2810.zip | |
Merge branch 'beta'
uh yeah its pretty necessary guys
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 | 107 |
1 files changed, 75 insertions, 32 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 c271d63a..2e6b29da 100644 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java @@ -2,6 +2,7 @@ package io.github.moulberry.notenoughupdates.util; import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.apache.commons.io.IOUtils; import java.io.BufferedReader; import java.io.IOException; @@ -16,57 +17,99 @@ 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.newCachedThreadPool(); + private ExecutorService es = Executors.newFixedThreadPool(3); + + private int myApiErrors = 0; + private String[] myApiURLs = {"https://moulberry.codes/", "http://51.75.78.252/", "http://moulberry.codes/" }; public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer) { - getHypixelApiAsync(generateApiUrl(apiKey.trim(), method, args), consumer); + getHypixelApiAsync(apiKey, method, args, consumer, () -> {}); + } + + public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer, Runnable error) { + getApiAsync(generateApiUrl(apiKey.trim(), method, args), consumer, error); + } + + private String getMyApiURL() { + return myApiURLs[myApiErrors%myApiURLs.length]; } - public void getHypixelApiAsync(String urlS, Consumer<JsonObject> consumer) { + public void getApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { es.submit(() -> { - consumer.accept(getHypixelApiSync(urlS)); + try { + consumer.accept(getApiSync(urlS)); + } catch(Exception e) { + error.run(); + } }); } - public JsonObject getHypixelApiSync(String urlS) { - URLConnection connection; - try { - URL url = new URL(urlS); - connection = url.openConnection(); - connection.setConnectTimeout(3000); - } catch(IOException e) { - return null; - } + public void getMyApiAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiSync(getMyApiURL()+urlS)); + } catch(Exception e) { + myApiErrors++; + error.run(); + } + }); + } + + public void getMyApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiGZIPSync(getMyApiURL()+urlS)); + } catch(Exception e) { + myApiErrors++; + error.run(); + } + }); + } - try(BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { - StringBuilder builder = new StringBuilder(); - int codePoint; - while((codePoint = reader.read()) != -1) { - builder.append(((char)codePoint)); + public void getApiGZIPAsync(String urlS, Consumer<JsonObject> consumer, Runnable error) { + es.submit(() -> { + try { + consumer.accept(getApiGZIPSync(urlS)); + } catch(Exception e) { + error.run(); } - String response = builder.toString(); + }); + } - JsonObject json = gson.fromJson(response, JsonObject.class); - return json; - } catch(IOException e) { - return null; - } + public JsonObject getApiSync(String urlS) throws IOException { + URL url = new URL(urlS); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + + String response = IOUtils.toString(connection.getInputStream(), StandardCharsets.UTF_8); + + JsonObject json = gson.fromJson(response, JsonObject.class); + return json; + } + + public JsonObject getApiGZIPSync(String urlS) throws IOException { + URL url = new URL(urlS); + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(10000); + connection.setReadTimeout(10000); + + 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) { - String url = "https://api.hypixel.net/" + method + "?key=" + apiKey; + StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method + "?key=" + apiKey); for(Map.Entry<String, String> entry : args.entrySet()) { - url += "&" + entry.getKey() + "=" + entry.getValue(); + url.append("&").append(entry.getKey()).append("=").append(entry.getValue()); } - return url; + return url.toString(); } } |
