Rename ServerConnectionListener.kt to ApiConnectionListener.kt,

ServerConnectionHandler.kt to ApiConnectionHandler.kt and
ServerConnectionException.kt to ApiConnectionException.kt
This commit is contained in:
dkadrikj 2021-11-22 11:10:22 +01:00 committed by Dino Kadrikj
parent b69d40f9fb
commit 519765b9bd
11 changed files with 27 additions and 24 deletions

View File

@ -46,7 +46,7 @@ import me.proton.core.network.domain.NetworkManager
import me.proton.core.network.domain.NetworkPrefs import me.proton.core.network.domain.NetworkPrefs
import me.proton.core.network.domain.client.ClientIdProvider import me.proton.core.network.domain.client.ClientIdProvider
import me.proton.core.network.domain.client.ExtraHeaderProvider import me.proton.core.network.domain.client.ExtraHeaderProvider
import me.proton.core.network.domain.serverconnection.ServerConnectionListener import me.proton.core.network.domain.serverconnection.ApiConnectionListener
import me.proton.core.network.domain.humanverification.HumanVerificationListener import me.proton.core.network.domain.humanverification.HumanVerificationListener
import me.proton.core.network.domain.humanverification.HumanVerificationProvider import me.proton.core.network.domain.humanverification.HumanVerificationProvider
import me.proton.core.network.domain.server.ServerTimeListener import me.proton.core.network.domain.server.ServerTimeListener
@ -109,7 +109,7 @@ class NetworkModule {
humanVerificationProvider: HumanVerificationProvider, humanVerificationProvider: HumanVerificationProvider,
humanVerificationListener: HumanVerificationListener, humanVerificationListener: HumanVerificationListener,
extraHeaderProvider: ExtraHeaderProvider, extraHeaderProvider: ExtraHeaderProvider,
serverConnectionListener: ServerConnectionListener apiConnectionListener: ApiConnectionListener
): ApiManagerFactory = ApiManagerFactory( ): ApiManagerFactory = ApiManagerFactory(
Constants.BASE_URL, Constants.BASE_URL,
apiClient, apiClient,
@ -131,7 +131,7 @@ class NetworkModule {
) )
}, },
extraHeaderProvider, extraHeaderProvider,
serverConnectionListener apiConnectionListener
) )
@Provides @Provides
@ -141,7 +141,7 @@ class NetworkModule {
@Provides @Provides
@Singleton @Singleton
fun provideGuestHoleFallbackListener(): ServerConnectionListener = object: ServerConnectionListener { fun provideGuestHoleFallbackListener(): ApiConnectionListener = object: ApiConnectionListener {
override suspend fun <T> onPotentiallyBlocked( override suspend fun <T> onPotentiallyBlocked(
path: String?, path: String?,
query: String?, query: String?,

View File

@ -37,8 +37,8 @@ import me.proton.core.network.domain.NetworkManager
import me.proton.core.network.domain.NetworkPrefs import me.proton.core.network.domain.NetworkPrefs
import me.proton.core.network.domain.client.ClientIdProvider import me.proton.core.network.domain.client.ClientIdProvider
import me.proton.core.network.domain.client.ExtraHeaderProvider import me.proton.core.network.domain.client.ExtraHeaderProvider
import me.proton.core.network.domain.serverconnection.ServerConnectionListener import me.proton.core.network.domain.serverconnection.ApiConnectionListener
import me.proton.core.network.domain.handlers.ServerConnectionHandler import me.proton.core.network.domain.handlers.ApiConnectionHandler
import me.proton.core.network.domain.handlers.HumanVerificationInvalidHandler import me.proton.core.network.domain.handlers.HumanVerificationInvalidHandler
import me.proton.core.network.domain.handlers.HumanVerificationNeededHandler import me.proton.core.network.domain.handlers.HumanVerificationNeededHandler
import me.proton.core.network.domain.handlers.ProtonForceUpdateHandler import me.proton.core.network.domain.handlers.ProtonForceUpdateHandler
@ -85,7 +85,7 @@ class ApiManagerFactory(
private val alternativeApiPins: List<String> = Constants.ALTERNATIVE_API_SPKI_PINS, private val alternativeApiPins: List<String> = Constants.ALTERNATIVE_API_SPKI_PINS,
private val cache: () -> Cache? = { null }, private val cache: () -> Cache? = { null },
private val extraHeaderProvider: ExtraHeaderProvider? = null, private val extraHeaderProvider: ExtraHeaderProvider? = null,
private val serverConnectionListener: ServerConnectionListener? private val apiConnectionListener: ApiConnectionListener?
) { ) {
@OptIn(ObsoleteCoroutinesApi::class) @OptIn(ObsoleteCoroutinesApi::class)
@ -137,7 +137,7 @@ class ApiManagerFactory(
): List<ApiErrorHandler<Api>> { ): List<ApiErrorHandler<Api>> {
val refreshTokenHandler = RefreshTokenHandler<Api>(sessionId, sessionProvider, sessionListener, monoClockMs) val refreshTokenHandler = RefreshTokenHandler<Api>(sessionId, sessionProvider, sessionListener, monoClockMs)
val forceUpdateHandler = ProtonForceUpdateHandler<Api>(apiClient) val forceUpdateHandler = ProtonForceUpdateHandler<Api>(apiClient)
val serverConnectionHandler = ServerConnectionHandler<Api>(serverConnectionListener) val serverConnectionHandler = ApiConnectionHandler<Api>(apiConnectionListener)
val humanVerificationNeededHandler = val humanVerificationNeededHandler =
HumanVerificationNeededHandler<Api>(sessionId, clientIdProvider, humanVerificationListener, monoClockMs) HumanVerificationNeededHandler<Api>(sessionId, clientIdProvider, humanVerificationListener, monoClockMs)
val humanVerificationInvalidHandler = val humanVerificationInvalidHandler =

View File

@ -20,7 +20,7 @@ package me.proton.core.network.data
import kotlinx.serialization.SerializationException import kotlinx.serialization.SerializationException
import me.proton.core.network.domain.ApiResult import me.proton.core.network.domain.ApiResult
import me.proton.core.network.domain.NetworkManager import me.proton.core.network.domain.NetworkManager
import me.proton.core.network.domain.exception.ServerConnectionException import me.proton.core.network.domain.exception.ApiConnectionException
import me.proton.core.util.kotlin.CoreLogger import me.proton.core.util.kotlin.CoreLogger
import okhttp3.Response import okhttp3.Response
import retrofit2.HttpException import retrofit2.HttpException
@ -46,7 +46,7 @@ internal suspend fun <Api, T> safeApiCall(
ApiResult.Error.Parse(e) ApiResult.Error.Parse(e)
} catch (e: CertificateException) { } catch (e: CertificateException) {
ApiResult.Error.Certificate(e) ApiResult.Error.Certificate(e)
} catch (e: ServerConnectionException) { } catch (e: ApiConnectionException) {
e.toApiResult(networkManager) e.toApiResult(networkManager)
} }
if (result is ApiResult.Error) { if (result is ApiResult.Error) {
@ -55,7 +55,7 @@ internal suspend fun <Api, T> safeApiCall(
return result return result
} }
private fun ServerConnectionException.toApiResult(networkManager: NetworkManager): ApiResult.Error.Connection { private fun ApiConnectionException.toApiResult(networkManager: NetworkManager): ApiResult.Error.Connection {
// handle the exceptions that might indicate that the API is potentially blocked // handle the exceptions that might indicate that the API is potentially blocked
return when (originalException) { return when (originalException) {
is SSLHandshakeException -> ApiResult.Error.Certificate(this) is SSLHandshakeException -> ApiResult.Error.Certificate(this)

View File

@ -20,7 +20,7 @@ package me.proton.core.network.data.interceptor
import me.proton.core.network.data.ProtonErrorException import me.proton.core.network.data.ProtonErrorException
import me.proton.core.network.data.protonApi.ProtonErrorData import me.proton.core.network.data.protonApi.ProtonErrorData
import me.proton.core.network.domain.exception.ServerConnectionException import me.proton.core.network.domain.exception.ApiConnectionException
import me.proton.core.util.kotlin.deserializeOrNull import me.proton.core.util.kotlin.deserializeOrNull
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
@ -35,7 +35,7 @@ class ServerErrorInterceptor : Interceptor {
} catch (e: IOException) { } catch (e: IOException) {
// every IO exception is possible potential blocking of the API // every IO exception is possible potential blocking of the API
with(request.url) { with(request.url) {
throw ServerConnectionException(encodedPath, query, e) throw ApiConnectionException(encodedPath, query, e)
} }
} }
if (!response.isSuccessful) { if (!response.isSuccessful) {

View File

@ -142,6 +142,7 @@ internal class ApiManagerTests {
mockk(), mockk(),
scope, scope,
cache = { null }, cache = { null },
apiConnectionListener = null
) )
coEvery { dohService.getAlternativeBaseUrls(any()) } returns listOf(proxy1url) coEvery { dohService.getAlternativeBaseUrls(any()) } returns listOf(proxy1url)

View File

@ -166,7 +166,8 @@ internal class HumanVerificationTests {
humanVerificationListener, humanVerificationListener,
cookieStore, cookieStore,
scope, scope,
cache = { null } cache = { null },
apiConnectionListener = null
) )
every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable

View File

@ -133,7 +133,8 @@ internal class ProtonApiBackendTests {
humanVerificationListener, humanVerificationListener,
cookieStore, cookieStore,
scope, scope,
cache = { null } cache = { null },
apiConnectionListener = null
) )
every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable every { networkManager.isConnectedToNetwork() } returns isNetworkAvailable

View File

@ -20,7 +20,7 @@ package me.proton.core.network.domain
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.withTimeoutOrNull import kotlinx.coroutines.withTimeoutOrNull
import me.proton.core.network.domain.exception.ServerConnectionException import me.proton.core.network.domain.exception.ApiConnectionException
import me.proton.core.network.domain.humanverification.HumanVerificationAvailableMethods import me.proton.core.network.domain.humanverification.HumanVerificationAvailableMethods
/** /**
@ -100,8 +100,8 @@ sealed class ApiResult<out T> {
) : Error(cause) { ) : Error(cause) {
override val isPotentialBlocking get() = potentialBlock override val isPotentialBlocking get() = potentialBlock
val path = if (cause is ServerConnectionException) cause.path else null val path = if (cause is ApiConnectionException) cause.path else null
val query = if (cause is ServerConnectionException) cause.query else null val query = if (cause is ApiConnectionException) cause.query else null
} }
/** /**

View File

@ -20,7 +20,7 @@ package me.proton.core.network.domain.exception
import java.io.IOException import java.io.IOException
class ServerConnectionException( class ApiConnectionException(
val path: String, val path: String,
val query: String?, val query: String?,
val originalException: IOException val originalException: IOException

View File

@ -24,10 +24,10 @@ import me.proton.core.network.domain.ApiBackend
import me.proton.core.network.domain.ApiErrorHandler import me.proton.core.network.domain.ApiErrorHandler
import me.proton.core.network.domain.ApiManager import me.proton.core.network.domain.ApiManager
import me.proton.core.network.domain.ApiResult import me.proton.core.network.domain.ApiResult
import me.proton.core.network.domain.serverconnection.ServerConnectionListener import me.proton.core.network.domain.serverconnection.ApiConnectionListener
class ServerConnectionHandler<Api>( class ApiConnectionHandler<Api>(
private val serverConnectionListener: ServerConnectionListener? private val apiConnectionListener: ApiConnectionListener?
) : ApiErrorHandler<Api> { ) : ApiErrorHandler<Api> {
override suspend fun <T> invoke( override suspend fun <T> invoke(
backend: ApiBackend<Api>, backend: ApiBackend<Api>,
@ -37,7 +37,7 @@ class ServerConnectionHandler<Api>(
if (!error.isPotentialBlocking) return error if (!error.isPotentialBlocking) return error
if (error !is ApiResult.Error.Connection) return error if (error !is ApiResult.Error.Connection) return error
return serverConnectionListener?.let { listener -> return apiConnectionListener?.let { listener ->
globalMutex.withLock { globalMutex.withLock {
listener.onPotentiallyBlocked(error.path, error.query) { backend(call) } listener.onPotentiallyBlocked(error.path, error.query) { backend(call) }
} }

View File

@ -20,7 +20,7 @@ package me.proton.core.network.domain.serverconnection
import me.proton.core.network.domain.ApiResult import me.proton.core.network.domain.ApiResult
interface ServerConnectionListener { interface ApiConnectionListener {
suspend fun <T> onPotentiallyBlocked( suspend fun <T> onPotentiallyBlocked(
path: String?, path: String?,