Class GBFeaturesRepository

java.lang.Object
growthbook.sdk.java.repository.GBFeaturesRepository
All Implemented Interfaces:
IGBFeaturesRepository

public class GBFeaturesRepository extends Object implements IGBFeaturesRepository
This class can be created with its `builder()` or constructor. It will fetch the features from the endpoint provided. Initialize with initialize() Get the features JSON with getFeaturesJson(). You would provide the features JSON when creating the GBContext
  • Field Details

  • Constructor Details

    • GBFeaturesRepository

      @Deprecated public GBFeaturesRepository(@Nullable String apiHost, String clientKey, @Deprecated @Nullable String encryptionKey, @Nullable FeatureRefreshStrategy refreshStrategy, @Nullable Integer swrTtlSeconds)
      Deprecated.
      Create a new GBFeaturesRepository
      Parameters:
      apiHost - The GrowthBook API host (default: ...)
      clientKey - Your client ID, e.g. sdk-abc123
      encryptionKey - optional key for decrypting encrypted payload
      swrTtlSeconds - How often the cache should be invalidated when using FeatureRefreshStrategy.STALE_WHILE_REVALIDATE (default: 60)
    • GBFeaturesRepository

      public GBFeaturesRepository(@Nullable String apiHost, String clientKey, @Deprecated @Nullable String encryptionKey, @Nullable FeatureRefreshStrategy refreshStrategy, @Nullable Integer swrTtlSeconds, @Nullable RequestBodyForRemoteEval requestBodyForRemoteEval)
      New constructor that support payload for remote eval
      Parameters:
      apiHost - The GrowthBook API host (default: ...)
      clientKey - Your client ID, e.g. sdk-abc123
      encryptionKey - optional key for decrypting encrypted payload
      refreshStrategy - Strategy for building url
      swrTtlSeconds - How often the cache should be invalidated when using FeatureRefreshStrategy.STALE_WHILE_REVALIDATE (default: 60)
      requestBodyForRemoteEval - Payload that would be sent with POST request when repository configure with Remote evalStrategy FeatureRefreshStrategy.REMOTE_EVAL_STRATEGY (default: 60)
    • GBFeaturesRepository

      public GBFeaturesRepository(@Nullable String apiHost, String clientKey, @Deprecated @Nullable String encryptionKey, @Nullable FeatureRefreshStrategy refreshStrategy, @Nullable Integer swrTtlSeconds, @Nullable Boolean isCacheDisabled)
    • GBFeaturesRepository

      public GBFeaturesRepository(@Nullable String apiHost, String clientKey, @Deprecated @Nullable String encryptionKey, @Nullable FeatureRefreshStrategy refreshStrategy, @Nullable Integer swrTtlSeconds, @Nullable okhttp3.OkHttpClient okHttpClient, @Nullable String decryptionKey, @Nullable Boolean isCacheDisabled, @Nullable RequestBodyForRemoteEval requestBodyForRemoteEval)
      New constructor that explicitly supports decryptionKey.
    • GBFeaturesRepository

      public GBFeaturesRepository(@Nullable String apiHost, String clientKey, @Nullable String decryptionKey, @Nullable FeatureRefreshStrategy refreshStrategy, @Nullable Integer swrTtlSeconds, @Nullable okhttp3.OkHttpClient okHttpClient, @Nullable Boolean isCacheDisabled, @Nullable RequestBodyForRemoteEval requestBodyForRemoteEval)
      Create a new GBFeaturesRepository
      Parameters:
      apiHost - The GrowthBook API host (default: ...)
      clientKey - Your client ID, e.g. sdk-abc123
      decryptionKey - optional key for decrypting encrypted payload
      swrTtlSeconds - How often the cache should be invalidated when using FeatureRefreshStrategy.STALE_WHILE_REVALIDATE (default: 60)
      okHttpClient - HTTP client (optional)
      isCacheDisabled - Parameter to disable or enable caching in project
      requestBodyForRemoteEval - Payload that would be sent with POST request when repository configure with Remote evalStrategy FeatureRefreshStrategy.REMOTE_EVAL_STRATEGY
  • Method Details

    • setCachingManager

      public void setCachingManager(CachingManager cachingManager)
    • getEncryptionKey

      @Deprecated @Nullable public String getEncryptionKey()
      Deprecated.
    • getFeaturesJson

      public String getFeaturesJson()
      Description copied from interface: IGBFeaturesRepository
      Required implementation to get the featuresJson
      Specified by:
      getFeaturesJson in interface IGBFeaturesRepository
      Returns:
      feature data JSON in a type of String. Handle refresh strategy
    • onFeaturesRefresh

      public void onFeaturesRefresh(FeatureRefreshCallback callback)
      Subscribe to feature refresh events This callback is called when the features are successfully refreshed or there is an error when refreshing. This is called even if the features have not changed.
      Specified by:
      onFeaturesRefresh in interface IGBFeaturesRepository
      Parameters:
      callback - This callback will be called when features are refreshed
    • clearCallbacks

      public void clearCallbacks()
      Description copied from interface: IGBFeaturesRepository
      Clears the feature refresh callbacks
      Specified by:
      clearCallbacks in interface IGBFeaturesRepository
    • initialize

      public void initialize() throws FeatureFetchException
      Specified by:
      initialize in interface IGBFeaturesRepository
      Throws:
      FeatureFetchException
    • initialize

      public void initialize(Boolean retryOnFailure) throws FeatureFetchException
      Specified by:
      initialize in interface IGBFeaturesRepository
      Throws:
      FeatureFetchException
    • fetchFeatures

      public void fetchFeatures() throws FeatureFetchException
      Performs a network request to fetch the features from the GrowthBook API with the provided endpoint. If an encryptionKey is provided, it is assumed the features endpoint is using encrypted features. This method will attempt to decrypt the encrypted features with the provided encryptionKey.
      Throws:
      FeatureFetchException
    • shutdown

      public void shutdown()
    • fetchForRemoteEval

      public void fetchForRemoteEval(RequestBodyForRemoteEval requestBodyForRemoteEval) throws FeatureFetchException
      Throws:
      FeatureFetchException
    • builder

    • getFeaturesEndpoint

      public String getFeaturesEndpoint()
      Endpoint for GET request
    • getEventsEndpoint

      public String getEventsEndpoint()
      Endpoint for SSE request
    • getRefreshStrategy

      public FeatureRefreshStrategy getRefreshStrategy()
      Strategy for building url
    • getDecryptionKey

      @Nullable public String getDecryptionKey()
      The key used to decrypt encrypted features from the API
    • getSwrTtlSeconds

      public Integer getSwrTtlSeconds()
      The standard cache TTL to use (60 seconds)
    • getExpiresAt

      public Long getExpiresAt()
      Seconds after that cache is expired
    • getInitialized

      public Boolean getInitialized()
      Flag to know whether GBFeatureRepository is initialized
    • getSavedGroupsJson

      @Nullable public String getSavedGroupsJson()
      Allows you to get the saved groups JSON from the provided getFeaturesEndpoint(). You must call initialize() before calling this method or your saved groups would not have loaded.
    • getCachingManager

      public CachingManager getCachingManager()
      CachingManger allows to cache features data to file
    • getRequestBodyForRemoteEval

      @Nullable public RequestBodyForRemoteEval getRequestBodyForRemoteEval()
      Request body for that be sent with POST request for remote eval feature
    • getRemoteEvalEndPoint

      public String getRemoteEvalEndPoint()
      Endpoint for POST request