Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2dc501ba authored by Hasib Prince's avatar Hasib Prince
Browse files

improved handling status 429

parent 55b1a4f8
Loading
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import com.google.android.material.snackbar.Snackbar
import dagger.hilt.android.AndroidEntryPoint
import foundation.e.apps.data.fusedDownload.models.FusedDownload
import foundation.e.apps.data.login.AuthObject
import foundation.e.apps.data.login.LoginSourceGPlay
import foundation.e.apps.data.login.LoginViewModel
import foundation.e.apps.data.login.exceptions.GPlayValidationException
import foundation.e.apps.data.preference.PreferenceManagerModule
@@ -308,11 +309,11 @@ class MainActivity : AppCompatActivity() {
    private suspend fun observeTooManyRequests() {
        EventBus.events.filter { appEvent ->
            appEvent is AppEvent.TooManyRequests
        }.collectLatest { appEvent ->
        }.collectLatest {
            binding.sessionErrorLayout.visibility = View.VISIBLE
            binding.retrySessionButton.setOnClickListener {
                binding.sessionErrorLayout.visibility = View.GONE
                validatedAuthObject(appEvent = appEvent)
                loginViewModel.startLoginFlow(listOf(LoginSourceGPlay::class.java.simpleName))
            }
        }
    }
+12 −4
Original line number Diff line number Diff line
@@ -45,7 +45,7 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject

class GPlayHttpClient @Inject constructor(
    cache: Cache,
    private  val cache: Cache,
) : IHttpClient {

    private val POST = "POST"
@@ -55,6 +55,9 @@ class GPlayHttpClient @Inject constructor(
        private const val TAG = "GPlayHttpClient"
        private const val HTTP_TIMEOUT_IN_SECOND = 10L
        private const val SEARCH = "search"
        private const val SEARCH_SUGGEST = "searchSuggest"
        private const val STATUS_CODE_UNAUTHORIZED = 401
        private const val STATUS_CODE_TOO_MANY_REQUESTS = 429
    }

    private val okHttpClient = OkHttpClient().newBuilder()
@@ -200,15 +203,20 @@ class GPlayHttpClient @Inject constructor(
            Timber.d("$TAG: Url: ${response.request.url}\nStatus: $code")

            when (code) {
                401 -> MainScope().launch {
                STATUS_CODE_UNAUTHORIZED -> MainScope().launch {
                    EventBus.invokeEvent(
                        AppEvent.InvalidAuthEvent(AuthObject.GPlayAuth::class.java.simpleName)
                    )
                }

                429 -> MainScope().launch {
                STATUS_CODE_TOO_MANY_REQUESTS -> MainScope().launch {
                    cache.evictAll()
                    if (response.request.url.toString().contains(SEARCH_SUGGEST)) {
                        return@launch
                    }

                    EventBus.invokeEvent(
                        AppEvent.TooManyRequests(AuthObject.GPlayAuth::class.java.simpleName)
                        AppEvent.TooManyRequests()
                    )
                }
            }
+0 −2
Original line number Diff line number Diff line
@@ -17,12 +17,10 @@

package foundation.e.apps.data.login

import android.content.Context
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.apps.data.enums.User
import foundation.e.apps.ui.parentFragment.LoadingViewModel
import kotlinx.coroutines.launch
+1 −1
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ abstract class LoadingViewModel : ViewModel() {
            it is GPlayValidationException
        }?.run {
            if (!autoRetried && retryBlock(failedAuthList)) {
                autoRetried = false
                autoRetried = true
                exceptionsList.clear()
                return
            }
+8 −1
Original line number Diff line number Diff line
@@ -54,6 +54,10 @@ import timber.log.Timber
 */
abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) {

    companion object {
        private const val STATUS_TOO_MANY_REQUESTS = "Status: 429"
    }

    val loginViewModel: LoginViewModel by lazy {
        ViewModelProvider(requireActivity())[LoginViewModel::class.java]
    }
@@ -353,7 +357,6 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) {
     * is shown to the user.
     */
    fun showDataLoadError(exception: Exception) {

        val dialogView = DialogErrorLogBinding.inflate(requireActivity().layoutInflater)
        dialogView.apply {
            moreInfo.setOnClickListener {
@@ -406,6 +409,10 @@ abstract class TimeoutFragment(@LayoutRes layoutId: Int) : Fragment(layoutId) {
        }
        val unknownSourceException = exceptions.find { it is UnknownSourceException }

        if (gPlayException?.message?.contains(STATUS_TOO_MANY_REQUESTS) == true) {
            return
        }

        /*
         * Take caution altering the cases.
         * Cases to be defined from most restrictive to least restrictive.
Loading