Fixed schedule generic endpoint

v5 v5.1.2
Jéluchu 7 months ago
parent f948ca6b13
commit 56b8c1108a

@ -5,6 +5,7 @@ import io.ktor.client.engine.cio.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.client.statement.* import io.ktor.client.statement.*
import io.ktor.http.* import io.ktor.http.*
import kotlinx.coroutines.delay
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
@ -24,4 +25,19 @@ object RestClient {
json.decodeFromString(deserializer, response.bodyAsText()) json.decodeFromString(deserializer, response.bodyAsText())
}.getOrElse { throwable -> throw throwable } }.getOrElse { throwable -> throw throwable }
} }
suspend fun <T> requestWithDelay(
url: String,
delay: Long = 1000,
deserializer: DeserializationStrategy<T>
): 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 }
}
} }

@ -48,22 +48,23 @@ class ScheduleService(
wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty() wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty()
) )
val documentsToInsert = parseTopDataToDocuments(response) val elements = parseTopDataToDocuments(response)
if (documentsToInsert.isNotEmpty()) schedules.insertMany(documentsToInsert) if (elements.isNotEmpty()) schedules.insertMany(elements)
timers.update(TimerKey.SCHEDULE) timers.update(TimerKey.SCHEDULE)
call.respond(HttpStatusCode.OK, Json.encodeToString(response)) call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
} else { } else {
val elements = schedules.find().toList() val elements = schedules.find().toList()
val directory = elements.map { documentToScheduleDayEntity(it) } call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
val json = Json.encodeToString(directory)
call.respond(HttpStatusCode.OK, json)
} }
} }
suspend fun getScheduleByDay(call: RoutingCall) { suspend fun getScheduleByDay(call: RoutingCall) {
val param = call.parameters["day"] ?: throw IllegalArgumentException(ErrorMessages.InvalidMalId.message) 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 elements = schedules.find(Filters.eq("day", param.lowercase())).toList()
val directory = elements.map { documentToScheduleDayEntity(it) } val directory = elements.map { documentToScheduleDayEntity(it) }
@ -71,6 +72,13 @@ class ScheduleService(
call.respond(HttpStatusCode.OK, json) call.respond(HttpStatusCode.OK, json)
} }
private suspend fun getSchedule(day: Day) = private suspend fun getSchedule(day: Day) = RestClient.requestWithDelay(
RestClient.request(BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day, ScheduleEntity.serializer()) url = BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day,
deserializer = ScheduleEntity.serializer()
)
private fun List<Document>.documentWeekMapper(): String {
val directory = map { documentToScheduleDayEntity(it) }
return Json.encodeToString(directory)
}
} }
Loading…
Cancel
Save