From 99b033ca9842a10cd95f940d5dbecb38e33515f3 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Mon, 17 Jun 2024 16:39:32 +0200 Subject: [PATCH] feat: Confidence builder in Provider for edits --- Sources/Confidence/Confidence.swift | 8 +++++++- .../ConfidenceFeatureProvider.swift | 13 +++++++++---- .../ConfidenceProviderTest.swift | 14 ++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index 09cc2599..7dc44b9a 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -260,6 +260,7 @@ extension Confidence { internal var storage: Storage? internal let eventStorage: EventStorage internal var flagResolver: ConfidenceResolveClient? + internal var metadata: ConfidenceMetadata? var region: ConfidenceRegion = .global var visitorId = VisitorUtil().getId() @@ -298,6 +299,11 @@ extension Confidence { return self } + public func withMetadata(metadata: ConfidenceMetadata) -> Builder { + self.metadata = metadata + return self + } + /** Sets the region for the network request to the Confidence backend. The default is `global` and the requests are automatically routed to the closest server. @@ -311,7 +317,7 @@ extension Confidence { let options = ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret), region: region) - let metadata = ConfidenceMetadata( + let metadata = metadata ?? ConfidenceMetadata( name: "SDK_ID_SWIFT_CONFIDENCE", version: "0.1.4") // x-release-please-version let uploader = RemoteConfidenceClient( diff --git a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift index b611c7f1..ff252458 100644 --- a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift +++ b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift @@ -21,12 +21,15 @@ public class ConfidenceFeatureProvider: FeatureProvider { private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue") /// Initialize the Provider via a `Confidence` object. - public convenience init(confidence: Confidence, initializationStrategy: InitializationStrategy = .fetchAndActivate) { - self.init(confidence: confidence, session: nil) + public convenience init( + confidenceBuilder: Confidence.Builder, + initializationStrategy: InitializationStrategy = .fetchAndActivate + ) { + self.init(confidenceBuilder: confidenceBuilder, session: nil) } internal init( - confidence: Confidence, + confidenceBuilder: Confidence.Builder, initializationStrategy: InitializationStrategy = .fetchAndActivate, session: URLSession? ) { @@ -35,7 +38,9 @@ public class ConfidenceFeatureProvider: FeatureProvider { version: "0.2.1") // x-release-please-version self.metadata = Metadata(name: metadata.name) self.initializationStrategy = initializationStrategy - self.confidence = confidence + self.confidence = confidenceBuilder + .withMetadata(metadata: metadata) + .build() } public func initialize(initialContext: OpenFeature.EvaluationContext?) { diff --git a/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift b/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift index edf4945a..59895a70 100644 --- a/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift +++ b/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift @@ -18,12 +18,13 @@ class ConfidenceProviderTest: XCTestCase { } let client = FakeClient() - let confidence = Confidence.Builder(clientSecret: "test") + let confidenceBuilder = Confidence.Builder(clientSecret: "test") .withContext(initialContext: ["targeting_key": .init(string: "user1")]) .withFlagResolverClient(flagResolver: client) - .build() - let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync) + let provider = ConfidenceFeatureProvider( + confidenceBuilder: confidenceBuilder, + initializationStrategy: .activateAndFetchAsync) OpenFeatureAPI.shared.setProvider(provider: provider) let cancellable = OpenFeatureAPI.shared.observe().sink { event in @@ -56,12 +57,13 @@ class ConfidenceProviderTest: XCTestCase { } } - let confidence = Confidence.Builder(clientSecret: "test") + let confidenceBuilder = Confidence.Builder(clientSecret: "test") .withContext(initialContext: ["targeting_key": .init(string: "user1")]) .withStorage(storage: FakeStorage()) - .build() - let provider = ConfidenceFeatureProvider(confidence: confidence, initializationStrategy: .activateAndFetchAsync) + let provider = ConfidenceFeatureProvider( + confidenceBuilder: confidenceBuilder, + initializationStrategy: .activateAndFetchAsync) OpenFeatureAPI.shared.setProvider(provider: provider) let cancellable = OpenFeatureAPI.shared.observe().sink { event in