Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/main/java/com/launchdarkly/client/LDClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class LDClient implements Closeable {
private final EventProcessor eventProcessor;
private final String apiKey;
protected static final String CLIENT_VERSION = getClientVersion();
private volatile boolean offline = false;


/**
Expand Down Expand Up @@ -117,6 +118,9 @@ public void sendEvent(String eventName, LDUser user, JsonObject data) {
* @param user the user that performed the event
*/
public void sendEvent(String eventName, LDUser user) {
if (this.offline) {
return;
}
sendEvent(eventName, user, null);
}

Expand All @@ -138,6 +142,10 @@ private void sendFlagRequestEvent(String featureKey, LDUser user, boolean value)
* @return whether or not the flag should be enabled, or {@code defaultValue} if the flag is disabled in the LaunchDarkly control panel
*/
public boolean getFlag(String featureKey, LDUser user, boolean defaultValue) {
if (this.offline) {
return defaultValue;
}

Gson gson = new Gson();
HttpCacheContext context = HttpCacheContext.create();
HttpGet request = config.getRequest(apiKey, "/api/eval/features/" + featureKey);
Expand Down Expand Up @@ -220,6 +228,32 @@ public void close() throws IOException {
}


/**
* Puts the LaunchDarkly client in offline mode.
* In offline mode, all calls to {@link #getFlag(String, LDUser, boolean)} will return the default value, and
* {@link #sendEvent(String, LDUser, com.google.gson.JsonObject)} will be a no-op.
*
*/
public void setOffline() {
this.offline = true;
}

/**
* Puts the LaunchDarkly client in online mode.
*
*/
public void setOnline() {
this.offline = false;
}

/**
*
* @return whether the client is in offline mode
*/
public boolean isOffline() {
return this.offline;
}

private static String getClientVersion() {
Class clazz = LDConfig.class;
String className = clazz.getSimpleName() + ".class";
Expand Down