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

Added getIdentityFlags method #51

Merged
merged 3 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 17 additions & 7 deletions FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,29 @@ class Flagsmith constructor(
const val DEFAULT_ANALYTICS_FLUSH_PERIOD_SECONDS = 10
}

fun getFeatureFlags(identity: String? = null, result: (Result<List<Flag>>) -> Unit) {
fun getFeatureFlags(identity: String? = null, traits: List<Trait>? = null, result: (Result<List<Flag>>) -> Unit) {
// Save the last used identity as we'll refresh with this if we get update events
lastUsedIdentity = identity

if (identity != null) {
retrofit.getIdentityFlagsAndTraits(identity).enqueueWithResult { res ->
flagUpdateFlow.tryEmit(res.getOrNull()?.flags ?: emptyList())
result(res.map { it.flags })
if (traits != null) {
retrofit.postTraits(IdentityAndTraits(identity, traits)).enqueueWithResult(result = {
result(it.map { response -> response.flags })
}).also { lastUsedIdentity = identity }
} else {
retrofit.getIdentityFlagsAndTraits(identity).enqueueWithResult { res ->
flagUpdateFlow.tryEmit(res.getOrNull()?.flags ?: emptyList())
result(res.map { it.flags })
}
Comment on lines +114 to +122
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to my comment here, I think we can remove this conditional logic and always use the POST endpoint, but if it's non-trivial to achieve, we can ignore for the purpose of this PR.

}
} else {
retrofit.getFlags().enqueueWithResult(defaults = defaultFlags) { res ->
flagUpdateFlow.tryEmit(res.getOrNull() ?: emptyList())
result(res)
if (traits != null) {
throw IllegalArgumentException("Cannot set traits without an identity");
} else {
retrofit.getFlags().enqueueWithResult(defaults = defaultFlags) { res ->
flagUpdateFlow.tryEmit(res.getOrNull() ?: emptyList())
result(res)
}
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,17 @@ class FeatureFlagTests {
}
assertEquals("Flagsmith requires a context to use the analytics feature", exception.message)
}

@Test
fun testGetFeatureFlagsWithIdentityAndTraits() {
mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES)
runBlocking {
val result = flagsmith.getFeatureFlagsSync(identity = "person", traits = listOf())
assertTrue(result.isSuccess)

val found = result.getOrThrow().find { flag -> flag.feature.name == "with-value" }
assertNotNull(found)
assertEquals(756.0, found?.featureStateValue)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import kotlin.coroutines.suspendCoroutine
suspend fun Flagsmith.hasFeatureFlagSync(forFeatureId: String, identity: String? = null): Result<Boolean>
= suspendCoroutine { cont -> this.hasFeatureFlag(forFeatureId, identity = identity) { cont.resume(it) } }

suspend fun Flagsmith.getFeatureFlagsSync(identity: String? = null) : Result<List<Flag>>
= suspendCoroutine { cont -> this.getFeatureFlags(identity = identity) { cont.resume(it) } }
suspend fun Flagsmith.getFeatureFlagsSync(identity: String? = null, traits: List<Trait>? = null) : Result<List<Flag>>
= suspendCoroutine { cont -> this.getFeatureFlags(identity = identity, traits = traits) { cont.resume(it) } }

suspend fun Flagsmith.getValueForFeatureSync(forFeatureId: String, identity: String? = null): Result<Any?>
= suspendCoroutine { cont -> this.getValueForFeature(forFeatureId, identity = identity) { cont.resume(it) } }
Expand Down
Loading