|
| 1 | +package no.nav.tilleggsstonader.libs.unleash |
| 2 | + |
| 3 | +import io.getunleash.DefaultUnleash |
| 4 | +import io.getunleash.UnleashContext |
| 5 | +import io.getunleash.Variant |
| 6 | +import io.getunleash.strategy.Strategy |
| 7 | +import io.getunleash.util.UnleashConfig |
| 8 | + |
| 9 | +internal class DefaultUnleashService( |
| 10 | + apiUrl: String, |
| 11 | + apiToken: String, |
| 12 | + appName: String, |
| 13 | + strategies: List<Strategy>, |
| 14 | +) : UnleashService { |
| 15 | + |
| 16 | + private val defaultUnleash: DefaultUnleash = DefaultUnleash( |
| 17 | + UnleashConfig.builder() |
| 18 | + .appName(appName) |
| 19 | + .unleashAPI("$apiUrl/api") |
| 20 | + .apiKey(apiToken) |
| 21 | + .unleashContextProvider { |
| 22 | + UnleashContext.builder().appName(appName).build() |
| 23 | + }.build(), |
| 24 | + *strategies.toTypedArray(), |
| 25 | + ) |
| 26 | + |
| 27 | + override fun isEnabled( |
| 28 | + toggle: ToggleId, |
| 29 | + defaultValue: Boolean, |
| 30 | + ): Boolean { |
| 31 | + return defaultUnleash.isEnabled(toggle.toggleId, defaultValue) |
| 32 | + } |
| 33 | + |
| 34 | + override fun isEnabled( |
| 35 | + toggle: ToggleId, |
| 36 | + properties: Map<String, String>, |
| 37 | + ): Boolean { |
| 38 | + val builder = UnleashContext.builder() |
| 39 | + properties.forEach { property -> builder.addProperty(property.key, property.value) } |
| 40 | + return defaultUnleash.isEnabled(toggle.toggleId, builder.build()) |
| 41 | + } |
| 42 | + |
| 43 | + override fun getVariant(toggle: ToggleId, defaultValue: Variant): Variant { |
| 44 | + return defaultUnleash.getVariant(toggle.toggleId, defaultValue) |
| 45 | + } |
| 46 | + |
| 47 | + override fun destroy() { |
| 48 | + // Spring trigger denne ved shutdown. Gjøres for å unngå at unleash fortsetter å gjøre kall ut |
| 49 | + defaultUnleash.shutdown() |
| 50 | + } |
| 51 | +} |
0 commit comments