From 8cb645020b1a608c3c134ecfbb2c84052d0a57ad Mon Sep 17 00:00:00 2001 From: Anthony Cicchetti Date: Mon, 17 Feb 2025 16:25:34 -0500 Subject: [PATCH] Updates, removed Fuel from dependency tree, some async.... --- VERSION | 2 +- build.gradle.kts | 2 + buildSrc/build.gradle.kts | 6 -- buildSrc/src/main/kotlin/Libs.kt | 8 +- .../com/anthonycicchetti/slackbot/Main.kt | 33 ++++--- .../slackbot/osrs/ItemMapper.kt | 85 ++++++++++--------- versions.properties | 53 +++++++++++- 7 files changed, 126 insertions(+), 63 deletions(-) diff --git a/VERSION b/VERSION index 0d91a54..1d0ba9e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.0 +0.4.0 diff --git a/build.gradle.kts b/build.gradle.kts index aab7ce8..2835257 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -44,6 +44,8 @@ dependencies { implementation(Libs.swagger_core) implementation(Libs.swagger_ui) implementation(Libs.slack_api_model_kotlin_extension) + implementation(Libs.ktor_client_cio) + implementation(Libs.ktor_client_core) testImplementation(Libs.junit_jupiter) } diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index c0f79e7..741bd01 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -20,9 +20,3 @@ repositories { plugins { `kotlin-dsl` } - -//java { -// toolchain { -// languageVersion = JavaLanguageVersion.of(21) -// } -//} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/Libs.kt b/buildSrc/src/main/kotlin/Libs.kt index 6df4d07..f698581 100644 --- a/buildSrc/src/main/kotlin/Libs.kt +++ b/buildSrc/src/main/kotlin/Libs.kt @@ -20,10 +20,6 @@ public object Libs { public const val com_github_johnrengelman_shadow_gradle_plugin: String = "com.github.johnrengelman.shadow:com.github.johnrengelman.shadow.gradle.plugin:_" - public const val fuel: String = "com.github.kittinunf.fuel:fuel:_" - - public const val fuel_jackson: String = "com.github.kittinunf.fuel:fuel-jackson:_" - public const val slack_api_model_kotlin_extension: String = "com.slack.api:slack-api-model-kotlin-extension:_" @@ -34,6 +30,10 @@ public object Libs { public const val javalin_openapi: String = "io.javalin:javalin-openapi:_" + public const val ktor_client_cio: String = "io.ktor:ktor-client-cio:_" + + public const val ktor_client_core: String = "io.ktor:ktor-client-core:_" + public const val swagger_core: String = "io.swagger.core.v3:swagger-core:_" public const val org_jetbrains_kotlin_jvm_gradle_plugin: String = diff --git a/src/main/kotlin/com/anthonycicchetti/slackbot/Main.kt b/src/main/kotlin/com/anthonycicchetti/slackbot/Main.kt index a79e0fd..e25de66 100644 --- a/src/main/kotlin/com/anthonycicchetti/slackbot/Main.kt +++ b/src/main/kotlin/com/anthonycicchetti/slackbot/Main.kt @@ -1,6 +1,8 @@ package com.anthonycicchetti.slackbot -import com.anthonycicchetti.slackbot.osrs.ItemMapper +import com.anthonycicchetti.slackbot.osrs.ApiItem +import com.anthonycicchetti.slackbot.osrs.getOsrsItemPrice +import com.anthonycicchetti.slackbot.osrs.getOsrsItemByName import com.anthonycicchetti.slackbot.utility.* import io.javalin.http.Context import io.javalin.Javalin @@ -12,6 +14,7 @@ import io.javalin.plugin.openapi.ui.SwaggerOptions import io.swagger.v3.oas.models.OpenAPI import io.swagger.v3.oas.models.info.Info import io.swagger.v3.oas.models.servers.Server +import kotlinx.coroutines.runBlocking import org.slf4j.LoggerFactory import java.util.* import kotlin.math.roundToInt @@ -67,13 +70,15 @@ fun main() { it.description = "Accepts an OSRS item name and searches for its prices on the wiki" it.summary = "POST for OSRS Price" }.body().json("200")) { ctx -> - logger.info("OSRS Item: Received request for ${ctx.body()}") - val item = ItemMapper.getItemByName(ctx.body()) - if (item == null) { - ctx.json(notFoundItem) - } else { - val itemPrices = ItemMapper.getItemPrice(item) - ctx.json((BlockResponse(200, item.name, itemPrices.high, itemPrices.low))) + runBlocking { + logger.info("OSRS Item: Received request for ${ctx.body()}") + val item: ApiItem? = getOsrsItemByName(ctx.body()) + if (item == null) { + ctx.json(notFoundItem) + } else { + val itemPrices = getOsrsItemPrice(item) + ctx.json((BlockResponse(200, item.name, itemPrices.high, itemPrices.low))) + } } }) } @@ -124,24 +129,28 @@ private fun sendResponse(ctx: Context, respObj: RespObj) { Commands.Spongebob -> { put("text", respObj.text.toSpongemock()); put("response_type", "in_channel") } + Commands.Uppercase -> { put("text", respObj.text.uppercase(Locale.getDefault())); put("response_type", "in_channel") } + Commands.Claptext -> { put("text", respObj.text.toClapText()); put("response_type", "in_channel") } - Commands.OsrsItem -> { - val item = ItemMapper.getItemByName(respObj.text) + + Commands.OsrsItem -> runBlocking { + val item = getOsrsItemByName(respObj.text) val itemPricesBlock: BlockResponse = if (item == null) { notFoundItem } else { - val itemPrices = ItemMapper.getItemPrice(item) + val itemPrices = getOsrsItemPrice(item) BlockResponse(200, item.name, itemPrices.high, itemPrices.low) } val logger = LoggerFactory.getLogger("sendResponse") - logger.info("${itemPricesBlock.response}") + logger.info(itemPricesBlock.response) put("blocks", itemPricesBlock.response); put("response_type", "in_channel") } + Commands.Error -> { put("text", respObj.text); put("response_type", "ephemeral") } diff --git a/src/main/kotlin/com/anthonycicchetti/slackbot/osrs/ItemMapper.kt b/src/main/kotlin/com/anthonycicchetti/slackbot/osrs/ItemMapper.kt index 4912caa..842790d 100644 --- a/src/main/kotlin/com/anthonycicchetti/slackbot/osrs/ItemMapper.kt +++ b/src/main/kotlin/com/anthonycicchetti/slackbot/osrs/ItemMapper.kt @@ -3,57 +3,64 @@ package com.anthonycicchetti.slackbot.osrs import com.fasterxml.jackson.annotation.JsonIgnoreProperties import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import com.github.kittinunf.fuel.Fuel -import com.github.kittinunf.result.Result +import io.ktor.client.* +import io.ktor.client.request.* +import io.ktor.client.statement.* +import io.ktor.client.call.* +import io.ktor.client.engine.cio.* +import io.ktor.client.plugins.* +import kotlinx.coroutines.* +import org.slf4j.Logger import org.slf4j.LoggerFactory -import java.nio.charset.Charset -class ItemMapper { - companion object { - private val logger = LoggerFactory.getLogger("ItemMapper") - private val mapper = ObjectMapper() - fun getItemByName(itemName: String): ApiItem? = - itemList.firstOrNull { item -> item.name.lowercase() == itemName.lowercase() } +val logger: Logger by lazy { LoggerFactory.getLogger("ItemMapper") } +val mapper: ObjectMapper by lazy { ObjectMapper() } - fun getItemPrice(item: ApiItem): Item { - val response = Fuel.get("https://prices.runescape.wiki/api/v1/osrs/latest", listOf("id" to item.id)) - .appendHeader("User-Agent", "private_slackbot - @AnthonyCicc") - .responseString(Charset.defaultCharset()).third - return when (response) { - is Result.Success -> { - val r: Resp = mapper.readValue(response.get()) - r.data.getOrDefault(item.id.toString(), Item()) - } - is Result.Failure -> { - logger.warn("Couldn't find Item with ID ${item.id}") - Item() - } - } +val client = HttpClient(CIO) { + install(UserAgent) { + agent = "private_slackbot - @AnthonyCicc" + } +} + +suspend fun getItemList(): List { + val response = client.get("https://prices.runescape.wiki/api/v1/osrs/mapping") + return when (response.status.value) { + in 200..299 -> { + logger.info("Got item mappings") + mapper.readValue(response.bodyAsText()) } - private val itemList: List = run { - val response = Fuel.get("https://prices.runescape.wiki/api/v1/osrs/mapping") - .appendHeader("User-Agent", "private_slackbot - @AnthonyCicc") - .responseString(Charset.defaultCharset()).third - when (response) { - is Result.Success -> { - logger.info("Got item mappings") - mapper.readValue(response.get()) - } - is Result.Failure -> { - logger.info("Could not get item mappings") - emptyList() - } - } + else -> { + logger.info("Could not get item mappings") + emptyList() } } } -data class Resp( +suspend fun getOsrsItemByName(itemName: String): ApiItem? = + getItemList().firstOrNull { item: ApiItem -> item.name.lowercase() == itemName.lowercase() } + +suspend fun getOsrsItemPrice(item: ApiItem): Item { + val response = client + .get("https://prices.runescape.wiki/api/v1/osrs/latest") + return when (response.status.value) { + in 200..299 -> { + val r: Resp = mapper.readValue(response.body()) + r.data.getOrDefault(item.id.toString(), Item()) + } + + else -> { + logger.warn("Couldn't find Item with ID ${item.id}") + Item() + } + } +} + +private data class Resp( val data: Map = emptyMap(), ) -data class Item ( +data class Item( val low: Int = 0, val lowTime: Long = 0L, val high: Int = 0, diff --git a/versions.properties b/versions.properties index f2390a0..fb99cc4 100644 --- a/versions.properties +++ b/versions.properties @@ -14,14 +14,63 @@ plugin.de.fayard.buildSrcLibs=0.60.5 plugin.org.gradle.kotlin.kotlin-dsl=5.1.2 ## # available=5.2.0 - version.com.fasterxml.jackson.core..jackson-databind=2.18.2 version.com.fasterxml.jackson.module..jackson-module-kotlin=2.18.2 +## unused version.com.github.kittinunf.fuel..fuel=2.3.1 version.com.slack.api..slack-api-model-kotlin-extension=1.24.0 +## # available=1.25.0 +## # available=1.25.1 +## # available=1.26.0 +## # available=1.26.1 +## # available=1.27.0 +## # available=1.27.1 +## # available=1.27.2 +## # available=1.27.3 +## # available=1.28.0 +## # available=1.28.1 +## # available=1.29.0 +## # available=1.29.1 +## # available=1.29.2 +## # available=1.30.0 +## # available=1.31.0 +## # available=1.32.0 +## # available=1.32.1 +## # available=1.32.2 +## # available=1.33.0 +## # available=1.34.0 +## # available=1.34.1 +## # available=1.35.0 +## # available=1.35.1 +## # available=1.36.0 +## # available=1.36.1 +## # available=1.37.0 +## # available=1.38.0 +## # available=1.38.1 +## # available=1.38.2 +## # available=1.38.3 +## # available=1.39.0 +## # available=1.39.1 +## # available=1.39.2 +## # available=1.39.3 +## # available=1.40.0 +## # available=1.40.1 +## # available=1.40.2 +## # available=1.40.3 +## # available=1.41.0 +## # available=1.42.0 +## # available=1.42.1 +## # available=1.43.0 +## # available=1.43.1 +## # available=1.44.0 +## # available=1.44.1 +## # available=1.44.2 +## # available=1.45.0 +## # available=1.45.1 +## # available=1.45.2 version.io.javalin..javalin=4.6.4 ## # available=4.6.5 @@ -137,6 +186,8 @@ version.kotlin=2.1.10 ## # available=2.1.20-Beta1 ## # available=2.1.20-Beta2 + version.ktor=3.1.0 + version.org.slf4j..slf4j-simple=1.7.36 ## # available=1.8.0-alpha0 ## # available=1.8.0-alpha1