From 0a1171b8f94349f938079d43fbc6d759294ff684 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Sat, 30 Jul 2022 16:27:46 +0200 Subject: [PATCH] Stop cashing cache response in student repo --- .../scrapper/repository/StudentRepository.kt | 33 +++++-------------- 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentRepository.kt b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentRepository.kt index 1a2bbd4f..0af4dd73 100644 --- a/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentRepository.kt +++ b/sdk-scrapper/src/main/kotlin/io/github/wulkanowy/sdk/scrapper/repository/StudentRepository.kt @@ -65,44 +65,29 @@ import java.time.LocalDate class StudentRepository(private val api: StudentService) { - private lateinit var cache: CacheResponse - - private lateinit var times: List - private val moshi by lazy { Moshi.Builder().add(CustomDateAdapter()) } private fun LocalDate.toISOFormat(): String = toFormat("yyyy-MM-dd'T00:00:00'") private suspend fun getCache(): CacheResponse { - if (::cache.isInitialized) return cache - - val it = api.getStart("Start") + val startPage = api.getStart("Start") val res = api.getUserCache( - token = getScriptParam("antiForgeryToken", it), - appGuid = getScriptParam("appGuid", it), - appVersion = getScriptParam("version", it) + token = getScriptParam("antiForgeryToken", startPage), + appGuid = getScriptParam("appGuid", startPage), + appVersion = getScriptParam("version", startPage) ).handleErrors() val data = requireNotNull(res.data) { "Required value was null. $res" } - cache = data return data } - private suspend fun getTimes(): List { - if (::times.isInitialized) return times - - val res = getCache() - times = res.times - return res.times - } - suspend fun getAttendance(startDate: LocalDate, endDate: LocalDate?): List { return api.getAttendance(AttendanceRequest(startDate.toDate())) .handleErrors() - .data?.mapAttendanceList(startDate, endDate, getTimes()).orEmpty() + .data?.mapAttendanceList(startDate, endDate, getCache().times).orEmpty() } suspend fun getAttendanceSummary(subjectId: Int?): List { @@ -112,11 +97,11 @@ class StudentRepository(private val api: StudentService) { } suspend fun excuseForAbsence(absents: List, content: String?): Boolean { - val it = api.getStart("Start") + val startPage = api.getStart("Start") return api.excuseForAbsence( - token = getScriptParam("antiForgeryToken", it), - appGuid = getScriptParam("appGuid", it), - appVersion = getScriptParam("version", it), + token = getScriptParam("antiForgeryToken", startPage), + appGuid = getScriptParam("appGuid", startPage), + appVersion = getScriptParam("version", startPage), attendanceExcuseRequest = AttendanceExcuseRequest( AttendanceExcuseRequest.Excuse( absents = absents.map { absence ->