@@ -2,6 +2,7 @@ package cloud.mindbox.mobile_sdk.managers
22
33import android.util.Log
44import androidx.annotation.VisibleForTesting
5+ import cloud.mindbox.mobile_sdk.fromJsonTyped
56import cloud.mindbox.mobile_sdk.inapp.data.dto.GeoTargetingDto
67import cloud.mindbox.mobile_sdk.inapp.domain.models.*
78import cloud.mindbox.mobile_sdk.logger.MindboxLoggerImpl
@@ -13,14 +14,17 @@ import cloud.mindbox.mobile_sdk.models.operation.response.SegmentationCheckRespo
1314import cloud.mindbox.mobile_sdk.network.MindboxServiceGenerator
1415import cloud.mindbox.mobile_sdk.repository.MindboxPreferences
1516import cloud.mindbox.mobile_sdk.toUrlQueryString
17+ import cloud.mindbox.mobile_sdk.utils.loggingRunCatching
1618import com.android.volley.DefaultRetryPolicy
1719import com.android.volley.DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
20+ import com.android.volley.ParseError
1821import com.android.volley.Request
1922import com.android.volley.VolleyError
2023import com.google.gson.Gson
2124import kotlinx.coroutines.*
2225import org.json.JSONException
2326import org.json.JSONObject
27+ import kotlin.coroutines.Continuation
2428import kotlin.coroutines.resume
2529import kotlin.coroutines.resumeWithException
2630import kotlin.coroutines.suspendCoroutine
@@ -325,12 +329,9 @@ internal class GatewayManager(private val mindboxServiceGenerator: MindboxServic
325329 " https://${configuration.domain} /geo" ,
326330 configuration,
327331 null ,
328- { jsonObject ->
329- continuation.resume(
330- gson.fromJson(
331- jsonObject.toString(),
332- GeoTargetingDto ::class .java
333- )
332+ { response ->
333+ continuation.resumeFromJson<GeoTargetingDto >(
334+ json = response.toString()
334335 )
335336 },
336337 { error ->
@@ -358,11 +359,8 @@ internal class GatewayManager(private val mindboxServiceGenerator: MindboxServic
358359 )
359360 )!! ,
360361 { response ->
361- continuation.resume(
362- gson.fromJson(
363- response.toString(),
364- ProductSegmentationResponseDto ::class .java
365- )
362+ continuation.resumeFromJson<ProductSegmentationResponseDto >(
363+ json = response.toString()
366364 )
367365 },
368366 { error ->
@@ -390,11 +388,8 @@ internal class GatewayManager(private val mindboxServiceGenerator: MindboxServic
390388 )
391389 )!! ,
392390 { response ->
393- continuation.resume(
394- gson.fromJson(
395- response.toString(),
396- SegmentationCheckResponse ::class .java
397- )
391+ continuation.resumeFromJson<SegmentationCheckResponse >(
392+ json = response.toString()
398393 )
399394 },
400395 { error ->
@@ -449,4 +444,16 @@ internal class GatewayManager(private val mindboxServiceGenerator: MindboxServic
449444 )
450445 }
451446 }
447+
448+ private inline fun <reified T > Continuation<T>.resumeFromJson (json : String ) {
449+ loggingRunCatching(null ) {
450+ gson.fromJsonTyped<T >(json)
451+ }?.let { dto ->
452+ resume(dto)
453+ } ? : run {
454+ resumeWithException(
455+ ParseError (JSONException (" Could not parse JSON: $json " ))
456+ )
457+ }
458+ }
452459}
0 commit comments