[API] Add ignoring signature errors.

This commit is contained in:
Kuba Szczodrzyński 2021-03-28 23:50:50 +02:00
parent 3093850a4a
commit b5d5685b4c
5 changed files with 53 additions and 11 deletions

View File

@ -36,11 +36,13 @@ import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode import org.greenrobot.eventbus.ThreadMode
import pl.droidsonroids.gif.GifDrawable import pl.droidsonroids.gif.GifDrawable
import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE
import pl.szczodrzynski.edziennik.data.api.ERROR_VULCAN_API_DEPRECATED import pl.szczodrzynski.edziennik.data.api.ERROR_VULCAN_API_DEPRECATED
import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask import pl.szczodrzynski.edziennik.data.api.edziennik.EdziennikTask
import pl.szczodrzynski.edziennik.data.api.events.* import pl.szczodrzynski.edziennik.data.api.events.*
import pl.szczodrzynski.edziennik.data.api.models.ApiError import pl.szczodrzynski.edziennik.data.api.models.ApiError
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.api.szkolny.response.RegisterAvailabilityStatus
import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update import pl.szczodrzynski.edziennik.data.api.szkolny.response.Update
import pl.szczodrzynski.edziennik.data.db.entity.LoginStore import pl.szczodrzynski.edziennik.data.db.entity.LoginStore
import pl.szczodrzynski.edziennik.data.db.entity.Metadata.* import pl.szczodrzynski.edziennik.data.db.entity.Metadata.*
@ -628,22 +630,40 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
app.profile.registerName?.let { registerName -> app.profile.registerName?.let { registerName ->
var status = app.config.sync.registerAvailability[registerName] var status = app.config.sync.registerAvailability[registerName]
if (status == null || status.nextCheckAt < currentTimeUnix()) { if (status == null || status.nextCheckAt < currentTimeUnix()) {
withContext(Dispatchers.IO) { val api = SzkolnyApi(app)
val api = SzkolnyApi(app) val result = withContext(Dispatchers.IO) {
api.runCatching(this@MainActivity) { return@withContext api.runCatching({
val availability = getRegisterAvailability() val availability = getRegisterAvailability()
app.config.sync.registerAvailability = availability app.config.sync.registerAvailability = availability
status = availability[registerName] availability[registerName]
}, onError = {
if (it.toErrorCode() == ERROR_API_INVALID_SIGNATURE) {
return@withContext false
}
return@withContext it
})
}
when (result) {
false -> {
Toast.makeText(this@MainActivity, R.string.error_no_api_access, Toast.LENGTH_SHORT).show()
return@let
}
is Throwable -> {
errorSnackbar.addError(result.toApiError(SzkolnyApi.TAG)).show()
return
}
is RegisterAvailabilityStatus -> {
status = result
} }
} }
} }
if (status?.available != true if (status?.available != true || status.minVersionCode > BuildConfig.VERSION_CODE) {
|| status?.minVersionCode ?: BuildConfig.VERSION_CODE > BuildConfig.VERSION_CODE) {
swipeRefreshLayout.isRefreshing = false swipeRefreshLayout.isRefreshing = false
loadTarget(DRAWER_ITEM_HOME) loadTarget(DRAWER_ITEM_HOME)
if (status != null) if (status != null)
RegisterUnavailableDialog(this, status!!) RegisterUnavailableDialog(this, status)
return return
} }
} }

View File

@ -90,7 +90,7 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa
return return
} }
profile.registerName?.let { registerName -> profile.registerName?.also { registerName ->
var status = app.config.sync.registerAvailability[registerName] var status = app.config.sync.registerAvailability[registerName]
if (status == null || status.nextCheckAt < currentTimeUnix()) { if (status == null || status.nextCheckAt < currentTimeUnix()) {
val api = SzkolnyApi(app) val api = SzkolnyApi(app)
@ -99,7 +99,11 @@ open class EdziennikTask(override val profileId: Int, val request: Any) : IApiTa
app.config.sync.registerAvailability = availability app.config.sync.registerAvailability = availability
status = availability[registerName] status = availability[registerName]
}, onError = { }, onError = {
taskCallback.onError(it.toApiError(TAG)) val apiError = it.toApiError(TAG)
if (apiError.errorCode == ERROR_API_INVALID_SIGNATURE) {
return@also
}
taskCallback.onError(apiError)
return return
}) })
} }

