Skip to content

Commit

Permalink
expose sdk id to be set with provider or from the outside
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Jun 14, 2024
1 parent 6ac48f9 commit 5f9d105
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
13 changes: 10 additions & 3 deletions Confidence/src/main/java/com/spotify/confidence/Confidence.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class Confidence internal constructor(
}
}

fun setSdk(sdk: SdkMetadata) {
(flagResolver as RemoteFlagResolver).setSdk(sdk)
(eventSenderEngine as EventSenderEngineImpl).setSdk(sdk)
(flagApplierClient as FlagApplierClientImpl).setSdk(sdk)
}

private val flagApplier = FlagApplierWithRetries(
client = flagApplierClient,
dispatcher = dispatcher,
Expand Down Expand Up @@ -235,6 +241,7 @@ object ConfidenceFactory {
fun create(
context: Context,
clientSecret: String,
sdk: SdkMetadata = SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION),
initialContext: Map<String, ConfidenceValue> = mapOf(),
region: ConfidenceRegion = ConfidenceRegion.GLOBAL,
dispatcher: CoroutineDispatcher = Dispatchers.IO
Expand All @@ -243,12 +250,12 @@ object ConfidenceFactory {
context,
clientSecret,
flushPolicies = listOf(minBatchSizeFlushPolicy),
sdkMetadata = SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION),
sdkMetadata = sdk,
dispatcher = dispatcher
)
val flagApplierClient = FlagApplierClientImpl(
clientSecret,
SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION),
sdk,
region,
dispatcher
)
Expand All @@ -258,7 +265,7 @@ object ConfidenceFactory {
region = region,
httpClient = OkHttpClient(),
dispatcher = dispatcher,
sdkMetadata = SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION)
sdkMetadata = sdk
)
val visitorId = ConfidenceValue.String(VisitorUtil.getId(context))
val initContext = initialContext.toMutableMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal class EventSenderEngineImpl(
private val flushPolicies: MutableList<FlushPolicy> = mutableListOf(),
private val clock: Clock = Clock.CalendarBacked.systemUTC(),
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
private val sdkMetadata: SdkMetadata
private var sdkMetadata: SdkMetadata
) : EventSenderEngine {
private val writeReqChannel: Channel<EngineEvent> = Channel()
private val sendChannel: Channel<String> = Channel()
Expand Down Expand Up @@ -95,6 +95,10 @@ internal class EventSenderEngineImpl(
}
}

fun setSdk(sdk: SdkMetadata) {
sdkMetadata = sdk
}

override fun onLowMemoryChannel(): Channel<List<File>> {
return eventStorage.onLowMemoryChannel()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal class RemoteFlagResolver(
private val clientSecret: String,
private val region: ConfidenceRegion,
private val httpClient: OkHttpClient,
private val sdkMetadata: SdkMetadata,
private var sdkMetadata: SdkMetadata,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
private val baseUrl: HttpUrl? = null
) : FlagResolver {
Expand All @@ -36,6 +36,9 @@ internal class RemoteFlagResolver(
"Accept",
"application/json"
)
internal fun setSdk(sdk: SdkMetadata) {
sdkMetadata = sdk
}
override suspend fun resolve(flags: List<String>, context: Map<String, ConfidenceValue>): Result<FlagResolution> {
val sdk = Sdk(sdkMetadata.sdkId, sdkMetadata.sdkVersion)
val request = ResolveFlagsRequest(flags.map { "flags/$it" }, context, clientSecret, false, sdk)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import okhttp3.OkHttpClient

internal class FlagApplierClientImpl : FlagApplierClient {
private val clientSecret: String
private val sdkMetadata: SdkMetadata
private var sdkMetadata: SdkMetadata
private val okHttpClient: OkHttpClient
private val baseUrl: String
private val headers: Headers
Expand Down Expand Up @@ -78,6 +78,10 @@ internal class FlagApplierClientImpl : FlagApplierClient {
)
}

internal fun setSdk(sdk: SdkMetadata) {
sdkMetadata = sdk
}

override suspend fun apply(flags: List<AppliedFlag>, resolveToken: String): Result<Unit> {
val request = ApplyFlagsRequest(
flags.map { AppliedFlag("flags/${it.flag}", it.applyTime) },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@

package com.spotify.confidence.openfeature

import com.spotify.confidence.BuildConfig
import com.spotify.confidence.Confidence
import com.spotify.confidence.ConfidenceError.ErrorCode
import com.spotify.confidence.ConfidenceError.FlagNotFoundError
import com.spotify.confidence.ConfidenceError.ParseError
import com.spotify.confidence.ConfidenceValue
import com.spotify.confidence.Evaluation
import com.spotify.confidence.ResolveReason
import com.spotify.confidence.client.SdkMetadata
import dev.openfeature.sdk.EvaluationContext
import dev.openfeature.sdk.FeatureProvider
import dev.openfeature.sdk.Hook
Expand Down Expand Up @@ -152,6 +154,7 @@ class ConfidenceFeatureProvider private constructor(
eventHandler: EventHandler = EventHandler(Dispatchers.IO),
dispatcher: CoroutineDispatcher = Dispatchers.IO
): ConfidenceFeatureProvider {
confidence.setSdk(SdkMetadata("SDK_ID_KOTLIN_PROVIDER", BuildConfig.SDK_VERSION))
return ConfidenceFeatureProvider(
hooks = hooks,
metadata = metadata,
Expand Down

0 comments on commit 5f9d105

Please sign in to comment.