Class FBClientImp

java.lang.Object
co.featbit.server.FBClientImp
All Implemented Interfaces:
FBClient, Closeable, AutoCloseable

public final class FBClientImp extends Object implements FBClient
A client for the Featbit API. The client is thread-safe.
  • Constructor Details

    • FBClientImp

      public FBClientImp(String envSecret, FBConfig config)
      Creates a new client to connect to feature flag center with a specified configuration.

      This constructor can be used to configure advanced SDK features; see FBConfig.Builder.

      Applications SHOULD instantiate a single instance for the lifetime of the application. In the case where an application needs to evaluate feature flags from different environments, you may create multiple clients, but they should still be retained for the lifetime of the application rather than created per request or per thread.

      Note that unless client is configured in offline modeFBConfig.Builder.offline(boolean) or set by Factory.externalDataSynchronization(), this client try to connect to feature flag center as soon as the constructor is called. The constructor will return when it successfully connects, or when the timeout set by FBConfig.Builder.startWaitTime(Duration) (default: 15 seconds) expires, whichever comes first. If it has not succeeded in connecting when the timeout elapses, you will receive the client in an uninitialized state where feature flags will return default values; it will still continue trying to connect in the background unless there has been an ProtocolException or you close the clientclose(). You can detect whether initialization has succeeded by calling isInitialized().

      If you prefer to have the constructor return immediately, and then wait for initialization to finish at some other point, you can use getDataUpdateStatusProvider() as follows:

      
           FBConfig config = new FBConfig.Builder()
               .startWait(Duration.ZERO)
               .streamingURI("your streaming URI")
               .eventURI("your event URI")
               .build();
           FBClient client = new FBClientImp(sdkKey, config);
      
           // later, when you want to wait for initialization to finish:
           boolean inited = client.getDataUpdateStatusProvider().waitForOKState(Duration.ofSeconds(15))
           if (!inited) {
               // do whatever is appropriate if initialization has timed out
           }
       

      This constructor can throw unchecked exceptions if it is immediately apparent that the SDK cannot work with these parameters. In fact, if the env secret is not valid, it will throw an IllegalArgumentException a null value for a non-nullable parameter may throw a NullPointerException. The constructor will not throw any exception that could only be detected after making a request to our API

      Parameters:
      envSecret - the secret key for your own environment
      config - a client configuration object FBConfig
      Throws:
      NullPointerException - if a non-nullable parameter was null
      IllegalArgumentException - if envSecret is invalid
  • Method Details

    • isInitialized

      public boolean isInitialized()
      Description copied from interface: FBClient
      Tests whether the client is ready to be used.
      Specified by:
      isInitialized in interface FBClient
      Returns:
      true if the client is ready, or false if it is still initializing
    • variation

      public String variation(String featureFlagKey, FBUser user, String defaultValue)
      Description copied from interface: FBClient
      Calculates the value of a feature flag for a given user.

      Specified by:
      variation in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      the variation for the given user, or defaultValue if the flag is disabled or an error occurs
    • variationDetail

      public FlagState<String> variationDetail(String featureFlagKey, FBUser user, String defaultValue)
      Description copied from interface: FBClient
      Calculates the value of a feature flag for a given user, and returns an object that describes the way the value was determined.

      Specified by:
      variationDetail in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • boolVariation

      public boolean boolVariation(String featureFlagKey, FBUser user, Boolean defaultValue)
      Description copied from interface: FBClient
      Calculates the boolean value of a feature flag for a given user.

      Specified by:
      boolVariation in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      if the flag should be enabled, or defaultValue if the flag is disabled or an error occurs
    • isEnabled

      public boolean isEnabled(String featureFlagKey, FBUser user)
      Description copied from interface: FBClient
      alias of boolVariation for a given user
      Specified by:
      isEnabled in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      Returns:
      if the flag should be enabled, or false if the flag is disabled, or an error occurs
    • boolVariationDetail

      public FlagState<Boolean> boolVariationDetail(String featureFlagKey, FBUser user, Boolean defaultValue)
      Description copied from interface: FBClient
      Calculates the value of a feature flag for a given user, and returns an object that describes the way the value was determined.

      Specified by:
      boolVariationDetail in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • doubleVariation

      public double doubleVariation(String featureFlagKey, FBUser user, Double defaultValue)
      Description copied from interface: FBClient
      Calculates the double value of a feature flag for a given user.

      Specified by:
      doubleVariation in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      the variation for the given user, or defaultValue if the flag is disabled or an error occurs
    • doubleVariationDetail

      public FlagState<Double> doubleVariationDetail(String featureFlagKey, FBUser user, Double defaultValue)
      Description copied from interface: FBClient
      Calculates the double value of a feature flag for a given user, and returns an object that describes the way the value was determined.

      Specified by:
      doubleVariationDetail in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • intVariation

      public int intVariation(String featureFlagKey, FBUser user, Integer defaultValue)
      Description copied from interface: FBClient
      Calculates the integer value of a feature flag for a given user. Note that If the variation has a numeric value, but not an integer, it is rounded toward zero(DOWN mode)

      Specified by:
      intVariation in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      the variation for the given user, or defaultValue if the flag is disabled or an error occurs
    • intVariationDetail

      public FlagState<Integer> intVariationDetail(String featureFlagKey, FBUser user, Integer defaultValue)
      Description copied from interface: FBClient
      Calculates the int value of a feature flag for a given user, and returns an object that describes the way the value was determined.

      Note that If the variation has a numeric value, but not an int value, it is rounded toward zero(DOWN mode)

      Specified by:
      intVariationDetail in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • longVariation

      public long longVariation(String featureFlagKey, FBUser user, Long defaultValue)
      Description copied from interface: FBClient
      Calculates the long value of a feature flag for a given user. Note that If the variation has a numeric value, but not a long value, it is rounded toward zero(DOWN mode)

      Specified by:
      longVariation in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      the variation for the given user, or defaultValue if the flag is disabled or an error occurs
    • longVariationDetail

      public FlagState<Long> longVariationDetail(String featureFlagKey, FBUser user, Long defaultValue)
      Description copied from interface: FBClient
      Calculates the long value of a feature flag for a given user, and returns an object that describes the way the value was determined.

      Note that If the variation has a numeric value, but not a long value, it is rounded toward zero(DOWN mode)

      Specified by:
      longVariationDetail in interface FBClient
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • jsonVariation

      public <T> T jsonVariation(String featureFlagKey, FBUser user, Class<T> clazz, T defaultValue)
      Description copied from interface: FBClient
      Calculates the json value of a feature flag for a given user.
      Specified by:
      jsonVariation in interface FBClient
      Type Parameters:
      T - json object type
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      clazz - json deserialization class
      defaultValue - the default value of the flag
      Returns:
      the variation for the given user, or defaultValue if the flag is disabled, current user doesn't exist
    • jsonVariationDetail

      public <T> FlagState<T> jsonVariationDetail(String featureFlagKey, FBUser user, Class<T> clazz, T defaultValue)
      Description copied from interface: FBClient
      Calculates the json value of a feature flag for a given user, and returns an object that describes the way the value was determined.
      Specified by:
      jsonVariationDetail in interface FBClient
      Type Parameters:
      T - json object type
      Parameters:
      featureFlagKey - the unique key for the feature flag
      user - the end user requesting the flag
      clazz - json deserialization class
      defaultValue - the default value of the flag
      Returns:
      an FlagState object
    • isFlagKnown

      public boolean isFlagKnown(String featureKey)
      Description copied from interface: FBClient
      Returns true if the specified feature flag currently exists.
      Specified by:
      isFlagKnown in interface FBClient
      Parameters:
      featureKey - the unique key for the feature flag
      Returns:
      true if the flag exists
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • isOffline

      public boolean isOffline()
    • getDataUpdateStatusProvider

      public Status.DataUpdateStatusProvider getDataUpdateStatusProvider()
      Description copied from interface: FBClient
      Returns an interface for tracking the status of the update processor.

      The update processor is the mechanism that the SDK uses to get feature flag, such as a streaming connection. The Status.DataUpdateStatusProvider is used to check whether the update processor is currently operational

      Specified by:
      getDataUpdateStatusProvider in interface FBClient
      Returns:
      a Status.DataUpdateStatusProvider
    • initializeFromExternalJson

      public boolean initializeFromExternalJson(String json)
      Description copied from interface: FBClient
      initialization in the offline mode

      Specified by:
      initializeFromExternalJson in interface FBClient
      Parameters:
      json - feature flags in the json format
      Returns:
      true if the initialization is well done
    • getAllLatestFlagsVariations

      public AllFlagStates<String> getAllLatestFlagsVariations(FBUser user)
      Description copied from interface: FBClient
      Returns a list of all feature flags value with details for a given user, including the reason that describes the way the value was determined, that can be used on the client side sdk or a front end .

      note that this method does not send insight events back to feature flag center.

      Specified by:
      getAllLatestFlagsVariations in interface FBClient
      Parameters:
      user - the end user requesting the flag
      Returns:
      a AllFlagStates
    • flush

      public void flush()
      Description copied from interface: FBClient
      Flushes all pending events.
      Specified by:
      flush in interface FBClient
    • identify

      public void identify(FBUser user)
      Description copied from interface: FBClient
      register a user
      Specified by:
      identify in interface FBClient
      Parameters:
      user - user to register
    • trackMetric

      public void trackMetric(FBUser user, String eventName)
      Description copied from interface: FBClient
      tracks that a user performed an event and provides a default numeric value for custom metrics
      Specified by:
      trackMetric in interface FBClient
      Parameters:
      user - the user that performed the event
      eventName - the name of the event
    • trackMetric

      public void trackMetric(FBUser user, String eventName, double metricValue)
      Description copied from interface: FBClient
      tracks that a user performed an event, and provides an additional numeric value for custom metrics.
      Specified by:
      trackMetric in interface FBClient
      Parameters:
      user - the user that performed the event
      eventName - the name of the event
      metricValue - a numeric value used by the experimentation feature in numeric custom metrics.
    • trackMetrics

      public void trackMetrics(FBUser user, String... eventNames)
      Description copied from interface: FBClient
      tracks that a user performed a series of events with default numeric value for custom metrics
      Specified by:
      trackMetrics in interface FBClient
      Parameters:
      user - the user that performed the event
      eventNames - event names
    • trackMetrics

      public void trackMetrics(FBUser user, Map<String,Double> metrics)
      Description copied from interface: FBClient
      tracks that a user performed a series of events
      Specified by:
      trackMetrics in interface FBClient
      Parameters:
      user - the user that performed the event
      metrics - event name and numeric value in K/V