Skip to content

Commit

Permalink
More Builder and constructor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Mar 28, 2024
1 parent d0dfe59 commit 1829a79
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 49 deletions.
10 changes: 4 additions & 6 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 6 additions & 6 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -88,5 +90,3 @@ public enum ConfidenceRegion {
case europe
case usa
}


87 changes: 50 additions & 37 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ 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,
cache: ProviderCache,
storage: Storage,
overrides: [String: LocalOverride] = [:],
flagApplier: FlagApplier,
applyStorage: Storage,
initializationStrategy: InitializationStrategy,
confidence: Confidence?
) {
Expand All @@ -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?) {
Expand Down Expand Up @@ -125,7 +149,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws
-> OpenFeature.ProviderEvaluation<Bool>
-> OpenFeature.ProviderEvaluation<Bool>
{
return try errorWrappedResolveFlag(
flag: key,
Expand All @@ -135,7 +159,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func getStringEvaluation(key: String, defaultValue: String, context: EvaluationContext?) throws
-> OpenFeature.ProviderEvaluation<String>
-> OpenFeature.ProviderEvaluation<String>
{
return try errorWrappedResolveFlag(
flag: key,
Expand All @@ -145,7 +169,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func getIntegerEvaluation(key: String, defaultValue: Int64, context: EvaluationContext?) throws
-> OpenFeature.ProviderEvaluation<Int64>
-> OpenFeature.ProviderEvaluation<Int64>
{
return try errorWrappedResolveFlag(
flag: key,
Expand All @@ -155,7 +179,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func getDoubleEvaluation(key: String, defaultValue: Double, context: EvaluationContext?) throws
-> OpenFeature.ProviderEvaluation<Double>
-> OpenFeature.ProviderEvaluation<Double>
{
return try errorWrappedResolveFlag(
flag: key,
Expand All @@ -165,7 +189,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func getObjectEvaluation(key: String, defaultValue: OpenFeature.Value, context: EvaluationContext?)
throws -> OpenFeature.ProviderEvaluation<OpenFeature.Value>
throws -> OpenFeature.ProviderEvaluation<OpenFeature.Value>
{
return try errorWrappedResolveFlag(
flag: key,
Expand Down Expand Up @@ -204,7 +228,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

public func errorWrappedResolveFlag<T>(flag: String, defaultValue: T, ctx: EvaluationContext?, errorPrefix: String)
throws -> ProviderEvaluation<T>
throws -> ProviderEvaluation<T>
{
do {
let path = try FlagPath.getPath(for: flag)
Expand Down Expand Up @@ -272,7 +296,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

private func resolveFlagNoValue<T>(defaultValue: T, resolverResult: ResolveResult, ctx: EvaluationContext)
-> ProviderEvaluation<T>
-> ProviderEvaluation<T>
{
switch resolverResult.resolvedValue.resolveReason {
case .noMatch:
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -449,8 +472,7 @@ extension ConfidenceFeatureProvider {
flagApplier: FlagApplier?,
storage: Storage,
cache: ProviderCache?,
initializationStrategy: InitializationStrategy,
applyStorage: Storage
initializationStrategy: InitializationStrategy
) {
self.options = options
self.session = session
Expand All @@ -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
Expand All @@ -475,8 +496,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand All @@ -492,8 +512,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand All @@ -509,8 +528,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand All @@ -526,8 +544,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand All @@ -543,8 +560,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand All @@ -560,8 +576,7 @@ extension ConfidenceFeatureProvider {
flagApplier: flagApplier,
storage: storage,
cache: cache,
initializationStrategy: initializationStrategy,
applyStorage: applyStorage
initializationStrategy: initializationStrategy
)
}

Expand Down Expand Up @@ -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)

Expand All @@ -627,7 +641,6 @@ extension ConfidenceFeatureProvider {
storage: storage,
overrides: localOverrides,
flagApplier: flagApplier,
applyStorage: applyStorage,
initializationStrategy: initializationStrategy,
confidence: confidence
)
Expand Down

0 comments on commit 1829a79

Please sign in to comment.