diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt index a99ac7a4..85736e1f 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/VulcanHebe.kt @@ -26,6 +26,7 @@ import pl.szczodrzynski.edziennik.utils.Utils.d import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Time import java.net.HttpURLConnection +import java.net.HttpURLConnection.HTTP_NOT_FOUND import java.net.URLEncoder import java.time.Instant import java.time.LocalDateTime @@ -183,6 +184,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { payload: JsonElement? = null, baseUrl: Boolean = false, firebaseToken: String? = null, + allow404: Boolean = false, crossinline onSuccess: (json: T, response: Response?) -> Unit ) { val url = "${if (baseUrl) data.apiUrl else data.fullApiUrl}$endpoint" @@ -226,6 +228,20 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { val callback = object : JsonCallbackHandler() { override fun onSuccess(json: JsonObject?, response: Response?) { + if (allow404 && response?.code() == HTTP_NOT_FOUND) { + try { + onSuccess(null as T, response) + } catch (e: Exception) { + data.error( + ApiError(tag, EXCEPTION_VULCAN_HEBE_REQUEST) + .withResponse(response) + .withThrowable(e) + .withApiResponse(json) + ) + } + return + } + if (json == null) { data.error( ApiError(TAG, ERROR_RESPONSE_EMPTY) @@ -338,6 +354,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { query: Map = mapOf(), baseUrl: Boolean = false, firebaseToken: String? = null, + allow404: Boolean = false, crossinline onSuccess: (json: T, response: Response?) -> Unit ) { val queryPath = query.map { @@ -348,6 +365,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { if (query.isNotEmpty()) "$endpoint?$queryPath" else endpoint, baseUrl = baseUrl, firebaseToken = firebaseToken, + allow404 = allow404, onSuccess = onSuccess ) } @@ -382,6 +400,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { messageBox: String? = null, params: Map = mapOf(), includeFilterType: Boolean = true, + allow404: Boolean = false, onSuccess: (data: List, response: Response?) -> Unit ) { val url = if (includeFilterType && filterType != null) @@ -427,7 +446,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) { ) .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) - apiGet(tag, url, query) { json: JsonArray, response -> + apiGet(tag, url, query, allow404) { json: JsonArray, response -> onSuccess(json.map { it.asJsonObject }, response) } } diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAddressbook.kt b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAddressbook.kt index d7bec22b..5893486b 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAddressbook.kt +++ b/app/src/main/java/pl/szczodrzynski/edziennik/data/api/edziennik/vulcan/data/hebe/VulcanHebeAddressbook.kt @@ -19,6 +19,7 @@ import pl.szczodrzynski.edziennik.data.db.entity.Teacher.Companion.TYPE_PARENTS_ import pl.szczodrzynski.edziennik.data.db.entity.Teacher.Companion.TYPE_STUDENT import pl.szczodrzynski.edziennik.data.db.entity.Teacher.Companion.TYPE_TEACHER import pl.szczodrzynski.edziennik.ext.* +import java.net.HttpURLConnection.HTTP_NOT_FOUND class VulcanHebeAddressbook( override val data: DataVulcan, @@ -41,8 +42,15 @@ class VulcanHebeAddressbook( VULCAN_HEBE_ENDPOINT_ADDRESSBOOK, HebeFilterType.BY_PERSON, lastSync = lastSync, - includeFilterType = false - ) { list, _ -> + includeFilterType = false, + allow404 = true, + ) { list, response -> + if (response?.code() == HTTP_NOT_FOUND) { + data.setSyncNext(ENDPOINT_VULCAN_HEBE_ADDRESSBOOK, 2 * DAY) + onSuccess(ENDPOINT_VULCAN_HEBE_ADDRESSBOOK) + return@apiGetList + } + list.forEach { person -> val id = person.getString("Id") ?: return@forEach