Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test Examples

on:
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-java@v3
with:
distribution: temurin
java-version: 11
- name: Setup Gradle
uses: gradle/gradle-build-action@v2
- name: Run local bucketing example
run: ./gradlew runLocalExample
env:
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"
- name: Run cloud bucketing example
run: ./gradlew runCloudExample
env:
DEVCYCLE_SERVER_SDK_KEY: "${{ secrets.DEVCYCLE_SERVER_SDK_KEY }}"
26 changes: 26 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,29 @@ dependencies {
testCompileOnly("org.projectlombok:lombok:$lombok_version")
}

// Gradle magic for adding a new "examples" source set that's separate from the main source set
sourceSets {
examples {
java {
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
}
}
}

configurations {
examplesImplementation.extendsFrom implementation
examplesRuntimeOnly.extendsFrom runtimeOnly
}

task runLocalExample(type: JavaExec) {
description = "Run the local bucketing example"
classpath = sourceSets.examples.runtimeClasspath
main = 'com.devcycle.examples.LocalExample'
}

task runCloudExample(type: JavaExec) {
description = "Run the cloud bucketing example"
classpath = sourceSets.examples.runtimeClasspath
main = 'com.devcycle.examples.CloudExample'
}
49 changes: 49 additions & 0 deletions src/examples/java/com/devcycle/examples/CloudExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.devcycle.examples;

import com.devcycle.sdk.server.cloud.api.DevCycleCloudClient;
import com.devcycle.sdk.server.cloud.model.DevCycleCloudOptions;
import com.devcycle.sdk.server.common.exception.DevCycleException;
import com.devcycle.sdk.server.common.model.DevCycleUser;

public class CloudExample {
public static String VARIABLE_KEY = "test-boolean-variable";

public static void main(String[] args) throws InterruptedException {
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
if (server_sdk_key == null) {
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
System.exit(1);
}

// Create user object
DevCycleUser user = DevCycleUser.builder()
.userId("SOME_USER_ID")
.build();

// The default value can be of type string, boolean, number, or JSON
Boolean defaultValue = false;

DevCycleCloudOptions options = DevCycleCloudOptions.builder().build();

// Initialize DevCycle Client
DevCycleCloudClient client = new DevCycleCloudClient(server_sdk_key, options);

// Fetch variable values using the identifier key, with a default value and user
// object
// The default value can be of type string, boolean, number, or JSON
Boolean variableValue = false;
try {
variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);
} catch(DevCycleException e) {
System.err.println("Error fetching variable value: " + e.getMessage());
System.exit(1);
}

// Use variable value
if (variableValue) {
System.err.println("feature is enabled");
} else {
System.err.println("feature is NOT enabled");
}
}
}
50 changes: 50 additions & 0 deletions src/examples/java/com/devcycle/examples/LocalExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.devcycle.examples;

import com.devcycle.sdk.server.local.api.DevCycleLocalClient;
import com.devcycle.sdk.server.local.model.DevCycleLocalOptions;
import com.devcycle.sdk.server.common.model.DevCycleUser;

public class LocalExample {
public static String VARIABLE_KEY = "test-boolean-variable";

public static void main(String[] args) throws InterruptedException {
String server_sdk_key = System.getenv("DEVCYCLE_SERVER_SDK_KEY");
if (server_sdk_key == null) {
System.err.println("Please set the DEVCYCLE_SERVER_SDK_KEY environment variable");
System.exit(1);
}

// Create user object
DevCycleUser user = DevCycleUser.builder()
.userId("SOME_USER_ID")
.build();

// The default value can be of type string, boolean, number, or JSON
Boolean defaultValue = false;

DevCycleLocalOptions options = DevCycleLocalOptions.builder().configPollingIntervalMs(60000)
.disableAutomaticEventLogging(false).disableCustomEventLogging(false).build();

// Initialize DevCycle Client
DevCycleLocalClient client = new DevCycleLocalClient(server_sdk_key, options);

for (int i = 0; i < 10; i++) {
if(client.isInitialized()) {
break;
}
Thread.sleep(500);
}

// Fetch variable values using the identifier key, with a default value and user
// object
// The default value can be of type string, boolean, number, or JSON
Boolean variableValue = client.variableValue(user, VARIABLE_KEY, defaultValue);

// Use variable value
if (variableValue) {
System.err.println("feature is enabled");
} else {
System.err.println("feature is NOT enabled");
}
}
}