Skip to content

Commit 56b8c11

Browse files
committed
Fixed schedule generic endpoint
1 parent f948ca6 commit 56b8c11

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/main/kotlin/com/jeluchu/core/connection/RestClient.kt

+16
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import io.ktor.client.engine.cio.*
55
import io.ktor.client.request.*
66
import io.ktor.client.statement.*
77
import io.ktor.http.*
8+
import kotlinx.coroutines.delay
89
import kotlinx.serialization.DeserializationStrategy
910
import kotlinx.serialization.json.Json
1011

@@ -24,4 +25,19 @@ object RestClient {
2425
json.decodeFromString(deserializer, response.bodyAsText())
2526
}.getOrElse { throwable -> throw throwable }
2627
}
28+
29+
suspend fun <T> requestWithDelay(
30+
url: String,
31+
delay: Long = 1000,
32+
deserializer: DeserializationStrategy<T>
33+
): T {
34+
return runCatching {
35+
val response = client.get(url) {
36+
headers { append(HttpHeaders.Accept, ContentType.Application.Json.toString()) }
37+
}
38+
39+
delay(delay)
40+
json.decodeFromString(deserializer, response.bodyAsText())
41+
}.getOrElse { throwable -> throw throwable }
42+
}
2743
}

src/main/kotlin/com/jeluchu/features/schedule/services/ScheduleService.kt

+17-9
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,37 @@ class ScheduleService(
4848
wednesday = getSchedule(Day.WEDNESDAY).data?.map { it.toDayEntity(Day.WEDNESDAY) }.orEmpty()
4949
)
5050

51-
val documentsToInsert = parseTopDataToDocuments(response)
52-
if (documentsToInsert.isNotEmpty()) schedules.insertMany(documentsToInsert)
51+
val elements = parseTopDataToDocuments(response)
52+
if (elements.isNotEmpty()) schedules.insertMany(elements)
5353
timers.update(TimerKey.SCHEDULE)
5454

55-
call.respond(HttpStatusCode.OK, Json.encodeToString(response))
55+
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
5656
} else {
5757
val elements = schedules.find().toList()
58-
val directory = elements.map { documentToScheduleDayEntity(it) }
59-
val json = Json.encodeToString(directory)
60-
call.respond(HttpStatusCode.OK, json)
58+
call.respond(HttpStatusCode.OK, elements.documentWeekMapper())
6159
}
6260
}
6361

6462
suspend fun getScheduleByDay(call: RoutingCall) {
6563
val param = call.parameters["day"] ?: throw IllegalArgumentException(ErrorMessages.InvalidMalId.message)
66-
if (parseDay(param) == null) call.respond(HttpStatusCode.BadRequest, ErrorResponse(ErrorMessages.InvalidDay.message))
64+
if (parseDay(param) == null) call.respond(
65+
HttpStatusCode.BadRequest,
66+
ErrorResponse(ErrorMessages.InvalidDay.message)
67+
)
6768

6869
val elements = schedules.find(Filters.eq("day", param.lowercase())).toList()
6970
val directory = elements.map { documentToScheduleDayEntity(it) }
7071
val json = Json.encodeToString(directory)
7172
call.respond(HttpStatusCode.OK, json)
7273
}
7374

74-
private suspend fun getSchedule(day: Day) =
75-
RestClient.request(BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day, ScheduleEntity.serializer())
75+
private suspend fun getSchedule(day: Day) = RestClient.requestWithDelay(
76+
url = BaseUrls.JIKAN + Endpoints.SCHEDULES + "/" + day,
77+
deserializer = ScheduleEntity.serializer()
78+
)
79+
80+
private fun List<Document>.documentWeekMapper(): String {
81+
val directory = map { documentToScheduleDayEntity(it) }
82+
return Json.encodeToString(directory)
83+
}
7684
}

0 commit comments

Comments
 (0)