summaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/utils
diff options
context:
space:
mode:
authorhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-09-27 12:32:23 +0200
committerhannibal2 <24389977+hannibal00212@users.noreply.github.com>2023-09-27 12:32:23 +0200
commit91a4c759d9b20c3026c89a0fa0da29fd4967f7e2 (patch)
treea81f8867850a15bec675d80fdf8a41de667dbdac /src/main/java/at/hannibal2/skyhanni/utils
parent23b12864f115c4cd37a48b01afdb093732dd5a12 (diff)
downloadskyhanni-91a4c759d9b20c3026c89a0fa0da29fd4967f7e2.tar.gz
skyhanni-91a4c759d9b20c3026c89a0fa0da29fd4967f7e2.tar.bz2
skyhanni-91a4c759d9b20c3026c89a0fa0da29fd4967f7e2.zip
code cleanup and removed code duplication
Diffstat (limited to 'src/main/java/at/hannibal2/skyhanni/utils')
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/APIUtil.kt18
-rw-r--r--src/main/java/at/hannibal2/skyhanni/utils/LorenzUtils.kt4
2 files changed, 13 insertions, 9 deletions
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("<center><h1>502 Bad Gateway</h1></center>")) {
- 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> T.transformIf(condition: Boolean, transofmration: T.() -> T) =
- if (condition) transofmration(this) else this
+ fun <T> T.transformIf(condition: T.() -> Boolean, transofmration: T.() -> T) =
+ if (condition()) transofmration(this) else this
fun <T> T.conditionalTransform(condition: Boolean, ifTrue: T.() -> Any, ifFalse: T.() -> Any) =
if (condition) ifTrue(this) else ifFalse(this)