Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from launchdarkly/jko/offline-mode
Browse files Browse the repository at this point in the history
Add an offline mode that never talks to a remote server
  • Loading branch information
jkodumal committed Jan 24, 2015
2 parents 74a245b + 802eceb commit 4e4521d
Showing 1 changed file with 34 additions and 0 deletions.
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

0 comments on commit 4e4521d

Please sign in to comment.