diff options
Diffstat (limited to 'mod/src/main/kotlin')
3 files changed, 32 insertions, 3 deletions
diff --git a/mod/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt b/mod/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt index 93bb453..42a1f42 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/modules/ExternalDataProvider.kt @@ -14,6 +14,7 @@ import java.util.concurrent.CompletableFuture class ExternalDataProvider @Inject constructor( val requestUtil: RequestUtil ) { + // TODO: Save all the data locally, so in case of a failed request older versions can be used fun createAuxillaryDataRequest(path: String): Request { return requestUtil.createRequest("https://github.com/nea89o/ledger-auxiliary-data/raw/refs/heads/master/$path") @@ -22,7 +23,9 @@ class ExternalDataProvider @Inject constructor( private val itemNameFuture: CompletableFuture<Map<String, String>> = CompletableFuture.supplyAsync { val request = createAuxillaryDataRequest("data/item_names.json") val response = request.execute(requestUtil) - val nameMap = response.json(GsonUtil.typeToken<Map<String, String>>()) + val nameMap = + response?.json(GsonUtil.typeToken<Map<String, String>>()) + ?: mapOf() return@supplyAsync nameMap } diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt new file mode 100644 index 0000000..3953e09 --- /dev/null +++ b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestTrace.kt @@ -0,0 +1,21 @@ +package moe.nea.ledger.utils.network + +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import moe.nea.ledger.utils.telemetry.ContextValue + +class RequestTrace(val request: Request) : ContextValue { + override fun serialize(): JsonElement { + return JsonObject().apply { + addProperty("url", request.url.toString()) + addProperty("method", request.method.name) + addProperty("content-type", request.headers["content-type"]) + addProperty("accept", request.headers["accept"]) + } + } + + companion object { + val KEY = "http_request" + fun createTrace(request: Request): Pair<String, RequestTrace> = KEY to RequestTrace(request) + } +}
\ No newline at end of file diff --git a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt index a49c65a..8101527 100644 --- a/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt +++ b/mod/src/main/kotlin/moe/nea/ledger/utils/network/RequestUtil.kt @@ -2,6 +2,8 @@ package moe.nea.ledger.utils.network import moe.nea.ledger.utils.ErrorUtil import moe.nea.ledger.utils.di.Inject +import moe.nea.ledger.utils.telemetry.CommonKeys +import moe.nea.ledger.utils.telemetry.ContextValue import java.net.URL import java.net.URLConnection import java.security.KeyStore @@ -38,7 +40,10 @@ class RequestUtil @Inject constructor(val errorUtil: ErrorUtil) { fun createRequest(url: String) = createRequest(URL(url)) fun createRequest(url: URL) = Request(url, Request.Method.GET, null, mapOf()) - fun executeRequest(request: Request): Response { + fun executeRequest(request: Request): Response? = errorUtil.catch( + CommonKeys.EVENT_MESSAGE to ContextValue.string("Failed to execute request"), + RequestTrace.createTrace(request) + ) { val connection = request.url.openConnection() enhanceConnection(connection) connection.setRequestProperty("accept-encoding", "gzip") @@ -56,7 +61,7 @@ class RequestUtil @Inject constructor(val errorUtil: ErrorUtil) { val text = stream.bufferedReader().readText() stream.close() // Do NOT call connection.disconnect() to allow for connection reuse - return Response(request, text, connection.headerFields) + return@catch Response(request, text, connection.headerFields) } |