Skip to content

Commit

Permalink
[API/Vulcan] Ignore 404 response on Addressbook.
Browse files Browse the repository at this point in the history
  • Loading branch information
kuba2k2 committed Nov 25, 2022
1 parent 6d3eb65 commit 0123f50
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -338,6 +354,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) {
query: Map<String, String> = mapOf(),
baseUrl: Boolean = false,
firebaseToken: String? = null,
allow404: Boolean = false,
crossinline onSuccess: (json: T, response: Response?) -> Unit
) {
val queryPath = query.map {
Expand All @@ -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
)
}
Expand Down Expand Up @@ -382,6 +400,7 @@ open class VulcanHebe(open val data: DataVulcan, open val lastSync: Long?) {
messageBox: String? = null,
params: Map<String, String> = mapOf(),
includeFilterType: Boolean = true,
allow404: Boolean = false,
onSuccess: (data: List<JsonObject>, response: Response?) -> Unit
) {
val url = if (includeFilterType && filterType != null)
Expand Down Expand Up @@ -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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit 0123f50

Please sign in to comment.