Repository navigation
Expand file tree
/
Copy pathApp.java
More file actions
86 lines (76 loc) · 3.27 KB
/
Copy pathApp.java
File metadata and controls
86 lines (76 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.example;
import com.featurevisor.sdk.Featurevisor;
import com.featurevisor.sdk.FeaturevisorLogLevel;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.Map;
public class App {
private static final String DATAFILE_URL =
"https://featurevisor-example-cloudflare.pages.dev/production/featurevisor-sdk-v3.json";
public static void main(String[] args) throws Exception {
String datafile = fetchDatafile();
Map<String, Object> context = Map.of(
"userId", "customer-123",
"country", "nl",
"locale", "nl-NL",
"accountPlan", "pro"
);
Featurevisor f = Featurevisor.createFeaturevisor(
new Featurevisor.FeaturevisorOptions()
.datafileString(datafile)
.context(context)
.logLevel(FeaturevisorLogLevel.ERROR)
);
try {
boolean commerceEnabled = f.isEnabled("commerce_platform");
String checkoutVariation = f.getVariation("checkout_experience");
Integer maxItems = f.getVariableInteger("checkout_experience", "max_items");
List<String> paymentMethods = f.getVariableArray(
"checkout_experience",
"payment_methods"
);
Map<String, Object> endpoints = f.getVariableObject("serviceEndpoints");
String supportContact = f.getVariableString("supportContact");
System.out.println("Commerce platform enabled: " + commerceEnabled);
System.out.println("Checkout variation: " + valueOrUnavailable(checkoutVariation));
System.out.println("Maximum checkout items: " + valueOrUnavailable(maxItems));
System.out.println("Payment methods: " + paymentMethods);
System.out.printf(
"Service endpoint: %s (timeout: %s ms, retries: %s)%n",
endpoints.get("baseUrl"),
endpoints.get("timeoutMs"),
endpoints.get("retries")
);
System.out.println("Support contact: " + valueOrUnavailable(supportContact));
} finally {
f.close();
}
}
private static String fetchDatafile() throws Exception {
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(10))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(DATAFILE_URL))
.timeout(Duration.ofSeconds(10))
.header("Accept", "application/json")
.build();
HttpResponse<String> response = client.send(
request,
HttpResponse.BodyHandlers.ofString()
);
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new IllegalStateException(
"Datafile request failed with HTTP " + response.statusCode()
);
}
return response.body();
}
private static Object valueOrUnavailable(Object value) {
return value == null ? "unavailable" : value;
}
}