From 56b8c1108a88826727ac273494c84628eb0dd4a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9luchu?= Date: Thu, 9 Jan 2025 01:05:38 +0100 Subject: [PATCH] Fixed schedule generic endpoint --- .../com/jeluchu/core/connection/RestClient.kt | 16 ++++++++++++ .../schedule/services/ScheduleService.kt | 26 ++++++++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/com/jeluchu/core/connection/RestClient.kt b/src/main/kotlin/com/jeluchu/core/connection/RestClient.kt index 577275b..458fc18 100644 --- a/src/main/kotlin/com/jeluchu/core/connection/RestClient.kt +++ b/src/main/kotlin/com/jeluchu/core/connection/RestClient.kt @@ -5,6 +5,7 @@ import io.ktor.client.engine.cio.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* +import kotlinx.coroutines.delay import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.json.Json @@ -24,4 +25,19 @@ object RestClient { json.decodeFromString(deserializer, response.bodyAsText()) }.getOrElse { throwable -> throw throwable } } + + suspend fun requestWithDelay( + url: String, + delay: Long = 1000, + deserializer: DeserializationStrategy + ): T { + return runCatching { + val response = client.get(url) { + headers { append(HttpHeaders.Accept, ContentType.Application.Json.toString()) } + } + + delay(delay) + json.decodeFromString(deserializer, response.bodyAsText()) + }.getOrElse { throwable -> throw throwable } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/jeluchu/features/schedule/services/ScheduleService.kt b/src/main/kotlin/com/jeluchu/features/schedule/services/ScheduleService.kt index 90f9f2d..cdb9f12 100644 --- a/src/main/kotlin/com/jeluchu/features/schedule/services/ScheduleService.kt +++ b/src/main/kotlin/com/jeluchu/features/schedule/services/ScheduleService.kt @@ -48,22 +48,23 @@ class ScheduleService( wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty() ) - val documentsToInsert = parseTopDataToDocuments(response) - if (documentsToInsert.isNotEmpty()) schedules.insertMany(documentsToInsert) + val elements = parseTopDataToDocuments(response) + if (elements.isNotEmpty()) schedules.insertMany(elements) timers.update(TimerKey.SCHEDULE) - call.respond(HttpStatusCode.OK, Json.encodeToString(response)) + call.respond(HttpStatusCode.OK, elements.documentWeekMapper()) } else { val elements = schedules.find().toList() - val directory = elements.map { documentToScheduleDayEntity(it) } - val json = Json.encodeToString(directory) - call.respond(HttpStatusCode.OK, json) + call.respond(HttpStatusCode.OK, elements.documentWeekMapper()) } } suspend fun getScheduleByDay(call: RoutingCall) { val param = call.parameters["day"] ?: throw IllegalArgumentException(ErrorMessages.InvalidMalId.message) - if (parseDay(param) == null) call.respond(HttpStatusCode.BadRequest, ErrorResponse(ErrorMessages.InvalidDay.message)) + if (parseDay(param) == null) call.respond( + HttpStatusCode.BadRequest, + ErrorResponse(ErrorMessages.InvalidDay.message) + ) val elements = schedules.find(Filters.eq("day", param.lowercase())).toList() val directory = elements.map { documentToScheduleDayEntity(it) } @@ -71,6 +72,13 @@ class ScheduleService( call.respond(HttpStatusCode.OK, json) } - private suspend fun getSchedule(day: Day) = - RestClient.request(BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day, ScheduleEntity.serializer()) + private suspend fun getSchedule(day: Day) = RestClient.requestWithDelay( + url = BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day, + deserializer = ScheduleEntity.serializer() + ) + + private fun List.documentWeekMapper(): String { + val directory = map { documentToScheduleDayEntity(it) } + return Json.encodeToString(directory) + } } \ No newline at end of file