All Downloads are FREE. Search and download functionalities are using the official Maven repository.

deployment.DeploymentApi.kt Maven / Gradle / Ivy

There is a newer version: 0.5.1
Show newest version
package com.amplitude.deployment

import com.amplitude.VERSION
import com.amplitude.experiment.evaluation.EvaluationFlag
import com.amplitude.util.get
import com.amplitude.util.json
import com.amplitude.util.logger
import com.amplitude.util.retry
import io.ktor.client.HttpClient
import io.ktor.client.call.body
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.request.headers
import io.ktor.client.request.parameter

internal interface DeploymentApi {
    suspend fun getFlagConfigs(deploymentKey: String): List
}

internal class DeploymentApiV1(
    private val serverUrl: String
) : DeploymentApi {

    companion object {
        val log by logger()
    }

    private val client = HttpClient(OkHttp)

    override suspend fun getFlagConfigs(deploymentKey: String): List {
        log.trace("getFlagConfigs: start - deploymentKey=$deploymentKey")
        val response = retry(onFailure = { e -> log.error("Get flag configs failed: $e") }) {
            client.get(serverUrl, "/sdk/v2/flags") {
                parameter("v", "0")
                headers {
                    set("Authorization", "Api-Key $deploymentKey")
                    set("X-Amp-Exp-Library", "experiment-local-proxy/$VERSION")
                }
            }
        }
        return json.decodeFromString>(response.body()).also {
            log.trace("getFlagConfigs: end - deploymentKey=$deploymentKey")
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy