From 643a49ab969a87cda197f90ae34d63c81d074054 Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Tue, 25 Jun 2024 11:44:04 +0200 Subject: [PATCH] refactor: Revert "feat: CreateConfidence from Provider for Metadata (#141)" (#142) * Revert "feat: CreateConfidence from Provider for Metadata (#141)" This reverts commit bf368df1fba8b545b22f16a901d1ebd7b5d6fde9. * refactor: minor polishing * refacor: Remove unused metadata from Provider --- Sources/Confidence/Confidence.swift | 19 ++------ .../RemoteResolveConfidenceClient.swift | 5 +- .../ConfidenceFeatureProvider.swift | 48 +++---------------- .../ConfidenceProviderTest.swift | 20 +------- Tests/ConfidenceTests/ConfidenceTest.swift | 7 --- 5 files changed, 14 insertions(+), 85 deletions(-) diff --git a/Sources/Confidence/Confidence.swift b/Sources/Confidence/Confidence.swift index 04220a86..f0057a38 100644 --- a/Sources/Confidence/Confidence.swift +++ b/Sources/Confidence/Confidence.swift @@ -244,6 +244,9 @@ public class Confidence: ConfidenceEventSender { } } + /** + Sets the initial Context. + */ public func withContext(_ context: ConfidenceStruct) -> Self { return Self.init( clientSecret: clientSecret, @@ -302,9 +305,6 @@ extension Confidence { return self } - /** - Sets the initial Context. - */ public func withContext(initialContext: ConfidenceStruct) -> Builder { self.initialContext = initialContext return self @@ -319,21 +319,12 @@ extension Confidence { return self } - /** - Overrides the Metadata for the Confidence instance. In normal production scenarios, Metadata is - handled automatically by the SDK and this overrides should not be applied. - */ - public func withMetadata(metadata: ConfidenceMetadata) -> Builder { - self.metadata = metadata - return self - } - public func build() -> Confidence { let options = ConfidenceClientOptions( credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret), region: region) - let metadata = metadata ?? ConfidenceMetadata( - name: Confidence.sdkId, + let metadata = ConfidenceMetadata( + name: sdkId, version: "0.2.2") // x-release-please-version let uploader = RemoteConfidenceClient( options: options, diff --git a/Sources/Confidence/RemoteResolveConfidenceClient.swift b/Sources/Confidence/RemoteResolveConfidenceClient.swift index 8c725771..7521f75a 100644 --- a/Sources/Confidence/RemoteResolveConfidenceClient.swift +++ b/Sources/Confidence/RemoteResolveConfidenceClient.swift @@ -3,12 +3,11 @@ import Foundation public class RemoteConfidenceResolveClient: ConfidenceResolveClient { private let targetingKey = "targeting_key" private var options: ConfidenceClientOptions + private let metadata: ConfidenceMetadata + private var httpClient: HttpClient private var applyOnResolve: Bool - // Internal for testing - internal let metadata: ConfidenceMetadata - init( options: ConfidenceClientOptions, session: URLSession? = nil, diff --git a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift index 2f1618e8..c073864a 100644 --- a/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift +++ b/Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift @@ -5,64 +5,27 @@ import OpenFeature import os struct Metadata: ProviderMetadata { - var name: String? = ConfidenceFeatureProvider.providerId + var name: String? } /// The implementation of the Confidence Feature Provider. This implementation allows to pre-cache evaluations. public class ConfidenceFeatureProvider: FeatureProvider { public static let providerId: String = "SDK_ID_SWIFT_PROVIDER" - - public var metadata: ProviderMetadata = Metadata() + public var metadata: ProviderMetadata public var hooks: [any Hook] = [] private let lock = UnfairLock() private let initializationStrategy: InitializationStrategy private let eventHandler = EventHandler(ProviderEvent.notReady) private let confidence: Confidence + private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue") private var cancellables = Set() private var currentResolveTask: Task? - private let confidenceFeatureProviderQueue = DispatchQueue(label: "com.provider.queue") - - /** - Creates the `Confidence` object to be used as init parameter for this Provider. - */ - public static func createConfidence(clientSecret: String) -> ConfidenceForOpenFeature { - return ConfidenceForOpenFeature.init(confidence: Confidence.Builder.init(clientSecret: clientSecret) - .withRegion(region: .global) - .withMetadata(metadata: ConfidenceMetadata.init( - name: providerId, - version: "0.2.2") // x-release-please-version - ) - .build()) - } - - /** - Proxy holder to ensure correct Confidence configuration passed into the Provider's init. - Do not instantiate directly. - */ - public class ConfidenceForOpenFeature { - internal init(confidence: Confidence) { - self.confidence = confidence - } - let confidence: Confidence - } /** Initialize the Provider via a `Confidence` object. - The `Confidence` object must be creted via the `createConfidence` function available from this same class, - rather then be instantiated directly via `Confidence.init(...)` as you would if not using the OpenFeature integration. + The `initializationStrategy` defines when the Provider is ready to read flags, before or after a refresh of the flag evaluation fata. */ - public convenience init( - confidenceForOF: ConfidenceForOpenFeature, - initializationStrategy: InitializationStrategy = .fetchAndActivate - ) { - self.init(confidence: confidenceForOF.confidence, session: nil) - } - - // Allows to pass a confidence object with injected configurations for testing - internal convenience init( - confidence: Confidence, - initializationStrategy: InitializationStrategy = .fetchAndActivate - ) { + public convenience init(confidence: Confidence, initializationStrategy: InitializationStrategy = .fetchAndActivate) { self.init(confidence: confidence, session: nil) } @@ -71,6 +34,7 @@ public class ConfidenceFeatureProvider: FeatureProvider { initializationStrategy: InitializationStrategy = .fetchAndActivate, session: URLSession? ) { + self.metadata = Metadata(name: ConfidenceFeatureProvider.providerId) self.initializationStrategy = initializationStrategy self.confidence = confidence } diff --git a/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift b/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift index f5f580c8..edf4945a 100644 --- a/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift +++ b/Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift @@ -1,10 +1,10 @@ import Foundation +import ConfidenceProvider import Combine import OpenFeature import XCTest @testable import Confidence -@testable import ConfidenceProvider class ConfidenceProviderTest: XCTestCase { private var readyExpectation = XCTestExpectation(description: "Ready") @@ -74,22 +74,4 @@ class ConfidenceProviderTest: XCTestCase { await fulfillment(of: [errorExpectation], timeout: 5.0) cancellable.cancel() } - - func testMetadata() async throws { - class FakeClient: ConfidenceResolveClient { - func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult { - throw ConfidenceError.internalError(message: "test") - } - } - - let confidenceForOpenFeature = ConfidenceFeatureProvider.createConfidence(clientSecret: "testSecret") - let provider = ConfidenceFeatureProvider( - confidenceForOF: confidenceForOpenFeature, - initializationStrategy: .activateAndFetchAsync - ) - XCTAssertEqual("SDK_ID_SWIFT_PROVIDER", provider.metadata.name) - let remoteClient = confidenceForOpenFeature.confidence.remoteFlagResolver as? RemoteConfidenceResolveClient - XCTAssertEqual("SDK_ID_SWIFT_PROVIDER", remoteClient?.metadata.name) - XCTAssertNotEqual("", remoteClient?.metadata.version) - } } diff --git a/Tests/ConfidenceTests/ConfidenceTest.swift b/Tests/ConfidenceTests/ConfidenceTest.swift index bb97b5b5..25543438 100644 --- a/Tests/ConfidenceTests/ConfidenceTest.swift +++ b/Tests/ConfidenceTests/ConfidenceTest.swift @@ -581,13 +581,6 @@ class ConfidenceTest: XCTestCase { XCTAssertEqual(error as? ConfidenceError, ConfidenceError.invalidContextInMessage) } } - - func testConfidenceMetadata() { - let confidence = Confidence.Builder(clientSecret: "").build() - let remoteClient = confidence.remoteFlagResolver as? RemoteConfidenceResolveClient - XCTAssertEqual("SDK_ID_SWIFT_CONFIDENCE", remoteClient?.metadata.name) - XCTAssertNotEqual("", remoteClient?.metadata.version) - } } final class DispatchQueueFake: DispatchQueueType {