Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FlagApi fetch error handling #47

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions sdk/src/main/java/com/amplitude/experiment/FlagApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.experiment.evaluation.json
import com.amplitude.experiment.util.AsyncFuture
import com.amplitude.experiment.util.Logger
import kotlinx.serialization.SerializationException
import kotlinx.serialization.decodeFromString
import okhttp3.Call
import okhttp3.Callback
Expand All @@ -23,7 +24,10 @@ internal data class GetFlagsOptions(
)

internal interface FlagApi {
fun getFlags(options: GetFlagsOptions? = null, callback: ((Map<String, EvaluationFlag>) -> Unit)? = null): Future<Map<String, EvaluationFlag>>
fun getFlags(
options: GetFlagsOptions? = null,
callback: ((Map<String, EvaluationFlag>) -> Unit)? = null
): Future<Map<String, EvaluationFlag>>
}

internal class SdkFlagApi(
Expand Down Expand Up @@ -60,12 +64,18 @@ internal class SdkFlagApi(
override fun onResponse(call: Call, response: Response) {
try {
Logger.d("Received fetch flags response: $response")
val body = response.body?.string() ?: ""
val flags = json.decodeFromString<List<EvaluationFlag>>(body)
.associateBy { it.key }
future.complete(flags)
tyiuhc marked this conversation as resolved.
Show resolved Hide resolved

if (response.isSuccessful) {
bgiori marked this conversation as resolved.
Show resolved Hide resolved
val body = response.body?.string() ?: ""
} else {
Logger.e("Non-successful response: ${response.code}")
future.completeExceptionally(IOException("Non-successful response: ${response.code}"))
}
} catch (e: IOException) {
onFailure(call, e)
} catch (e: SerializationException) {
Logger.e("Error decoding JSON: ${e.message}")
future.completeExceptionally(e)
}
}

Expand Down
Loading