1414
1515package com.optimizely.ab.android.sdk.cmab
1616
17+ import android.content.Context
1718import androidx.annotation.VisibleForTesting
1819import com.optimizely.ab.android.shared.Client
19- import org.slf4j.Logger
20+ import com.optimizely.ab.android.shared.OptlyStorage
21+ import com.optimizely.ab.cmab.client.CmabClient
22+ import com.optimizely.ab.cmab.client.CmabClientHelper
23+ import com.optimizely.ab.cmab.client.CmabFetchException
24+ import com.optimizely.ab.cmab.client.CmabInvalidResponseException
25+ import org.slf4j.LoggerFactory
2026import java.net.HttpURLConnection
2127import java.net.URL
2228
2329@VisibleForTesting(otherwise = VisibleForTesting .PROTECTED )
24- open class DefaultCmabClient (private val client : Client , private val logger : Logger ) {
30+ open class DefaultCmabClient (private val client : Client ) : CmabClient {
31+
32+ private val logger = LoggerFactory .getLogger(DefaultCmabClient ::class .java)
33+
34+ constructor (context: Context ) : this (Client (OptlyStorage (context), LoggerFactory .getLogger(OptlyStorage ::class .java)))
2535
2636 @VisibleForTesting(otherwise = VisibleForTesting .PROTECTED )
27- fun fetchDecision (
37+ open fun fetchDecision (
2838 ruleId : String? ,
2939 userId : String? ,
3040 attributes : Map <String ?, Any ?>? ,
3141 cmabUuid : String?
32- ): String {
33- val request: Client .Request <String > = Client .Request {
42+ ): String? {
43+ val request: Client .Request <String ? > = Client .Request {
3444 var urlConnection: HttpURLConnection ? = null
3545 try {
3646 val apiEndpoint = String .format(CmabClientHelper .CMAB_PREDICTION_ENDPOINT , ruleId)
@@ -40,6 +50,7 @@ open class DefaultCmabClient(private val client: Client, private val logger: Log
4050 val url = URL (apiEndpoint)
4151 urlConnection = client.openConnection(url)
4252 if (urlConnection == null ) {
53+ logger.error(" Error opening connection to $apiEndpoint " )
4354 return @Request null
4455 }
4556
@@ -88,15 +99,7 @@ open class DefaultCmabClient(private val client: Client, private val logger: Log
8899 }
89100 }
90101 }
91- val response = client.execute(request, REQUEST_BACKOFF_TIMEOUT , REQUEST_RETRIES_POWER )
92- val parser: ResponseJsonParser = ResponseJsonParserFactory .getParser()
93- try {
94- return parser.parseQualifiedSegments(response)
95- } catch (e: java.lang.Exception ) {
96- logger.error(" Audience segments fetch failed (Error Parsing Response)" )
97- logger.debug(e.message)
98- }
99- return null
102+ return client.execute(request, REQUEST_BACKOFF_TIMEOUT , REQUEST_RETRIES_POWER )
100103 }
101104
102105 companion object {
0 commit comments