View File

@ -5,6 +5,7 @@
package pl.szczodrzynski.edziennik.data.api.szkolny package pl.szczodrzynski.edziennik.data.api.szkolny
import android.os.Build import android.os.Build
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -14,6 +15,7 @@ import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.EventBus
import pl.szczodrzynski.edziennik.* import pl.szczodrzynski.edziennik.*
import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE
import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.DateAdapter import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.DateAdapter
import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.TimeAdapter import pl.szczodrzynski.edziennik.data.api.szkolny.adapter.TimeAdapter
import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureInterceptor import pl.szczodrzynski.edziennik.data.api.szkolny.interceptor.SignatureInterceptor
@ -89,11 +91,17 @@ class SzkolnyApi(val app: App) : CoroutineScope {
} }
catch (e: Exception) { catch (e: Exception) {
withContext(coroutineContext) { withContext(coroutineContext) {
val apiError = e.toApiError(TAG)
if (apiError.errorCode == ERROR_API_INVALID_SIGNATURE) {
Toast.makeText(activity, R.string.error_no_api_access, Toast.LENGTH_SHORT).show()
return@withContext null
}
ErrorDetailsDialog( ErrorDetailsDialog(
activity, activity,
listOf(e.toApiError(TAG)), listOf(apiError),
R.string.error_occured R.string.error_occured
) )
null
} }
null null
} }

View File

@ -5,10 +5,13 @@
package pl.szczodrzynski.edziennik.data.api.task package pl.szczodrzynski.edziennik.data.api.task
import pl.szczodrzynski.edziennik.App import pl.szczodrzynski.edziennik.App
import pl.szczodrzynski.edziennik.data.api.ERROR_API_INVALID_SIGNATURE
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApi
import pl.szczodrzynski.edziennik.data.api.szkolny.SzkolnyApiException
import pl.szczodrzynski.edziennik.data.db.entity.Metadata import pl.szczodrzynski.edziennik.data.db.entity.Metadata
import pl.szczodrzynski.edziennik.data.db.entity.Notification import pl.szczodrzynski.edziennik.data.db.entity.Notification
import pl.szczodrzynski.edziennik.data.db.entity.Profile import pl.szczodrzynski.edziennik.data.db.entity.Profile
import pl.szczodrzynski.edziennik.toErrorCode
import pl.szczodrzynski.edziennik.utils.models.Date import pl.szczodrzynski.edziennik.utils.models.Date
class AppSync(val app: App, val notifications: MutableList<Notification>, val profiles: List<Profile>, val api: SzkolnyApi) { class AppSync(val app: App, val notifications: MutableList<Notification>, val profiles: List<Profile>, val api: SzkolnyApi) {
@ -27,7 +30,13 @@ class AppSync(val app: App, val notifications: MutableList<Notification>, val pr
*/ */
fun run(lastSyncTime: Long, markAsSeen: Boolean = false): Int { fun run(lastSyncTime: Long, markAsSeen: Boolean = false): Int {
val blacklistedIds = app.db.eventDao().blacklistedIds val blacklistedIds = app.db.eventDao().blacklistedIds
val events = api.getEvents(profiles, notifications, blacklistedIds, lastSyncTime) val events = try {
api.getEvents(profiles, notifications, blacklistedIds, lastSyncTime)
} catch (e: SzkolnyApiException) {
if (e.toErrorCode() == ERROR_API_INVALID_SIGNATURE)
return 0
throw e
}
app.config.sync.lastAppSync = System.currentTimeMillis() app.config.sync.lastAppSync = System.currentTimeMillis()

View File

@ -1424,4 +1424,5 @@
<string name="settings_about_version_details_text">Szczegóły wersji</string> <string name="settings_about_version_details_text">Szczegóły wersji</string>
<string name="settings_about_version_details_subtext">Informacje o kompilacji</string> <string name="settings_about_version_details_subtext">Informacje o kompilacji</string>
<string name="build_dialog_open_repo">Sprawdź kod</string> <string name="build_dialog_open_repo">Sprawdź kod</string>
<string name="error_no_api_access">Brak dostępu do API</string>
</resources> </resources>