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("

502 Bad Gateway

")) { - if (showApiErrors) { + if (showApiErrors && apiName == "Hypixel API") { LorenzUtils.clickableChat( "[SkyHanni] Problems with detecting the Hypixel API. §eClick here to hide this message for now.", "shtogglehypixelapierrors" @@ -64,8 +68,8 @@ object APIUtil { } else { CopyErrorCommand.logError( - Error("Hypixel API error for url: '$urlString'", e), - "Failed to load data from Hypixel API" + Error("$apiName error for url: '$urlString'", e), + "Failed to load data from $apiName" ) } } @@ -76,8 +80,8 @@ object APIUtil { throw throwable } else { CopyErrorCommand.logError( - Error("Hypixel API error for url: '$urlString'", throwable), - "Failed to load data from Hypixel API" + Error("$apiName error for url: '$urlString'", throwable), + "Failed to load data from $apiName" ) } } finally { diff --git a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt index d2c4075ff..3ad8e3fe9 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt @@ -535,8 +535,8 @@ object LorenzUtils { val JsonPrimitive.asIntOrNull get() = takeIf { it.isNumber }?.asInt - fun T.transformIf(condition: Boolean, transofmration: T.() -> T) = - if (condition) transofmration(this) else this + fun T.transformIf(condition: T.() -> Boolean, transofmration: T.() -> T) = + if (condition()) transofmration(this) else this fun T.conditionalTransform(condition: Boolean, ifTrue: T.() -> Any, ifFalse: T.() -> Any) = if (condition) ifTrue(this) else ifFalse(this) -- cgit