diff --git a/src/main/java/io/harness/cf/client/api/InnerClient.java b/src/main/java/io/harness/cf/client/api/InnerClient.java index f29a3bc1..8ad56fbd 100644 --- a/src/main/java/io/harness/cf/client/api/InnerClient.java +++ b/src/main/java/io/harness/cf/client/api/InnerClient.java @@ -1,12 +1,17 @@ package io.harness.cf.client.api; +import com.google.gson.Gson; import com.google.gson.JsonObject; import io.harness.cf.client.common.SdkCodes; import io.harness.cf.client.connector.*; import io.harness.cf.client.dto.Message; import io.harness.cf.client.dto.Target; import io.harness.cf.model.FeatureConfig; +import io.harness.cf.model.FeatureSnapshot; import io.harness.cf.model.Variation; +import java.util.LinkedList; +import java.util.List; +import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; @@ -307,6 +312,40 @@ public synchronized void waitForInitialization() } } + public List getFeatureSnapshots() { + // TODO return null/empty list if snapshot is not enabled in config. + return getFeatureSnapshots(""); + } + + public List getFeatureSnapshots(String prefix) { + // TODO return null/empty list if snapshot is not enabled in config. + List identifiers = repository.getAllFeatureIdentifiers(prefix); + List snapshots = new LinkedList<>(); + + for (String identifier : identifiers) { + FeatureSnapshot snapshot = getFeatureSnapshot(identifier); + snapshots.add(snapshot); + } + + return snapshots; + } + + public FeatureSnapshot getFeatureSnapshot(@NonNull String identifier) { + // TODO return null/empty list if snapshot is not enabled in config. + Optional ofc = repository.getCurrentAndPreviousFeatureConfig(identifier); + FeatureSnapshot result = new FeatureSnapshot(); + if (ofc.isPresent()) { + FeatureConfig[] fc = ofc.get(); + result.setPrevious(fc[0]); + result.setCurrent(fc[1]); + } + // this is here to create a deep copy of the object before its returned. + // this way we protect the cache. + Gson gson = new Gson(); + FeatureSnapshot deepCopySnapshot = gson.fromJson(gson.toJson(result), FeatureSnapshot.class); + return deepCopySnapshot; + } + public void on(@NonNull final Event event, @NonNull final Consumer consumer) { final CopyOnWriteArrayList> consumers = events.getOrDefault(event, new CopyOnWriteArrayList<>()); diff --git a/src/main/resources/client-v1.yaml b/src/main/resources/client-v1.yaml index f4643fe3..23c7661c 100644 --- a/src/main/resources/client-v1.yaml +++ b/src/main/resources/client-v1.yaml @@ -561,6 +561,13 @@ components: type: string required: - variation + FeatureSnapshot: + type: object + properties: + current: + $ref: '#/components/schemas/FeatureConfig' + previous: + $ref: '#/components/schemas/FeatureConfig' FeatureConfig: type: object properties: