From 2dd4a2b36211c380c0bf4e231859dfafd3c04a5b Mon Sep 17 00:00:00 2001 From: Roman / Linnea Gräf Date: Thu, 29 Sep 2022 17:25:37 +0200 Subject: Add custom keystore and refactor HypixelApi.java (#318) * Add custom keystore and refactor HypixelApi.java * Fix inputstream leak (+ less console spam) * Fix HOTM crash * Use api selected variable to find best profile * Number formatting * Make old profiles show again Co-authored-by: nopo --- .../notenoughupdates/util/HypixelApi.java | 174 --------------------- 1 file changed, 174 deletions(-) delete mode 100644 src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java (limited to 'src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java') diff --git a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java b/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java deleted file mode 100644 index 3e38fd2e..00000000 --- a/src/main/java/io/github/moulberry/notenoughupdates/util/HypixelApi.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * Copyright (C) 2022 NotEnoughUpdates contributors - * - * This file is part of NotEnoughUpdates. - * - * NotEnoughUpdates is free software: you can redistribute it - * and/or modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either - * version 3 of the License, or (at your option) any later version. - * - * NotEnoughUpdates is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with NotEnoughUpdates. If not, see . - */ - -package io.github.moulberry.notenoughupdates.util; - -import com.google.gson.Gson; -import com.google.gson.JsonObject; -import io.github.moulberry.notenoughupdates.NotEnoughUpdates; -import org.apache.commons.io.IOUtils; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.ConnectException; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLEncoder; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.function.Consumer; -import java.util.zip.GZIPInputStream; - -public class HypixelApi { - private final Gson gson = new Gson(); - private final ExecutorService es = Executors.newFixedThreadPool(3); - - public CompletableFuture getHypixelApiAsync(String apiKey, String method, HashMap args) { - return getApiAsync(generateApiUrl(apiKey, method, args)); - } - - public void getHypixelApiAsync( - String apiKey, - String method, - HashMap args, - Consumer consumer - ) { - getHypixelApiAsync(apiKey, method, args, consumer, () -> { - }); - } - - public void getHypixelApiAsync( - String apiKey, - String method, - HashMap args, - Consumer consumer, - Runnable error - ) { - getApiAsync(generateApiUrl(apiKey, method, args), consumer, error); - } - - private String getMyApiURL() { - return String.format("https://%s/", NotEnoughUpdates.INSTANCE.config.apiData.moulberryCodesApi); - } - - public CompletableFuture getApiAsync(String urlS) { - CompletableFuture result = new CompletableFuture<>(); - es.submit(() -> { - try { - result.complete(getApiSync(urlS)); - } catch (Exception e) { - result.completeExceptionally(e); - } - }); - return result; - } - - public void getApiAsync(String urlS, Consumer consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiSync(urlS)); - } catch (Exception e) { - error.run(); - } - }); - } - - public void getMyApiAsync(String urlS, Consumer consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiSync(getMyApiURL() + urlS)); - } catch (Exception e) { - if (NotEnoughUpdates.INSTANCE.config.hidden.dev) { - e.printStackTrace(); - } - error.run(); - } - }); - } - - public void getMyApiGZIPAsync(String urlS, Consumer consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiGZIPSync(getMyApiURL() + urlS)); - } catch (Exception e) { - error.run(); - } - }); - } - - public void getApiGZIPAsync(String urlS, Consumer consumer, Runnable error) { - es.submit(() -> { - try { - consumer.accept(getApiGZIPSync(urlS)); - } catch (Exception e) { - error.run(); - } - }); - } - - 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); - if (json == null) throw new ConnectException("Invalid JSON"); - 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 args) { - if (apiKey != null) - args.put("key", apiKey.trim()); - StringBuilder url = new StringBuilder("https://api.hypixel.net/" + method); - boolean first = true; - for (Map.Entry entry : args.entrySet()) { - if (first) { - url.append("?"); - first = false; - } else { - url.append("&"); - } - try { - url.append(URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8.name())).append("=") - .append(URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.name())); - } catch (UnsupportedEncodingException e) { - } - } - return url.toString(); - } -} -- cgit