Skip to content

Commit 1d0abcd

Browse files
committed
refactor(flipt): upgrade Flipt provider to use latest 1.0.0 Flipt SDK
1 parent 4f6c150 commit 1d0abcd

File tree

6 files changed

+86
-72
lines changed

6 files changed

+86
-72
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 fliptApiClientBuilder = FliptClient.builder().url(apiUrl);
33+
FliptProviderConfig fliptProviderConfig = FliptProviderConfig.builder()
34+
.fliptClientBuilder(fliptApiClientBuilder)
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: 2 additions & 2 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>
@@ -20,7 +20,7 @@
2020
<dependency>
2121
<groupId>io.flipt</groupId>
2222
<artifactId>flipt-java</artifactId>
23-
<version>0.2.15</version>
23+
<version>1.0.0</version>
2424
</dependency>
2525

2626
<dependency>

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 fliptApiClient;
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+
fliptApiClient = 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 = fliptApiClient.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 = fliptApiClient.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";

providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package dev.openfeature.contrib.providers.flipt;
22

3-
import com.flipt.api.FliptApiClient;
4-
import com.flipt.api.FliptApiClientBuilder;
5-
import com.flipt.api.core.Environment;
3+
import io.flipt.api.FliptClient;
4+
import io.flipt.api.FliptClient.FliptClientBuilder;
65
import com.github.tomakehurst.wiremock.client.WireMock;
76
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
87
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
@@ -71,22 +70,22 @@ public void shutdown() {
7170

7271
private void mockFliptAPI(String url, String resourceName, String flagKey) {
7372
stubFor(
74-
post(urlEqualTo(url))
75-
.withHeader("Content-Type", equalTo("application/json"))
76-
.withRequestBody(WireMock.containing(flagKey))
77-
.willReturn(
78-
aResponse()
79-
.withStatus(200)
80-
.withHeader("Content-Type", "application/json")
81-
.withBody(readResourceFileContent(resourceName))));
73+
post(urlEqualTo(url))
74+
.withHeader("Content-Type", equalTo("application/json"))
75+
.withRequestBody(WireMock.containing(flagKey))
76+
.willReturn(
77+
aResponse()
78+
.withStatus(200)
79+
.withHeader("Content-Type", "application/json")
80+
.withBody(readResourceFileContent(resourceName))));
8281
}
8382

8483
@SneakyThrows
8584
private FliptProvider buildFliptProvider() {
86-
FliptApiClientBuilder fliptApiClientBuilder = FliptApiClient.builder().url(apiUrl).environment(Environment.custom(apiUrl));
85+
FliptClientBuilder fliptApiClientBuilder = FliptClient.builder().url(apiUrl);
8786
FliptProviderConfig fliptProviderConfig = FliptProviderConfig.builder()
88-
.fliptApiClientBuilder(fliptApiClientBuilder)
89-
.build();
87+
.fliptClientBuilder(fliptApiClientBuilder)
88+
.build();
9089
return new FliptProvider(fliptProviderConfig);
9190
}
9291

@@ -112,7 +111,7 @@ void getStringVariantEvaluation() {
112111
MutableContext evaluationContext = new MutableContext();
113112
evaluationContext.setTargetingKey(TARGETING_KEY);
114113
assertEquals(VARIANT_FLAG_VALUE, fliptProvider.getStringEvaluation(VARIANT_FLAG_NAME, "",
115-
evaluationContext).getValue());
114+
evaluationContext).getValue());
116115
assertEquals(VARIANT_FLAG_VALUE, client.getStringValue(VARIANT_FLAG_NAME, "", evaluationContext));
117116
assertEquals("fallback_str", client.getStringValue("non-existing", "fallback_str", evaluationContext));
118117
}
@@ -124,7 +123,7 @@ void getIntegerEvaluation() {
124123
evaluationContext.setTargetingKey(TARGETING_KEY);
125124
evaluationContext.add("userId", "int");
126125
assertEquals(INT_FLAG_VALUE, fliptProvider.getIntegerEvaluation(INT_FLAG_NAME, 1,
127-
evaluationContext).getValue());
126+
evaluationContext).getValue());
128127
assertEquals(INT_FLAG_VALUE, client.getIntegerValue(INT_FLAG_NAME, 1, evaluationContext));
129128
assertEquals(1, client.getIntegerValue("non-existing", 1, evaluationContext));
130129

