Skip to content

Commit c922389

Browse files
committed
added method to add interceptor
1 parent 6453c4c commit c922389

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

zcnetwork/src/main/java/com/zoomcar/zcnetwork/core/ZcNetworkBuilder.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.zoomcar.zcnetwork.core
33
import android.app.Activity
44
import androidx.fragment.app.Fragment
55
import com.zoomcar.zcnetwork.utils.ZcRequestType
6+
import okhttp3.Interceptor
67

78
/*
89
* @created 08/01/2020 - 12:35 PM
@@ -22,6 +23,7 @@ class ZcNetworkBuilder {
2223
private lateinit var requestType: ZcRequestType
2324
private var useDefaultService: Boolean = true
2425
private var baseUrl: String? = null
26+
private var interceptors: MutableList<Interceptor> = mutableListOf()
2527

2628
fun setActivity(activity: Activity): ZcNetworkBuilder = apply { this.activity = activity }
2729
fun setFragment(fragment: Fragment): ZcNetworkBuilder = apply { this.fragment = fragment }
@@ -40,7 +42,11 @@ class ZcNetworkBuilder {
4042
fun setListener(listener: ZcNetworkListener): ZcNetworkBuilder =
4143
apply { this.listener = listener }
4244

45+
fun addInterceptor(interceptor: Interceptor): ZcNetworkBuilder =
46+
apply { this.interceptors.add(interceptor)}
47+
4348
fun setTag(tag: String): ZcNetworkBuilder = apply { this.tag = tag }
49+
fun addInterceptor(tag: String): ZcNetworkBuilder = apply { this.tag = tag }
4450
fun setUrl(url: String): ZcNetworkBuilder = apply { this.url = url }
4551
fun setRequestType(requestType: ZcRequestType) = apply { this.requestType = requestType }
4652
fun setBaseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl }
@@ -57,7 +63,8 @@ class ZcNetworkBuilder {
5763
tag,
5864
url,
5965
useDefaultService,
60-
bodyParams
66+
bodyParams,
67+
interceptors
6168
)
6269
}
6370
}

zcnetwork/src/main/java/com/zoomcar/zcnetwork/core/ZcNetworkManager.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.zoomcar.zcnetwork.utils.NetworkResponseStatus.FAILURE
1818
import com.zoomcar.zcnetwork.utils.NetworkResponseStatus.SUCCESS
1919
import com.zoomcar.zcnetwork.utils.ZcRequestType
2020
import com.zoomcar.zcnetwork.utils.getTimeDifferenceInMillis
21+
import okhttp3.Interceptor
2122
import retrofit2.Call
2223
import retrofit2.Callback
2324
import retrofit2.Response
@@ -33,6 +34,7 @@ object ZcNetworkManager {
3334
private var zcRequestManager: ZcRequestManager? = null
3435
private var isDebugLogEnabled: Boolean = false
3536
private var baseUrl: String? = null
37+
private var interceptors: MutableList<Interceptor> = mutableListOf()
3638

3739
fun builder(applicationContext: Context): ZcNetworkManager =
3840
apply { this.applicationContext = applicationContext }
@@ -45,14 +47,19 @@ object ZcNetworkManager {
4547
this.analyticsListener = analyticsListener
4648
}
4749

50+
fun addInterceptor(interceptor: Interceptor): ZcNetworkManager =
51+
apply {
52+
this.interceptors.add(interceptor)
53+
}
54+
4855
fun addBaseUrl(baseUrl: String?): ZcNetworkManager = apply { this.baseUrl = baseUrl }
4956

5057
fun build() {
5158
if (this.baseUrl == null) throw java.lang.IllegalArgumentException(
5259
CustomExceptions.NO_BASE_URL
5360
)
5461
zcRequestManager =
55-
ZcRequestManager.getInstance(applicationContext, isDebugLogEnabled, baseUrl!!)
62+
ZcRequestManager.getInstance(applicationContext, isDebugLogEnabled, baseUrl!!, interceptors)
5663
}
5764

5865
fun request(
@@ -66,7 +73,8 @@ object ZcNetworkManager {
6673
tag: String? = null,
6774
url: String,
6875
defaultService: Boolean = true,
69-
bodyParams: HashMap<String, Any>
76+
bodyParams: HashMap<String, Any>,
77+
interceptors: MutableList<Interceptor>
7078
) {
7179

7280
if (zcRequestManager == null) throw IllegalArgumentException(NOT_INITIALIZED)
@@ -153,7 +161,7 @@ object ZcNetworkManager {
153161
val call: Call<JsonElement>?
154162
if (defaultService) {
155163
val apiService =
156-
ZcRequestManager.getInstance(applicationContext, baseUrl = this.baseUrl!!)
164+
ZcRequestManager.getInstance(applicationContext, baseUrl = this.baseUrl!!, interceptor = interceptors)
157165
.getDefaultApiService()
158166
call = when (requestType) {
159167
ZcRequestType.GET -> apiService.getResource(url, requestParams)
@@ -171,7 +179,7 @@ object ZcNetworkManager {
171179
applicationContext: Context
172180
) {
173181
if (headerParams != null) {
174-
ZcRequestManager.getInstance(applicationContext, baseUrl = baseUrl!!)
182+
ZcRequestManager.getInstance(applicationContext, baseUrl = baseUrl!!, interceptor = interceptors)
175183
.setHeaderParams(headerParams)
176184
}
177185
}

zcnetwork/src/main/java/com/zoomcar/zcnetwork/core/ZcRequestManager.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.readystatesoftware.chuck.ChuckInterceptor
55
import com.zoomcar.zcnetwork.utils.TimeoutDefaults
66
import com.zoomcar.zcnetwork.utils.TimeoutHeaders
77
import okhttp3.HttpUrl
8+
import okhttp3.Interceptor
89
import okhttp3.OkHttpClient
910
import okhttp3.Request
1011
import okhttp3.logging.HttpLoggingInterceptor
@@ -20,7 +21,8 @@ import java.util.concurrent.TimeUnit
2021
class ZcRequestManager(
2122
private val applicationContext: Context,
2223
private val isDebugLogEnabled: Boolean,
23-
baseUrl: String
24+
baseUrl: String,
25+
private val interceptors: List<Interceptor>
2426
) {
2527

2628
private lateinit var defaultApiService: ZcApiService
@@ -41,11 +43,17 @@ class ZcRequestManager(
4143
fun getInstance(
4244
applicationContext: Context,
4345
debugLogEnabled: Boolean = false,
44-
baseUrl: String
46+
baseUrl: String,
47+
interceptor: List<Interceptor> = listOf()
4548
) =
4649
instance ?: synchronized(applicationContext) {
4750
instance
48-
?: ZcRequestManager(applicationContext, debugLogEnabled, baseUrl).also {
51+
?: ZcRequestManager(
52+
applicationContext,
53+
debugLogEnabled,
54+
baseUrl,
55+
interceptor
56+
).also {
4957
instance = it
5058
}
5159
}
@@ -90,6 +98,9 @@ class ZcRequestManager(
9098
val logging = HttpLoggingInterceptor()
9199
logging.level = HttpLoggingInterceptor.Level.BODY
92100
builder.addInterceptor(logging)
101+
interceptors.map {
102+
builder.addInterceptor(it)
103+
}
93104
builder.addInterceptor(ChuckInterceptor(applicationContext))
94105
}
95106

zcnetwork/src/main/java/com/zoomcar/zcnetwork/models/BaseErrorVO.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlinx.android.parcel.RawValue
1111
@JsonObject
1212
open class BaseErrorVO : Parcelable {
1313
@JsonField
14+
@SerializedName(value = "status")
1415
var status: Int = 0
1516

1617
@SerializedName(value = "error_code")
@@ -21,12 +22,15 @@ open class BaseErrorVO : Parcelable {
2122
@JsonField(name = ["error_title"])
2223
var errorTitle: String? = null
2324

25+
@SerializedName("msg")
2426
@JsonField
2527
var msg: String? = null
2628

2729
@JsonField
30+
@SerializedName("httpStatusCode")
2831
var httpStatusCode: Int = 0
2932

3033
@JsonField(name = ["metadata"])
34+
@SerializedName("metadata")
3135
var metadata: MutableMap<String, @RawValue Any?>? = null
3236
}

0 commit comments

Comments
 (0)