Skip to content

Commit

Permalink
Added bulk traits update
Browse files Browse the repository at this point in the history
  • Loading branch information
devapro committed Feb 21, 2024
1 parent b37f1d7 commit 59e2906
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
7 changes: 6 additions & 1 deletion FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ class Flagsmith constructor(
}.also { lastUsedIdentity = identity }

fun setTrait(trait: Trait, identity: String, result: (Result<TraitWithIdentity>) -> Unit) =
retrofit.postTraits(TraitWithIdentity(trait.key, trait.traitValue, Identity(identity))).enqueueWithResult(result = result)
retrofit.postTrait(TraitWithIdentity(trait.key, trait.traitValue, Identity(identity))).enqueueWithResult(result = result)

fun setTraits(traits: List<Trait>, identity: String, result: (Result<List<TraitWithIdentity>>) -> Unit) {
val request = traits.map { TraitWithIdentity(it.key, it.traitValue, Identity(identity)) }
retrofit.postTraits(request).enqueueWithResult(result = result)
}

fun getIdentity(identity: String, result: (Result<IdentityFlagsAndTraits>) -> Unit) =
retrofit.getIdentityFlagsAndTraits(identity).enqueueWithResult(defaults = null, result = result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.PUT
import retrofit2.http.Query

interface FlagsmithRetrofitService {
Expand All @@ -25,7 +26,10 @@ interface FlagsmithRetrofitService {
fun getFlags() : Call<List<Flag>>

@POST("traits/")
fun postTraits(@Body trait: TraitWithIdentity) : Call<TraitWithIdentity>
fun postTrait(@Body trait: TraitWithIdentity) : Call<TraitWithIdentity>

@PUT("traits/bulk/")
fun postTraits(@Body traits: List<TraitWithIdentity>) : Call<List<TraitWithIdentity>>

@POST("analytics/flags/")
fun postAnalytics(@Body eventMap: Map<String, Int?>) : Call<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ suspend fun Flagsmith.getTraitSync(id: String, identity: String): Result<Trait?>
suspend fun Flagsmith.setTraitSync(trait: Trait, identity: String) : Result<TraitWithIdentity>
= suspendCoroutine { cont -> this.setTrait(trait, identity) { cont.resume(it) } }

suspend fun Flagsmith.setTraitsSync(traits: List<Trait>, identity: String) : Result<List<TraitWithIdentity>>
= suspendCoroutine { cont -> this.setTraits(traits, identity) { cont.resume(it) } }

suspend fun Flagsmith.getIdentitySync(identity: String): Result<IdentityFlagsAndTraits>
= suspendCoroutine { cont -> this.getIdentity(identity) { cont.resume(it) } }

13 changes: 13 additions & 0 deletions FlagsmithClient/src/test/java/com/flagsmith/TraitsTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ class TraitsTests {
}
}

@Test
fun testSetTraits() {
mockServer.mockResponseFor(MockEndpoint.SET_TRAITS)
runBlocking {
val result =
flagsmith.setTraitsSync(listOf(Trait(key = "set-from-client", value = "12345")), "person")
assertTrue(result.isSuccess)
assertEquals("set-from-client", result.getOrThrow().first().key)
assertEquals("12345", result.getOrThrow().first().stringValue)
assertEquals("person", result.getOrThrow().first().identity.identifier)
}
}

@Test
fun testSetTraitInteger() {
mockServer.mockResponseFor(MockEndpoint.SET_TRAIT_INTEGER)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.flagsmith.mockResponses
import com.flagsmith.entities.Trait
import com.flagsmith.mockResponses.endpoints.FlagsEndpoint
import com.flagsmith.mockResponses.endpoints.IdentityFlagsAndTraitsEndpoint
import com.flagsmith.mockResponses.endpoints.TraitsBulkEndpoint
import com.flagsmith.mockResponses.endpoints.TraitsEndpoint
import org.mockserver.integration.ClientAndServer
import org.mockserver.matchers.Times
Expand All @@ -16,6 +17,7 @@ enum class MockEndpoint(val path: String, val body: String) {
GET_IDENTITIES(IdentityFlagsAndTraitsEndpoint("").path, MockResponses.getIdentities),
GET_FLAGS(FlagsEndpoint.path, MockResponses.getFlags),
SET_TRAIT(TraitsEndpoint(Trait(key = "", traitValue = ""), "").path, MockResponses.setTrait),
SET_TRAITS(TraitsBulkEndpoint(listOf(Trait(key = "", traitValue = "")), "").path, MockResponses.setTraits),
SET_TRAIT_INTEGER(TraitsEndpoint(Trait(key = "", traitValue = ""), "").path, MockResponses.setTraitInteger),
SET_TRAIT_DOUBLE(TraitsEndpoint(Trait(key = "", traitValue = ""), "").path, MockResponses.setTraitDouble),
SET_TRAIT_BOOLEAN(TraitsEndpoint(Trait(key = "", traitValue = ""), "").path, MockResponses.setTraitBoolean),
Expand Down Expand Up @@ -166,6 +168,18 @@ object MockResponses {
}
""".trimIndent()

val setTraits = """
[
{
"trait_key": "set-from-client",
"trait_value": "12345",
"identity": {
"identifier": "person"
}
}
]
""".trimIndent()

val setTraitInteger = """
{
"trait_key": "set-from-client",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.flagsmith.mockResponses.endpoints

import com.flagsmith.entities.Identity
import com.flagsmith.entities.Trait
import com.flagsmith.entities.TraitWithIdentity
import com.google.gson.Gson

data class TraitsBulkEndpoint(private val traits: List<Trait>, private val identity: String) :
PostEndpoint<TraitWithIdentity>(
path = "/traits/bulk/",
body = Gson().toJson(
traits.map {
TraitWithIdentity(
key = it.key,
traitValue = it.traitValue,
identity = Identity(identity)
)
}
),
)

0 comments on commit 59e2906

Please sign in to comment.