@@ -139,7 +138,7 @@ void getDoubleEvaluation() {
139138
evaluationContext.setTargetingKey(TARGETING_KEY);
140139
evaluationContext.add("userId", "double");
141140
assertEquals(DOUBLE_FLAG_VALUE, fliptProvider.getDoubleEvaluation(DOUBLE_FLAG_NAME, 1.1,
142-
evaluationContext).getValue());
141+
evaluationContext).getValue());
143142
assertEquals(DOUBLE_FLAG_VALUE, client.getDoubleValue(DOUBLE_FLAG_NAME, 1.1, evaluationContext));
144143
assertEquals(1.1, client.getDoubleValue("non-existing", 1.1, evaluationContext));
145144

@@ -153,7 +152,8 @@ void getStringEvaluationByUser() {
153152
MutableContext evaluationContext = new MutableContext();
154153
evaluationContext.setTargetingKey(TARGETING_KEY);
155154
evaluationContext.add("userId", "111");
156-
assertEquals(VARIANT_FLAG_VALUE, fliptProvider.getStringEvaluation(USERS_FLAG_NAME, "", evaluationContext).getValue());
155+
assertEquals(VARIANT_FLAG_VALUE,
156+
fliptProvider.getStringEvaluation(USERS_FLAG_NAME, "", evaluationContext).getValue());
157157
assertEquals(VARIANT_FLAG_VALUE, client.getStringValue(USERS_FLAG_NAME, "", evaluationContext));
158158
evaluationContext.add("userId", "2");
159159
assertEquals("", client.getStringValue(USERS_FLAG_NAME, "", evaluationContext));
@@ -165,10 +165,11 @@ void getEvaluationMetadataTest() {
165165
MutableContext evaluationContext = new MutableContext();
166166
evaluationContext.setTargetingKey(TARGETING_KEY);
167167
ProviderEvaluation<String> stringEvaluation = fliptProvider.getStringEvaluation(VARIANT_FLAG_NAME, "",
168-
evaluationContext);
168+
evaluationContext);
169169
ImmutableMetadata flagMetadata = stringEvaluation.getFlagMetadata();
170170
assertEquals("attachment-1", flagMetadata.getString("variant-attachment"));
171-
FlagEvaluationDetails<String> nonExistingFlagEvaluation = client.getStringDetails("non-existing", "", evaluationContext);
171+
FlagEvaluationDetails<String> nonExistingFlagEvaluation = client.getStringDetails("non-existing", "",
172+
evaluationContext);
172173
assertEquals(null, nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
173174
}
174175

@@ -179,11 +180,13 @@ void shouldThrowIfNotInitialized() {
179180
assertEquals(ProviderState.NOT_READY, asyncInitfliptProvider.getState());
180181

181182
// ErrorCode.PROVIDER_NOT_READY should be returned when evaluated via the client
182-
assertThrows(ProviderNotReadyError.class, ()-> asyncInitfliptProvider.getBooleanEvaluation("fail_not_initialized", false, new ImmutableContext()));
183-
assertThrows(ProviderNotReadyError.class, ()-> asyncInitfliptProvider.getStringEvaluation("fail_not_initialized", "", new ImmutableContext()));
183+
assertThrows(ProviderNotReadyError.class, () -> asyncInitfliptProvider
184+
.getBooleanEvaluation("fail_not_initialized", false, new ImmutableContext()));
185+
assertThrows(ProviderNotReadyError.class,
186+
() -> asyncInitfliptProvider.getStringEvaluation("fail_not_initialized", "", new ImmutableContext()));
184187

185188
asyncInitfliptProvider.initialize(null);
186-
assertThrows(GeneralError.class, ()-> asyncInitfliptProvider.initialize(null));
189+
assertThrows(GeneralError.class, () -> asyncInitfliptProvider.initialize(null));
187190

188191
asyncInitfliptProvider.shutdown();
189192
}
@@ -197,8 +200,10 @@ void shouldThrowIfErrorEvent() {
197200
asyncInitfliptProvider.emitProviderError(ProviderEventDetails.builder().build());
198201

199202
// ErrorCode.PROVIDER_NOT_READY should be returned when evaluated via the client
200-
assertThrows(GeneralError.class, ()-> asyncInitfliptProvider.getBooleanEvaluation("fail", false, new ImmutableContext()));
201-
assertThrows(GeneralError.class, ()-> asyncInitfliptProvider.getStringEvaluation("fail", "", new ImmutableContext()));
203+
assertThrows(GeneralError.class,
204+
() -> asyncInitfliptProvider.getBooleanEvaluation("fail", false, new ImmutableContext()));
205+
assertThrows(GeneralError.class,
206+
() -> asyncInitfliptProvider.getStringEvaluation("fail", "", new ImmutableContext()));
202207

203208
asyncInitfliptProvider.shutdown();
204209
}

providers/flipt/version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.2
1+
0.1.0

0 commit comments

Comments
 (0)