Skip to content

Commit 42738b8

Browse files
authored
refactor(flipt): upgrade Flipt provider to use latest 1.0.0 Flipt SDK (#638)
Signed-off-by: Mark Phelps <[email protected]>
1 parent 4a5cac2 commit 42738b8

File tree

6 files changed

+109
-74
lines changed

6 files changed

+109
-74
lines changed

providers/flipt/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,34 @@
77
<!-- x-release-please-start-version -->
88

99
```xml
10-
1110
<dependency>
1211
<groupId>dev.openfeature.contrib.providers</groupId>
1312
<artifactId>flipt</artifactId>
14-
<version>0.0.2</version>
13+
<version>0.1.0</version>
1514
</dependency>
1615
```
1716

1817
<!-- x-release-please-end-version -->
1918

2019
## Concepts
20+
2121
* Boolean evaluation gets feature boolean evaluation / enabled status.
2222
* Non-boolean evaluation gets feature variant key.
2323

2424
## Usage
25-
Flipt OpenFeature Provider is using Flipt Java SDK.
25+
26+
Flipt OpenFeature Provider uses Flipt's [Server Java SDK](https://github.com/flipt-io/flipt-server-sdks/tree/main/flipt-java).
2627

2728
### Usage Example
2829

29-
```
30+
```java
31+
// create a Flipt client and provider
32+
FliptClientBuilder fliptClientBuilder = FliptClient.builder().url(apiUrl);
33+
FliptProviderConfig fliptProviderConfig = FliptProviderConfig.builder()
34+
.fliptClientBuilder(fliptClientBuilder)
35+
.build();
36+
37+
// create OpenFeature provider
3038
FeatureProvider featureProvider = new FliptProvider(fliptProviderConfig);
3139
OpenFeatureAPI.getInstance().setProviderAndWait("sync", fliptProvider);
3240
client = OpenFeatureAPI.getInstance().getClient("sync");

providers/flipt/pom.xml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</parent>
1111
<groupId>dev.openfeature.contrib.providers</groupId>
1212
<artifactId>flipt</artifactId>
13-
<version>0.0.2</version> <!--x-release-please-version -->
13+
<version>0.1.0</version> <!--x-release-please-version -->
1414

1515
<name>flipt</name>
1616
<description>Flipt provider for Java</description>
@@ -24,7 +24,7 @@
2424
<dependency>
2525
<groupId>io.flipt</groupId>
2626
<artifactId>flipt-java</artifactId>
27-
<version>0.2.15</version>
27+
<version>1.0.1</version>
2828
</dependency>
2929

3030
<dependency>
@@ -40,12 +40,32 @@
4040
<scope>test</scope>
4141
</dependency>
4242

43+
<dependency>
44+
<groupId>com.fasterxml.jackson.core</groupId>
45+
<artifactId>jackson-core</artifactId>
46+
<version>2.16.1</version>
47+
<scope>test</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>com.fasterxml.jackson.core</groupId>
52+
<artifactId>jackson-databind</artifactId>
53+
<version>2.16.1</version>
54+
<scope>test</scope>
55+
</dependency>
56+
57+
<dependency>
58+
<groupId>com.fasterxml.jackson.core</groupId>
59+
<artifactId>jackson-annotations</artifactId>
60+
<version>2.16.1</version>
61+
<scope>test</scope>
62+
</dependency>
63+
4364
<dependency>
4465
<groupId>org.apache.logging.log4j</groupId>
4566
<artifactId>log4j-slf4j2-impl</artifactId>
4667
<version>2.22.1</version>
4768
<scope>test</scope>
4869
</dependency>
49-
5070
</dependencies>
5171
</project>

providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package dev.openfeature.contrib.providers.flipt;
22

3-
import com.flipt.api.FliptApiClient;
4-
import com.flipt.api.resources.evaluation.types.BooleanEvaluationResponse;
5-
import com.flipt.api.resources.evaluation.types.EvaluationRequest;
6-
import com.flipt.api.resources.evaluation.types.VariantEvaluationResponse;
73
import dev.openfeature.sdk.EvaluationContext;
84
import dev.openfeature.sdk.EventProvider;
95
import dev.openfeature.sdk.ImmutableMetadata;
@@ -14,6 +10,10 @@
1410
import dev.openfeature.sdk.Value;
1511
import dev.openfeature.sdk.exceptions.GeneralError;
1612
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
13+
import io.flipt.api.FliptClient;
14+
import io.flipt.api.evaluation.models.BooleanEvaluationResponse;
15+
import io.flipt.api.evaluation.models.EvaluationRequest;
16+
import io.flipt.api.evaluation.models.VariantEvaluationResponse;
1717
import lombok.AccessLevel;
1818
import lombok.Getter;
1919
import lombok.Setter;
@@ -41,7 +41,7 @@ public class FliptProvider extends EventProvider {
4141

4242
@Setter(AccessLevel.PROTECTED)
4343
@Getter
44-
private FliptApiClient fliptApiClient;
44+
private FliptClient fliptClient;
4545

4646
@Setter(AccessLevel.PROTECTED)
4747
@Getter
@@ -51,6 +51,7 @@ public class FliptProvider extends EventProvider {
5151

5252
/**
5353
* Constructor.
54+
*
5455
* @param fliptProviderConfig FliptProviderConfig
5556
*/
5657
public FliptProvider(FliptProviderConfig fliptProviderConfig) {
@@ -59,6 +60,7 @@ public FliptProvider(FliptProviderConfig fliptProviderConfig) {
5960

6061
/**
6162
* Initialize the provider.
63+
*
6264
* @param evaluationContext evaluation context
6365
* @throws Exception on error
6466
*/
@@ -69,7 +71,7 @@ public void initialize(EvaluationContext evaluationContext) throws Exception {
6971
throw new GeneralError("already initialized");
7072
}
7173
super.initialize(evaluationContext);
72-
fliptApiClient = fliptProviderConfig.getFliptApiClientBuilder().build();
74+
fliptClient = fliptProviderConfig.getFliptClientBuilder().build();
7375

7476
state = ProviderState.READY;
7577
log.info("finished initializing provider, state: {}", state);
@@ -103,45 +105,45 @@ public ProviderEvaluation<Boolean> getBooleanEvaluation(String key, Boolean defa
103105

104106
Map<String, String> contextMap = ContextTransformer.transform(ctx);
105107
EvaluationRequest request = EvaluationRequest.builder().namespaceKey(fliptProviderConfig.getNamespace())
106-
.flagKey(key).entityId(ctx.getTargetingKey()).context(contextMap).build();
108+
.flagKey(key).entityId(ctx.getTargetingKey()).context(contextMap).build();
107109

108110
BooleanEvaluationResponse response = null;
109111
try {
110-
response = fliptApiClient.evaluation().boolean_(request);
112+
response = fliptClient.evaluation().evaluateBoolean(request);
111113
} catch (Exception e) {
112114
log.error("Error evaluating boolean", e);
113115
throw new GeneralError(e.getMessage());
114116
}
115117

116118
return ProviderEvaluation.<Boolean>builder()
117-
.value(response.getEnabled())
118-
.reason(response.getReason().toString())
119-
.build();
119+
.value(response.isEnabled())
120+
.reason(response.getReason().toString())
121+
.build();
120122
}
121123

122124
@Override
123125
public ProviderEvaluation<String> getStringEvaluation(String key, String defaultValue, EvaluationContext ctx) {
124126
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
125127
return ProviderEvaluation.<String>builder()
126-
.value(valueProviderEvaluation.getValue().asString())
127-
.variant(valueProviderEvaluation.getVariant())
128+
.value(valueProviderEvaluation.getValue().asString())
129+
.variant(valueProviderEvaluation.getVariant())
128130
.errorCode(valueProviderEvaluation.getErrorCode())
129131
.reason(valueProviderEvaluation.getReason())
130132
.flagMetadata(valueProviderEvaluation.getFlagMetadata())
131-
.build();
133+
.build();
132134
}
133135

134136
@Override
135137
public ProviderEvaluation<Integer> getIntegerEvaluation(String key, Integer defaultValue, EvaluationContext ctx) {
136138
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
137139
Integer value = getIntegerValue(valueProviderEvaluation, defaultValue);
138140
return ProviderEvaluation.<Integer>builder()
139-
.value(value)
140-
.variant(valueProviderEvaluation.getVariant())
141-
.errorCode(valueProviderEvaluation.getErrorCode())
142-
.reason(valueProviderEvaluation.getReason())
143-
.flagMetadata(valueProviderEvaluation.getFlagMetadata())
144-
.build();
141+
.value(value)
142+
.variant(valueProviderEvaluation.getVariant())
143+
.errorCode(valueProviderEvaluation.getErrorCode())
144+
.reason(valueProviderEvaluation.getReason())
145+
.flagMetadata(valueProviderEvaluation.getFlagMetadata())
146+
.build();
145147
}
146148

147149
private static Integer getIntegerValue(ProviderEvaluation<Value> valueProviderEvaluation, Integer defaultValue) {
@@ -158,12 +160,12 @@ public ProviderEvaluation<Double> getDoubleEvaluation(String key, Double default
158160
ProviderEvaluation<Value> valueProviderEvaluation = getObjectEvaluation(key, new Value(defaultValue), ctx);
159161
Double value = getDoubleValue(valueProviderEvaluation, defaultValue);
160162
return ProviderEvaluation.<Double>builder()
161-
.value(value)
162-
.variant(valueProviderEvaluation.getVariant())
163-
.errorCode(valueProviderEvaluation.getErrorCode())
164-
.reason(valueProviderEvaluation.getReason())
165-
.flagMetadata(valueProviderEvaluation.getFlagMetadata())
166-
.build();
163+
.value(value)
164+
.variant(valueProviderEvaluation.getVariant())
165+
.errorCode(valueProviderEvaluation.getErrorCode())
166+
.reason(valueProviderEvaluation.getReason())
167+
.flagMetadata(valueProviderEvaluation.getFlagMetadata())
168+
.build();
167169
}
168170

169171
private static Double getDoubleValue(ProviderEvaluation<Value> valueProviderEvaluation, Double defaultValue) {
@@ -185,22 +187,22 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
185187
}
186188
Map<String, String> contextMap = ContextTransformer.transform(ctx);
187189
EvaluationRequest request = EvaluationRequest.builder().namespaceKey(fliptProviderConfig.getNamespace())
188-
.flagKey(key).entityId(ctx.getTargetingKey()).context(contextMap).build();
190+
.flagKey(key).entityId(ctx.getTargetingKey()).context(contextMap).build();
189191

190192
VariantEvaluationResponse response;
191193
try {
192-
response = fliptApiClient.evaluation().variant(request);
194+
response = fliptClient.evaluation().evaluateVariant(request);
193195
} catch (Exception e) {
194196
log.error("Error evaluating variant", e);
195197
throw new GeneralError(e.getMessage());
196198
}
197199

198-
if (!response.getMatch()) {
200+
if (!response.isMatch()) {
199201
log.debug("non matching variant for {} : {}", key, response.getReason());
200202
return ProviderEvaluation.<Value>builder()
201-
.value(defaultValue)
202-
.reason(DEFAULT.name())
203-
.build();
203+
.value(defaultValue)
204+
.reason(DEFAULT.name())
205+
.build();
204206
}
205207

206208
ImmutableMetadata.ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata.builder();
@@ -209,11 +211,11 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
209211
}
210212

211213
return ProviderEvaluation.<Value>builder()
212-
.value(new Value(response.getVariantKey()))
213-
.variant(response.getVariantKey())
214-
.reason(TARGETING_MATCH.name())
215-
.flagMetadata(flagMetadataBuilder.build())
216-
.build();
214+
.value(new Value(response.getVariantKey()))
215+
.variant(response.getVariantKey())
216+
.reason(TARGETING_MATCH.name())
217+
.flagMetadata(flagMetadataBuilder.build())
218+
.build();
217219
}
218220

219221
@Override

providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProviderConfig.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
package dev.openfeature.contrib.providers.flipt;
22

3-
import com.flipt.api.FliptApiClientBuilder;
3+
import io.flipt.api.FliptClient.FliptClientBuilder;
44
import lombok.Builder;
55
import lombok.Getter;
66

7-
87
/**
98
* FliptProvider config.
109
*/
1110
@Getter
1211
@Builder
1312
public class FliptProviderConfig {
14-
private FliptApiClientBuilder fliptApiClientBuilder;
13+
private FliptClientBuilder fliptClientBuilder;
1514

1615
@Builder.Default
1716
private String namespace = "default";

0 commit comments

Comments
 (0)