aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
diff options
context:
space:
mode:
authorunknown <james.jenour@protonmail.com>2020-05-31 01:59:47 +1000
committerunknown <james.jenour@protonmail.com>2020-05-31 01:59:47 +1000
commitde97f55968d183cc7d76aad87e3b27d382bfdbc9 (patch)
treeeab5e7769069f31b79016e3702855ebb9f614a8e /src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
downloadnotenoughupdates-de97f55968d183cc7d76aad87e3b27d382bfdbc9.tar.gz
notenoughupdates-de97f55968d183cc7d76aad87e3b27d382bfdbc9.tar.bz2
notenoughupdates-de97f55968d183cc7d76aad87e3b27d382bfdbc9.zip
1.5
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.java68
1 files changed, 68 insertions, 0 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
new file mode 100644
index 00000000..a39da281
--- /dev/null
+++ b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java
@@ -0,0 +1,68 @@
+package io.github.moulberry.notenoughupdates.util;
+
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.function.Consumer;
+
+public class HypixelApi {
+
+ private Gson gson = new Gson();
+ private ExecutorService es = Executors.newSingleThreadExecutor();
+
+ //https://api.hypixel.net/skyblock/profiles?key=06b20aa2-ff04-4851-9687-669c8111526f&uuid=d0e05de7-6067-454d-beae-c6d19d886191
+ public void getHypixelApiAsync(String apiKey, String method, HashMap<String, String> args, Consumer<JsonObject> consumer) {
+ getHypixelApiAsync(generateApiUrl(apiKey, method, args), consumer);
+ }
+
+ public void getHypixelApiAsync(String urlS, Consumer<JsonObject> consumer) {
+ es.submit(() -> {
+ consumer.accept(getHypixelApiSync(urlS));
+ });
+ }
+
+ public JsonObject getHypixelApiSync(String urlS) {
+ URLConnection connection;
+ try {
+ URL url = new URL(urlS);
+ connection = url.openConnection();
+ connection.setConnectTimeout(3000);
+ } catch(IOException e) {
+ return null;
+ }
+
+ try(BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
+ StringBuilder builder = new StringBuilder();
+ int codePoint;
+ while((codePoint = reader.read()) != -1) {
+ builder.append(((char)codePoint));
+ }
+ String response = builder.toString();
+
+ JsonObject json = gson.fromJson(response, JsonObject.class);
+ return json;
+ } catch(IOException e) {
+ return null;
+ }
+ }
+
+ public String generateApiUrl(String apiKey, String method, HashMap<String, String> args) {
+ String url = "https://api.hypixel.net/" + method + "?key=" + apiKey;
+ for(Map.Entry<String, String> entry : args.entrySet()) {
+ url += "&" + entry.getKey() + "=" + entry.getValue();
+ }
+ return url;
+ }
+
+}