From 91a4c759d9b20c3026c89a0fa0da29fd4967f7e2 Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Wed, 27 Sep 2023 12:32:23 +0200 Subject: code cleanup and removed code duplication --- src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt | 18 +++++++++++------- .../java/at/hannibal2/skyhanni/utils/LorenzUtils.kt | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt index 917243b47..7f9de6685 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.utils import at.hannibal2.skyhanni.SkyHanniMod import at.hannibal2.skyhanni.test.command.CopyErrorCommand +import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonParser import com.google.gson.JsonSyntaxException @@ -39,7 +40,10 @@ object APIUtil { ) .useSystemProperties() - fun getJSONResponse(urlString: String, silentError: Boolean = false): JsonObject { + fun getJSONResponse(urlString: String, silentError: Boolean = false) = + getJSONResponseAsElement(urlString, silentError) as JsonObject + + fun getJSONResponseAsElement(urlString: String, silentError: Boolean = false, apiName: String = "Hypixel API"): JsonElement { val client = builder.build() try { client.execute(HttpGet(urlString)).use { response -> @@ -47,14 +51,14 @@ object APIUtil { if (entity != null) { val retSrc = EntityUtils.toString(entity) try { - return parser.parse(retSrc) as JsonObject + return parser.parse(retSrc) } catch (e: JsonSyntaxException) { if (e.message?.contains("Use JsonReader.setLenient(true)") == true) { println("MalformedJsonException: Use JsonReader.setLenient(true)") println(" - getJSONResponse: '$urlString'") LorenzUtils.debug("MalformedJsonException: Use JsonReader.setLenient(true)") } else if (retSrc.contains("