TP-12345 | added try catch on network layer
This commit is contained in:
@@ -18,10 +18,15 @@ import com.navi.alfred.deserializer.MetricsDataDeserializer
|
||||
import com.navi.alfred.deserializer.SessionDataDeserializer
|
||||
import com.navi.alfred.model.EventMetricRequest
|
||||
import com.navi.alfred.model.SessionRequest
|
||||
import com.navi.alfred.utils.AlfredConstants.API_FAILURE
|
||||
import com.navi.alfred.utils.AlfredConstants.API_FAILURE_CODE
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Response
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import retrofit2.Retrofit
|
||||
import retrofit2.converter.gson.GsonConverterFactory
|
||||
import java.io.IOException
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
object AlfredRetrofitProvider {
|
||||
@@ -31,6 +36,22 @@ object AlfredRetrofitProvider {
|
||||
private lateinit var apiService: AlfredRetrofitService
|
||||
private lateinit var okHttpClient: OkHttpClient
|
||||
|
||||
private val headerInterceptor: Interceptor
|
||||
get() = Interceptor { chain ->
|
||||
val request = chain.request()
|
||||
val response: Response = try {
|
||||
chain.proceed(request).newBuilder().build()
|
||||
} catch (e: IOException) {
|
||||
// A mocked response in case of n/w exception
|
||||
Response.Builder()
|
||||
.request(request)
|
||||
.code(API_FAILURE_CODE)
|
||||
.message(API_FAILURE)
|
||||
.build()
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
fun init(context: Context) {
|
||||
okHttpClient = OkHttpClient.Builder().apply {
|
||||
connectTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
|
||||
@@ -41,6 +62,7 @@ object AlfredRetrofitProvider {
|
||||
.alwaysReadResponseBody(false).build()
|
||||
)
|
||||
}
|
||||
addInterceptor(headerInterceptor)
|
||||
}.build()
|
||||
apiService = getRetrofit().create(AlfredRetrofitService::class.java)
|
||||
}
|
||||
|
||||
@@ -73,5 +73,7 @@ object AlfredConstants {
|
||||
const val PREVIOUS_SCREEN_NAME = "previous_screen_name"
|
||||
const val LOAD_TIME = "load_time_in_ms"
|
||||
const val DISABLE_CHUCKER = "DISABLE_CHUCKER"
|
||||
const val API_FAILURE = "API_FAILURE"
|
||||
const val API_FAILURE_CODE = 300
|
||||
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import java.io.*
|
||||
import java.lang.Math.subtractExact
|
||||
import java.util.*
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipFile
|
||||
import java.util.zip.ZipOutputStream
|
||||
import kotlin.math.abs
|
||||
|
||||
@@ -344,6 +345,14 @@ fun getCpuUsageForDevice(): Float {
|
||||
|
||||
}
|
||||
|
||||
fun hasFilesInZip(zipFilePath: String): Boolean {
|
||||
val zipFile = ZipFile(zipFilePath)
|
||||
val entries = zipFile.entries()
|
||||
val hasEntries = entries.hasMoreElements()
|
||||
zipFile.close()
|
||||
return hasEntries
|
||||
}
|
||||
|
||||
fun getStorageUsageForDevice(context: Context): Pair<Long, Long> {
|
||||
val path = context.filesDir.path
|
||||
val stat = StatFs(path)
|
||||
|
||||
Reference in New Issue
Block a user