|
|
@ -1,13 +1,16 @@ |
|
|
|
package com.nivesh.production.bajajfd.api |
|
|
|
|
|
|
|
|
|
|
|
import com.nivesh.production.bajajfd.interfaces.ApiInterface |
|
|
|
import com.nivesh.production.bajajfd.util.Constants.Companion.BASE_URL |
|
|
|
import okhttp3.Interceptor |
|
|
|
import okhttp3.OkHttpClient |
|
|
|
import okhttp3.logging.HttpLoggingInterceptor |
|
|
|
import retrofit2.Retrofit |
|
|
|
import retrofit2.converter.gson.GsonConverterFactory |
|
|
|
|
|
|
|
class ApiClient { |
|
|
|
|
|
|
|
companion object { |
|
|
|
private val client by lazy { |
|
|
|
//lazy means we only initialize this here once |
|
|
@ -17,7 +20,7 @@ class ApiClient { |
|
|
|
//see the body of response |
|
|
|
//create client for retrofit |
|
|
|
val client = OkHttpClient.Builder() |
|
|
|
.addInterceptor(logging) |
|
|
|
.addInterceptor(ConnectVerifierInterceptor()) |
|
|
|
.retryOnConnectionFailure(true) |
|
|
|
.build() |
|
|
|
Retrofit.Builder() |
|
|
@ -29,5 +32,40 @@ class ApiClient { |
|
|
|
val getApiClient: ApiInterface by lazy { |
|
|
|
client.create(ApiInterface::class.java) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ConnectVerifierInterceptor : Interceptor { |
|
|
|
override fun intercept(chain: Interceptor.Chain): okhttp3.Response { |
|
|
|
// if (isNetworkAvailable(BaseActivity())) { |
|
|
|
// // throw IOException("No Network Available!") |
|
|
|
// |
|
|
|
// } |
|
|
|
|
|
|
|
val request = chain.request() |
|
|
|
return chain.proceed(request) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |