Skip to content

Commit 2237849

Browse files
author
Rob ODwyer
committed
add simplified examples and a workflow to run them on PRs
1 parent ea78374 commit 2237849

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Test Examples
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
7+
jobs:
8+
build:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-java@v3
13+
with:
14+
distribution: temurin
15+
java-version: 11
16+
- name: Setup Gradle
17+
uses: gradle/gradle-build-action@v2
18+
- name: Run local bucketing example
19+
run: ./gradlew runLocalExample
20+
env:
21+
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"
22+
- name: Run cloud bucketing example
23+
run: ./gradlew runCloudExample
24+
env:
25+
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"

build.gradle

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,16 @@ dependencies {
169169
testCompileOnly("org.projectlombok:lombok:$lombok_version")
170170
}
171171

172+
sourceSets.main.java.srcDirs += 'examples'
173+
174+
task runLocalExample(type: JavaExec) {
175+
description = "Run the local bucketing example"
176+
classpath = sourceSets.main.runtimeClasspath
177+
main = 'com.devcycle.sdk.server.local.LocalExample'
178+
}
179+
180+
task runCloudExample(type: JavaExec) {
181+
description = "Run the cloud bucketing example"
182+
classpath = sourceSets.main.runtimeClasspath
183+
main = 'com.devcycle.sdk.server.local.CloudExample'
184+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.devcycle.sdk.server.cloud;
2+
3+
import com.devcycle.sdk.server.cloud.api.DVCCloudClient;
4+
import com.devcycle.sdk.server.cloud.model.DVCCloudOptions;
5+
import com.devcycle.sdk.server.common.exception.DVCException;
6+
import com.devcycle.sdk.server.common.model.User;
7+
8+
public class CloudExample {
9+
public static String VARIABLE_KEY = "test-boolean-variable";
10+
11+
public static void main(String[] args) throws InterruptedException {
12+
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
13+
if (server_sdk_key == null) {
14+
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
15+
System.exit(1);
16+
}
17+
18+
// Create user object
19+
User user = User.builder()
20+
.userId("SOME_USER_ID")
21+
.build();
22+
23+
// The default value can be of type string, boolean, number, or JSON
24+
Boolean defaultValue = false;
25+
26+
DVCCloudOptions dvcOptions = DVCCloudOptions.builder().build();
27+
28+
// Initialize DevCycle Client
29+
DVCCloudClient dvcClient = new DVCCloudClient(server_sdk_key, dvcOptions);
30+
31+
// Fetch variable values using the identifier key, with a default value and user
32+
// object
33+
// The default value can be of type string, boolean, number, or JSON
34+
Boolean variableValue = false;
35+
try {
36+
variableValue = dvcClient.variableValue(user, VARIABLE_KEY, defaultValue);
37+
} catch(DVCException e) {
38+
System.err.println("Error fetching variable value: " + e.getMessage());
39+
System.exit(1);
40+
}
41+
42+
// Use variable value
43+
if (variableValue) {
44+
System.err.println("feature is enabled");
45+
} else {
46+
System.err.println("feature is NOT enabled");
47+
}
48+
}
49+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.devcycle.sdk.server.local;
2+
3+
import com.devcycle.sdk.server.local.api.DVCLocalClient;
4+
import com.devcycle.sdk.server.local.model.DVCLocalOptions;
5+
import com.devcycle.sdk.server.common.model.User;
6+
7+
public class LocalExample {
8+
public static String VARIABLE_KEY = "test-boolean-variable";
9+
10+
public static void main(String[] args) throws InterruptedException {
11+
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
12+
if (server_sdk_key == null) {
13+
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
14+
System.exit(1);
15+
}
16+
17+
// Create user object
18+
User user = User.builder()
19+
.userId("SOME_USER_ID")
20+
.build();
21+
22+
// The default value can be of type string, boolean, number, or JSON
23+
Boolean defaultValue = false;
24+
25+
DVCLocalOptions dvcOptions = DVCLocalOptions.builder().configPollingIntervalMs(60000)
26+
.disableAutomaticEventLogging(false).disableCustomEventLogging(false).build();
27+
28+
// Initialize DevCycle Client
29+
DVCLocalClient dvcClient = new DVCLocalClient(server_sdk_key, dvcOptions);
30+
31+
for (int i = 0; i < 10; i++) {
32+
if(dvcClient.isInitialized()) {
33+
break;
34+
}
35+
Thread.sleep(500);
36+
}
37+
38+
// Fetch variable values using the identifier key, with a default value and user
39+
// object
40+
// The default value can be of type string, boolean, number, or JSON
41+
Boolean variableValue = dvcClient.variableValue(user, VARIABLE_KEY, defaultValue);
42+
43+
// Use variable value
44+
if (variableValue) {
45+
System.err.println("feature is enabled");
46+
} else {
47+
System.err.println("feature is NOT enabled");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)