diff --git a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift index 309fef04..e84a5d2b 100644 --- a/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift +++ b/ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift @@ -22,17 +22,15 @@ extension ConfidenceDemoApp { } // If we have no cache, then do a fetch first. - var initializationStratgey: InitializationStrategy = .activateAndFetchAsync + var initializationStrategy: InitializationStrategy = .activateAndFetchAsync if ConfidenceFeatureProvider.isStorageEmpty() { - initializationStratgey = .fetchAndActivate + initializationStrategy = .fetchAndActivate } - // TODO: Remove Builder pattern let confidence = Confidence.Builder(clientSecret: secret) - .withOptions(options: ConfidenceClientOptions(initializationStrategy: initializationStratgey)) + .withInitializationstrategy(initializationStrategy: initializationStrategy) .build() - // TODO: Remove Builder pattern - let provider = ConfidenceFeatureProvider.Builder(confidence: confidence).build() + let provider = ConfidenceFeatureProvider(confidence: confidence) // NOTE: Using a random UUID for each app start is not advised and can result in getting stale values. let ctx = MutableContext(targetingKey: UUID.init().uuidString, structure: MutableStructure()) diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index ccb13d1a..174cc314 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -7,10 +7,12 @@ public class Confidence: ConfidenceEventSender { public var region: ConfidenceRegion public var initializationStrategy: InitializationStrategy - init(clientSecret: String, - timeout: TimeInterval, - region: ConfidenceRegion, - initializationStrategy: InitializationStrategy) { + init( + clientSecret: String, + timeout: TimeInterval, + region: ConfidenceRegion, + initializationStrategy: InitializationStrategy + ) { self.context = [:] self.clientSecret = clientSecret self.timeout = timeout @@ -88,5 +90,3 @@ public enum ConfidenceRegion { case europe case usa } - - diff --git a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift index 4101e556..97bc8371 100644 --- a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift +++ b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift @@ -24,7 +24,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { private let eventHandler = EventHandler(ProviderEvent.notReady) private let confidence: Confidence? - /// Should not be called externally, use `ConfidenceFeatureProvider.Builder` instead. + /// Should not be called externally, use `ConfidenceFeatureProvider.Builder`or init with `Confidence` instead. init( metadata: ProviderMetadata, client: RemoteConfidenceClient, @@ -32,7 +32,6 @@ public class ConfidenceFeatureProvider: FeatureProvider { storage: Storage, overrides: [String: LocalOverride] = [:], flagApplier: FlagApplier, - applyStorage: Storage, initializationStrategy: InitializationStrategy, confidence: Confidence? ) { @@ -44,8 +43,33 @@ public class ConfidenceFeatureProvider: FeatureProvider { self.initializationStrategy = initializationStrategy self.storage = storage self.confidence = confidence + self.resolver = LocalStorageResolver(cache: cache) + } - resolver = LocalStorageResolver(cache: cache) + /// Initialize the Provider via a Confidence object. + public init(confidence: Confidence) { + let metadata = ConfidenceMetadata(version: "0.1.4") // x-release-please-version + let options = ConfidenceClientOptions( + credentials: ConfidenceClientCredentials.clientSecret(secret: confidence.clientSecret), + timeout: confidence.timeout, + region: confidence.region) + self.metadata = metadata + self.cache = InMemoryProviderCache.from(storage: DefaultStorage.resolverFlagsCache()) + self.storage = DefaultStorage.resolverFlagsCache() + self.resolver = LocalStorageResolver(cache: cache) + self.flagApplier = FlagApplierWithRetries( + httpClient: NetworkClient(region: options.region), + storage: DefaultStorage.applierFlagsCache(), + options: options, + metadata: metadata) + self.client = RemoteConfidenceClient( + options: options, + applyOnResolve: false, + flagApplier: flagApplier, + metadata: metadata) + self.initializationStrategy = confidence.initializationStrategy + self.overrides = [:] + self.confidence = confidence } public func initialize(initialContext: OpenFeature.EvaluationContext?) { @@ -125,7 +149,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws - -> OpenFeature.ProviderEvaluation + -> OpenFeature.ProviderEvaluation { return try errorWrappedResolveFlag( flag: key, @@ -135,7 +159,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws - -> OpenFeature.ProviderEvaluation + -> OpenFeature.ProviderEvaluation { return try errorWrappedResolveFlag( flag: key, @@ -145,7 +169,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws - -> OpenFeature.ProviderEvaluation + -> OpenFeature.ProviderEvaluation { return try errorWrappedResolveFlag( flag: key, @@ -155,7 +179,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws - -> OpenFeature.ProviderEvaluation + -> OpenFeature.ProviderEvaluation { return try errorWrappedResolveFlag( flag: key, @@ -165,7 +189,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func getObjectEvaluation(key: String, defaultValue: OpenFeature.Value, context: EvaluationContext?) - throws -> OpenFeature.ProviderEvaluation + throws -> OpenFeature.ProviderEvaluation { return try errorWrappedResolveFlag( flag: key, @@ -204,7 +228,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } public func errorWrappedResolveFlag(flag: String, defaultValue: T, ctx: EvaluationContext?, errorPrefix: String) - throws -> ProviderEvaluation + throws -> ProviderEvaluation { do { let path = try FlagPath.getPath(for: flag) @@ -272,7 +296,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { } private func resolveFlagNoValue(defaultValue: T, resolverResult: ResolveResult, ctx: EvaluationContext) - -> ProviderEvaluation + -> ProviderEvaluation { switch resolverResult.resolvedValue.resolveReason { case .noMatch: @@ -419,7 +443,6 @@ extension ConfidenceFeatureProvider { var cache: ProviderCache? var flagApplier: (any FlagApplier)? var initializationStrategy: InitializationStrategy = .fetchAndActivate - var applyStorage: Storage = DefaultStorage.resolverApplyCache() var confidence: Confidence? /// DEPRECATED @@ -433,7 +456,7 @@ extension ConfidenceFeatureProvider { } /// TODO - public init(confidence: Confidence) { + init(confidence: Confidence) { self.options = ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: confidence.clientSecret), timeout: confidence.timeout, @@ -449,8 +472,7 @@ extension ConfidenceFeatureProvider { flagApplier: FlagApplier?, storage: Storage, cache: ProviderCache?, - initializationStrategy: InitializationStrategy, - applyStorage: Storage + initializationStrategy: InitializationStrategy ) { self.options = options self.session = session @@ -459,7 +481,6 @@ extension ConfidenceFeatureProvider { self.storage = storage self.cache = cache self.initializationStrategy = initializationStrategy - self.applyStorage = applyStorage } /// Allows the `ConfidenceClient` to be configured with a custom URLSession, useful for @@ -475,8 +496,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -492,8 +512,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -509,8 +528,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -526,8 +544,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -543,8 +560,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -560,8 +576,7 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } @@ -594,21 +609,20 @@ extension ConfidenceFeatureProvider { flagApplier: flagApplier, storage: storage, cache: cache, - initializationStrategy: initializationStrategy, - applyStorage: applyStorage + initializationStrategy: initializationStrategy ) } /// Creates the `ConfidenceFeatureProvider` according to the settings specified in the builder. public func build() -> ConfidenceFeatureProvider { let flagApplier = - flagApplier - ?? FlagApplierWithRetries( - httpClient: NetworkClient(region: options.region), - storage: DefaultStorage.applierFlagsCache(), - options: options, - metadata: metadata - ) + flagApplier + ?? FlagApplierWithRetries( + httpClient: NetworkClient(region: options.region), + storage: DefaultStorage.applierFlagsCache(), + options: options, + metadata: metadata + ) let cache = cache ?? InMemoryProviderCache.from(storage: storage) @@ -627,7 +641,6 @@ extension ConfidenceFeatureProvider { storage: storage, overrides: localOverrides, flagApplier: flagApplier, - applyStorage: applyStorage, initializationStrategy: initializationStrategy, confidence: confidence )