From 811bcc3bc38154f1e623a4151bdc3f748c40d404 Mon Sep 17 00:00:00 2001 From: Jack Walker Date: Fri, 31 May 2024 11:46:12 +0100 Subject: [PATCH 1/2] Implemented getIdentity flags for getting flags by an identity and settings traits at the same time --- .../src/main/java/com/flagsmith/Flagsmith.kt | 6 ++++++ .../src/test/java/com/flagsmith/FeatureFlagTests.kt | 13 +++++++++++++ .../test/java/com/flagsmith/SynchronousFlagsmith.kt | 3 +++ 3 files changed, 22 insertions(+) diff --git a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt index 47cc4b3..ea32f63 100644 --- a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt +++ b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt @@ -175,6 +175,12 @@ class Flagsmith constructor( retrofit.getIdentityFlagsAndTraits(identity).enqueueWithResult(defaults = null, result = result) .also { lastUsedIdentity = identity } + fun getIdentityFlags(identity: String, traits: List, result: (Result>) -> Unit) { + retrofit.postTraits(IdentityAndTraits(identity, traits)).enqueueWithResult(result = { + result(it.map { response -> response.flags }) + }).also { lastUsedIdentity = identity } + } + fun clearCache() { try { cache?.evictAll() diff --git a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt index f950ac4..c27b028 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt @@ -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.getIdentityFlagsSync(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) + } + } } \ No newline at end of file diff --git a/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt b/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt index 06c024d..a69bd4f 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt @@ -31,3 +31,6 @@ suspend fun Flagsmith.setTraitsSync(traits: List, identity: String) : Res suspend fun Flagsmith.getIdentitySync(identity: String): Result = suspendCoroutine { cont -> this.getIdentity(identity) { cont.resume(it) } } +suspend fun Flagsmith.getIdentityFlagsSync(identity: String, traits: List): Result> + = suspendCoroutine { cont -> this.getIdentityFlags(identity, traits) { cont.resume(it) } } + From 0955e050e026fc5abd15ddaf02838c198b60bd23 Mon Sep 17 00:00:00 2001 From: Jack Walker Date: Thu, 20 Jun 2024 11:46:47 +0100 Subject: [PATCH 2/2] Removed getIdentityFlags, added traits argument to getFeatureFlags --- .../src/main/java/com/flagsmith/Flagsmith.kt | 30 +++++++++++-------- .../java/com/flagsmith/FeatureFlagTests.kt | 2 +- .../com/flagsmith/SynchronousFlagsmith.kt | 7 ++--- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt index ea32f63..1fcc4ed 100644 --- a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt +++ b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt @@ -106,19 +106,29 @@ class Flagsmith constructor( const val DEFAULT_ANALYTICS_FLUSH_PERIOD_SECONDS = 10 } - fun getFeatureFlags(identity: String? = null, result: (Result>) -> Unit) { + fun getFeatureFlags(identity: String? = null, traits: List? = null, result: (Result>) -> 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 }) + } } } 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) + } } } } @@ -175,12 +185,6 @@ class Flagsmith constructor( retrofit.getIdentityFlagsAndTraits(identity).enqueueWithResult(defaults = null, result = result) .also { lastUsedIdentity = identity } - fun getIdentityFlags(identity: String, traits: List, result: (Result>) -> Unit) { - retrofit.postTraits(IdentityAndTraits(identity, traits)).enqueueWithResult(result = { - result(it.map { response -> response.flags }) - }).also { lastUsedIdentity = identity } - } - fun clearCache() { try { cache?.evictAll() diff --git a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt index c27b028..a6daeda 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt @@ -149,7 +149,7 @@ class FeatureFlagTests { fun testGetFeatureFlagsWithIdentityAndTraits() { mockServer.mockResponseFor(MockEndpoint.GET_IDENTITIES) runBlocking { - val result = flagsmith.getIdentityFlagsSync(identity = "person", traits = listOf()) + val result = flagsmith.getFeatureFlagsSync(identity = "person", traits = listOf()) assertTrue(result.isSuccess) val found = result.getOrThrow().find { flag -> flag.feature.name == "with-value" } diff --git a/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt b/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt index a69bd4f..41b9f12 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/SynchronousFlagsmith.kt @@ -10,8 +10,8 @@ import kotlin.coroutines.suspendCoroutine suspend fun Flagsmith.hasFeatureFlagSync(forFeatureId: String, identity: String? = null): Result = suspendCoroutine { cont -> this.hasFeatureFlag(forFeatureId, identity = identity) { cont.resume(it) } } -suspend fun Flagsmith.getFeatureFlagsSync(identity: String? = null) : Result> - = suspendCoroutine { cont -> this.getFeatureFlags(identity = identity) { cont.resume(it) } } +suspend fun Flagsmith.getFeatureFlagsSync(identity: String? = null, traits: List? = null) : Result> + = suspendCoroutine { cont -> this.getFeatureFlags(identity = identity, traits = traits) { cont.resume(it) } } suspend fun Flagsmith.getValueForFeatureSync(forFeatureId: String, identity: String? = null): Result = suspendCoroutine { cont -> this.getValueForFeature(forFeatureId, identity = identity) { cont.resume(it) } } @@ -31,6 +31,3 @@ suspend fun Flagsmith.setTraitsSync(traits: List, identity: String) : Res suspend fun Flagsmith.getIdentitySync(identity: String): Result = suspendCoroutine { cont -> this.getIdentity(identity) { cont.resume(it) } } -suspend fun Flagsmith.getIdentityFlagsSync(identity: String, traits: List): Result> - = suspendCoroutine { cont -> this.getIdentityFlags(identity, traits) { cont.resume(it) } } -