Skip to content

Commit

Permalink
feat: Enable use of project API key for default deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
tyiuhc committed Feb 1, 2024
1 parent 733923a commit 99b9c25
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
18 changes: 11 additions & 7 deletions src/main/kotlin/Experiment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ object Experiment {
* Initializes a singleton [RemoteEvaluationClient] instance. Subsequent calls will return the
* same instance, regardless of api key or config.
*
* @param apiKey The API key. This can be found in the Experiment settings and should not be null or empty.
* @param apiKey The Amplitude Project API key. This can be found in the Organization Settings -> Projects,
* and should not be null or empty. If a deployment key is provided in the config, it will be used instead.
* @param config see [RemoteEvaluationConfig] for configuration options
*/
@JvmStatic
Expand All @@ -26,14 +27,15 @@ object Experiment {
apiKey: String,
config: RemoteEvaluationConfig = RemoteEvaluationConfig()
): RemoteEvaluationClient = synchronized(remoteInstances) {
return when (val instance = remoteInstances[apiKey]) {
val usedKey = config.deploymentKey ?: apiKey
return when (val instance = remoteInstances[usedKey]) {
null -> {
Logger.implementation = SystemLogger(config.debug)
val newInstance = RemoteEvaluationClient(
apiKey,
usedKey,
config,
)
remoteInstances[apiKey] = newInstance
remoteInstances[usedKey] = newInstance
newInstance
}
else -> instance
Expand All @@ -43,7 +45,8 @@ object Experiment {
* Initializes a singleton [LocalEvaluationClient] instance. Subsequent calls will return the
* same instance, regardless of api key or config.
*
* @param apiKey The API key. This can be found in the Experiment settings and should not be null or empty.
* @param apiKey The Amplitude Project API key. This can be found in the Organization Settings -> Projects,
* and should not be null or empty. If a deployment key is provided in the config, it will be used instead.
* @param config see [LocalEvaluationConfig] for configuration options
*/
@JvmStatic
Expand All @@ -52,14 +55,15 @@ object Experiment {
apiKey: String,
config: LocalEvaluationConfig = LocalEvaluationConfig(),
): LocalEvaluationClient = synchronized(localInstances) {
val usedKey = config.deploymentKey ?: apiKey
return when (val instance = localInstances[apiKey]) {
null -> {
Logger.implementation = SystemLogger(config.debug)
val newInstance = LocalEvaluationClient(
apiKey,
usedKey,
config,
)
localInstances[apiKey] = newInstance
localInstances[usedKey] = newInstance
newInstance
}
else -> instance
Expand Down
13 changes: 13 additions & 0 deletions src/main/kotlin/LocalEvaluationConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class LocalEvaluationConfig internal constructor(
val flagConfigPollerRequestTimeoutMillis: Long = Defaults.FLAG_CONFIG_POLLER_REQUEST_TIMEOUT_MILLIS,
@JvmField
val assignmentConfiguration: AssignmentConfiguration? = Defaults.ASSIGNMENT_CONFIGURATION,
@JvmField
val deploymentKey: String? = Defaults.DEPLOYMENT_KEY,
) {

/**
Expand Down Expand Up @@ -53,6 +55,11 @@ class LocalEvaluationConfig internal constructor(
* null
*/
val ASSIGNMENT_CONFIGURATION: AssignmentConfiguration? = null

/**
* null
*/
val DEPLOYMENT_KEY: String? = null
}

companion object {
Expand All @@ -69,6 +76,7 @@ class LocalEvaluationConfig internal constructor(
private var flagConfigPollerIntervalMillis = Defaults.FLAG_CONFIG_POLLER_INTERVAL_MILLIS
private var flagConfigPollerRequestTimeoutMillis = Defaults.FLAG_CONFIG_POLLER_REQUEST_TIMEOUT_MILLIS
private var assignmentConfiguration = Defaults.ASSIGNMENT_CONFIGURATION
private var deploymentKey = Defaults.DEPLOYMENT_KEY

fun debug(debug: Boolean) = apply {
this.debug = debug
Expand All @@ -90,13 +98,18 @@ class LocalEvaluationConfig internal constructor(
this.assignmentConfiguration = assignmentConfiguration
}

fun deploymentKey(deploymentKey: String) = apply {
this.deploymentKey = deploymentKey
}

fun build(): LocalEvaluationConfig {
return LocalEvaluationConfig(
debug = debug,
serverUrl = serverUrl,
flagConfigPollerIntervalMillis = flagConfigPollerIntervalMillis,
flagConfigPollerRequestTimeoutMillis = flagConfigPollerRequestTimeoutMillis,
assignmentConfiguration = assignmentConfiguration,
deploymentKey = deploymentKey,
)
}
}
Expand Down
24 changes: 20 additions & 4 deletions src/main/kotlin/RemoteEvaluationConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class RemoteEvaluationConfig internal constructor(
val fetchRetryBackoffScalar: Double = Defaults.FETCH_RETRY_BACKOFF_SCALAR,
// @JvmField
// val fetchRetryTimeoutMillis: Long = Defaults.FETCH_RETRY_TIMEOUT_MILLIS,
@JvmField
val deploymentKey: String? = Defaults.DEPLOYMENT_KEY,
) {

/**
Expand All @@ -49,18 +51,22 @@ class RemoteEvaluationConfig internal constructor(
* 500
*/
const val FETCH_TIMEOUT_MILLIS = 500L

/**
* 1
*/
const val FETCH_RETRIES = 1

/**
* 0
*/
const val FETCH_RETRY_BACKOFF_MIN_MILLIS = 0L

/**
* 10000
*/
const val FETCH_RETRY_BACKOFF_MAX_MILLIS = 10000L

/**
* 1
*/
Expand All @@ -69,6 +75,10 @@ class RemoteEvaluationConfig internal constructor(
// * 500
// */
// const val FETCH_RETRY_TIMEOUT_MILLIS = 500L
/**
* null
*/
val DEPLOYMENT_KEY = null
}

companion object {
Expand All @@ -88,6 +98,7 @@ class RemoteEvaluationConfig internal constructor(
private var fetchRetryBackoffMaxMillis = Defaults.FETCH_RETRY_BACKOFF_MAX_MILLIS
private var fetchRetryBackoffScalar = Defaults.FETCH_RETRY_BACKOFF_SCALAR
// private var fetchRetryTimeoutMillis = Defaults.FETCH_RETRY_TIMEOUT_MILLIS
private var deploymentKey: String? = Defaults.DEPLOYMENT_KEY

fun debug(debug: Boolean) = apply {
this.debug = debug
Expand Down Expand Up @@ -121,6 +132,10 @@ class RemoteEvaluationConfig internal constructor(
// this.fetchRetryTimeoutMillis = fetchRetryTimeoutMillis
// }

fun deploymentKey(deploymentKey: String) = apply {
this.deploymentKey = deploymentKey
}

fun build(): RemoteEvaluationConfig {
return RemoteEvaluationConfig(
debug = debug,
Expand All @@ -129,16 +144,17 @@ class RemoteEvaluationConfig internal constructor(
fetchRetries = fetchRetries,
fetchRetryBackoffMinMillis = fetchRetryBackoffMinMillis,
fetchRetryBackoffMaxMillis = fetchRetryBackoffMaxMillis,
fetchRetryBackoffScalar = fetchRetryBackoffScalar
fetchRetryBackoffScalar = fetchRetryBackoffScalar,
// fetchRetryTimeoutMillis = fetchRetryTimeoutMillis,
deploymentKey = deploymentKey
)
}
}

override fun toString(): String {
return "ExperimentConfig(debug=$debug, serverUrl='$serverUrl', fetchTimeoutMillis=$fetchTimeoutMillis, " +
"fetchRetries=$fetchRetries, fetchRetryBackoffMinMillis=$fetchRetryBackoffMinMillis, " +
"fetchRetryBackoffMaxMillis=$fetchRetryBackoffMaxMillis, " +
"fetchRetryBackoffScalar=$fetchRetryBackoffScalar)"
"fetchRetries=$fetchRetries, fetchRetryBackoffMinMillis=$fetchRetryBackoffMinMillis, " +
"fetchRetryBackoffMaxMillis=$fetchRetryBackoffMaxMillis, " +
"fetchRetryBackoffScalar=$fetchRetryBackoffScalar)"
}
}

0 comments on commit 99b9c25

Please sign in to comment.