Skip to content

Commit

Permalink
feat: Confidence builder in Provider for edits
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Jun 17, 2024
1 parent 54a674e commit 99b033c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
8 changes: 7 additions & 1 deletion Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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(
Expand Down
13 changes: 9 additions & 4 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
) {
Expand All @@ -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?) {
Expand Down
14 changes: 8 additions & 6 deletions Tests/ConfidenceProviderTests/ConfidenceProviderTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 99b033c

Please sign in to comment.