Skip to content

Commit 9929be6

Browse files
DVC-7288 Add Variable Value method (#57)
* adding new method to the local and cloud clients * Updating example apps to use new variableValue() method and fixing bugs * Retrigger github actions
1 parent d87ff6d commit 9929be6

File tree

14 files changed

+175
-30
lines changed

14 files changed

+175
-30
lines changed

example-cloud/src/main/java/com/devcycle/example/java/sdk/app/controller/HelloWorld.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,48 @@ public String homePage(Model model) {
3636
return "home";
3737
}
3838

39+
3940
@GetMapping("/cloud/activateFlag")
40-
public String homePageActivatedFlag(Model model) {
41-
Variable<String> updateHomePage = dvcCloud.variable(getUser(), "string-var", "default string");
41+
public String homePageActivatedFlagValue(Model model) {
42+
String variableKey = "string-var";
43+
// if the variable "string-var" doesn't exist or is not applicable for the user, the default value will be returned
44+
try {
45+
String updateHomePageValue = dvcCloud.variableValue(getUser(), variableKey, "default string");
46+
model.addAttribute("variableKey", variableKey);
47+
model.addAttribute("variationValue", updateHomePageValue);
48+
}catch (DVCException e){
49+
System.out.println("DVCException: " + e.getMessage());
50+
}
51+
return "fragments/flagData :: value ";
52+
}
4253

43-
String variationValue = updateHomePage.getValue();
54+
@GetMapping("/cloud/activateFlagDetails")
55+
public String homePageActivatedFlagDetails(Model model) {
56+
String variableKey = "string-var";
57+
try{
58+
Variable<String> updateHomePageVariable = dvcCloud.variable(getUser(), variableKey, "default string");
4459

45-
// if the variable "activate-flag" doesn't exist isDefaulted will be true
46-
model.addAttribute("isDefaultValue", updateHomePage.getIsDefaulted());
47-
model.addAttribute("variationValue", variationValue);
48-
return "fragments/flagData :: value ";
60+
// if the variable "string-var" doesn't exist isDefaulted will be true
61+
model.addAttribute("isDefaultValue", updateHomePageVariable.getIsDefaulted());
62+
model.addAttribute("variableKey", variableKey);
63+
model.addAttribute("variationValue", updateHomePageVariable.getValue());
64+
}catch (DVCException e){
65+
System.out.println("DVCException: " + e.getMessage());
66+
}
67+
return "fragments/flagDataDetails :: value ";
4968
}
5069

5170
@GetMapping("/cloud/track")
5271
public String trackCloud(Model model) {
53-
DVCResponse response = null;
72+
String response = "";
5473
try {
55-
response = dvcCloud.track(getUser(), Event.builder().type("java-cloud-custom").build());
74+
dvcCloud.track(getUser(), Event.builder().type("java-cloud-custom").build());
75+
response = "java-cloud-custom tracked!";
5676
} catch(DVCException e) {
57-
System.out.println("Error tracking custom event: " + e.getMessage());
77+
response = "Error tracking custom event: " + e.getMessage();
5878
}
5979
model.addAttribute("trackSuccessMessage", "Cloud custom event tracked!");
60-
model.addAttribute("trackResponse", response.getMessage());
80+
model.addAttribute("trackResponse", response);
6181
return "fragments/trackData :: value ";
6282
}
6383

example-cloud/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ spring.thymeleaf.suffix=.html
66
spring.application.name=Default Flag
77
spring.application.oops=Default Flag when variation could not be fetched
88

9-
devcycle.sdkKey=your SDK Key
9+
devcycle.sdkKey=Your DVC_SERVER_SDK_KEY here
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<div th:fragment="value">
22
<H2 class="display-5 fw-bold">DevCycle Flag</H2>
33
<div>
4-
<label>Using default value?</label>
5-
<span th:text="${isDefaultValue}"></span>
4+
<label>Key:</label>
5+
<span th:text="${variableKey}"></span>
66
</div>
77
<div>
8-
<label>Variation value:</label>
8+
<label>Value:</label>
99
<span th:text="${variationValue}"></span>
1010
</div>
1111
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div th:fragment="value">
2+
<H2 class="display-5 fw-bold">DevCycle Flag</H2>
3+
<div>
4+
<label>Key:</label>
5+
<span th:text="${variableKey}"></span>
6+
</div>
7+
<div>
8+
<label>Using default value?</label>
9+
<span th:text="${isDefaultValue}"></span>
10+
</div>
11+
<div>
12+
<label>Variation value:</label>
13+
<span th:text="${variationValue}"></span>
14+
</div>
15+
</div>

example-cloud/src/main/resources/templates/home.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Cloud
2323
<p class="lead mb-4">Simple Spring Boot example to show a default value before creating a feature,
2424
fetching a variable, and tracking custom events via the DevCycle SDK</p>
2525
<div class="gap-2 d-sm-flex justify-content-sm-center pb-2">
26+
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="getValue()">
27+
Cloud - Get Value
28+
</button>
2629
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="getVariable()">
27-
Cloud - Get Variable
30+
Cloud - Get Variable Details
2831
</button>
2932
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="trackCloud()">
3033
Cloud - Track
@@ -43,7 +46,7 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Cloud
4346
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
4447
crossorigin="anonymous"></script>
4548
<script>
46-
const getVariable = () => {
49+
const getValue = () => {
4750
fetch('cloud/activateFlag').then(function (response) {
4851
return response.text();
4952
}).then(function (html) {
@@ -53,6 +56,16 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Cloud
5356
});
5457
}
5558

59+
const getVariable = () => {
60+
fetch('cloud/activateFlagDetails').then(function (response) {
61+
return response.text();
62+
}).then(function (html) {
63+
document.getElementById("variablePlaceholder").innerHTML = html;
64+
}).catch(function (err) {
65+
console.warn('Something went wrong.', err);
66+
});
67+
}
68+
5669
const trackCloud = () => {
5770
fetch('cloud/track').then(function (response) {
5871
return response.text();

example-local/src/main/java/com/devcycle/example/java/sdk/app/controller/HelloWorld.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,26 @@ public String homePage(Model model) {
4141
}
4242

4343
@GetMapping("/local/activateFlag")
44-
public String homePageActivatedFlag(Model model) {
45-
Variable<String> updateHomePage = dvcClient.variable(getUser(), "string-var", "default string");
44+
public String homePageActivatedFlagValue(Model model) {
45+
String variableKey = "string-var";
46+
// if the variable "string-var" doesn't exist or is not applicable for the user, the default value will be returned
47+
String updateHomePage = dvcClient.variableValue(getUser(), variableKey, "default string");
48+
model.addAttribute("variableKey", variableKey);
49+
model.addAttribute("variationValue", updateHomePage);
50+
return "fragments/flagData :: value ";
51+
}
4652

47-
String variationValue = updateHomePage.getValue();
53+
@GetMapping("/local/activateFlagDetails")
54+
public String homePageActivatedFlagDetails(Model model) {
55+
String variableKey = "string-var";
56+
Variable<String> updateHomePageVariable = dvcClient.variable(getUser(), variableKey, "default string");
4857

49-
// if the variable "activate-flag" doesn't exist isDefaulted will be true
50-
model.addAttribute("isDefaultValue", updateHomePage.getIsDefaulted());
51-
model.addAttribute("variationValue", variationValue);
52-
return "fragments/flagData :: value ";
58+
// if the variable "string-var" doesn't exist isDefaulted will be true
59+
60+
model.addAttribute("isDefaultValue", updateHomePageVariable.getIsDefaulted());
61+
model.addAttribute("variableKey", variableKey);
62+
model.addAttribute("variationValue", updateHomePageVariable.getValue());
63+
return "fragments/flagDataDetails :: value ";
5364
}
5465

5566
@GetMapping("/local/track")

example-local/src/main/resources/application.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ spring.thymeleaf.suffix=.html
66
spring.application.name=Default Flag
77
spring.application.oops=Default Flag when variation could not be fetched
88

9-
devcycle.sdkKey=your SDK Key
9+
devcycle.sdkKey=Your DVC_SERVER_SDK_KEY here
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<div th:fragment="value">
22
<H2 class="display-5 fw-bold">DevCycle Flag</H2>
33
<div>
4-
<label>Using default value?</label>
5-
<span th:text="${isDefaultValue}"></span>
4+
<label>Key:</label>
5+
<span th:text="${variableKey}"></span>
66
</div>
77
<div>
8-
<label>Variation value:</label>
8+
<label>Value:</label>
99
<span th:text="${variationValue}"></span>
1010
</div>
1111
</div>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<div th:fragment="value">
2+
<H2 class="display-5 fw-bold">DevCycle Flag</H2>
3+
<div>
4+
<label>Key:</label>
5+
<span th:text="${variableKey}"></span>
6+
</div>
7+
<div>
8+
<label>Using default value?</label>
9+
<span th:text="${isDefaultValue}"></span>
10+
</div>
11+
<div>
12+
<label>Variation value:</label>
13+
<span th:text="${variationValue}"></span>
14+
</div>
15+
</div>

example-local/src/main/resources/templates/home.html

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Local
2323
<p class="lead mb-4">Simple Spring Boot example to show a default value before creating a feature,
2424
fetching a variable, and tracking custom events via the DevCycle SDK</p>
2525
<div class="gap-2 d-sm-flex justify-content-sm-center pb-2">
26+
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="getValue()">
27+
Local - Get Value
28+
</button>
2629
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="getVariable()">
27-
Local - Get Variable
30+
Local - Get Variable Details
2831
</button>
2932
<button type="button" class="btn btn-primary btn-lg px-4 gap-3" th:onclick="trackLocal()">
3033
Local - Track
@@ -43,7 +46,7 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Local
4346
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
4447
crossorigin="anonymous"></script>
4548
<script>
46-
const getVariable = () => {
49+
const getValue = () => {
4750
fetch('local/activateFlag').then(function (response) {
4851
return response.text();
4952
}).then(function (html) {
@@ -53,6 +56,16 @@ <h1 class="display-5 fw-bold">Example app for the DevCycle Java SDK using Local
5356
});
5457
}
5558

59+
const getVariable = () => {
60+
fetch('local/activateFlagDetails').then(function (response) {
61+
return response.text();
62+
}).then(function (html) {
63+
document.getElementById("variablePlaceholder").innerHTML = html;
64+
}).catch(function (err) {
65+
console.warn('Something went wrong.', err);
66+
});
67+
}
68+
5669
const trackLocal = () => {
5770
fetch('local/track').then(function (response) {
5871
return response.text();

0 commit comments

Comments
 (0)