Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ConfidenceBuilder in Provider, metadata override #139

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading