Skip to content

Commit

Permalink
Return JSON string format initialize response (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinlili-statsig authored Aug 22, 2023
1 parent 09dc7c7 commit c0215ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/main/java/com/statsig/androidsdk/ExternalInitializeResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.statsig.androidsdk

/**
* A helper class for interfacing with Initialize Response currently being used in the Statsig console
*/
class ExternalInitializeResponse(
private val values: String?,
private val evaluationDetails: EvaluationDetails,
) {
internal companion object {
fun getUninitialized(): ExternalInitializeResponse {
return ExternalInitializeResponse(null, EvaluationDetails(EvaluationReason.Uninitialized))
}
}
fun getInitializeResponseJSON(): String? {
return values
}

fun getEvaluationDetails(): EvaluationDetails {
return evaluationDetails.copy()
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/statsig/androidsdk/Statsig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ object Statsig {
})
}

/**
* @return Initialize response currently being used in JSON and evaluation details
* @throws IllegalStateException if the SDK has not been initialized
*/
fun getInitializeResponseJson(): ExternalInitializeResponse {
var result: ExternalInitializeResponse? = null
enforceInitialized("getInitializeResponseJson")
errorBoundary.capture({
result = client.getInitializeResponseJson()
}, tag = "getInitializeResponseJson")
return result ?: ExternalInitializeResponse.getUninitialized()
}

@JvmSynthetic
suspend fun shutdownSuspend() {
enforceInitialized("shutdownSuspend")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/statsig/androidsdk/StatsigClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,10 @@ internal class StatsigClient() {
}
}

fun getInitializeResponseJson(): ExternalInitializeResponse? {
return store.getCurrentCacheValuesAndEvaluationReason()
}

suspend fun shutdownSuspend() {
enforceInitialized("shutdown")
pollingJob?.cancel()
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/statsig/androidsdk/Store.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ internal class Store(private val statsigScope: CoroutineScope, private val share
cacheById[currentUserCacheKey] = currentCache
cacheString = gson.toJson(cacheById)
}

StatsigUtil.saveStringToSharedPrefs(sharedPrefs, CACHE_BY_USER_KEY, cacheString)
}

Expand Down Expand Up @@ -342,6 +341,10 @@ internal class Store(private val statsigScope: CoroutineScope, private val share
)
}

fun getCurrentCacheValuesAndEvaluationReason(): ExternalInitializeResponse {
return ExternalInitializeResponse(gson.toJson(currentCache.values), getEvaluationDetails(true))
}

private fun hydrateDynamicConfig(name: String, details: EvaluationDetails, config: APIDynamicConfig?): DynamicConfig {
return DynamicConfig(
name,
Expand Down

0 comments on commit c0215ec

Please sign in to comment.