Skip to content

Commit

Permalink
Basic EvaluationContext wiring in events
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Mar 28, 2024
1 parent 23c6ebd commit 839db71
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 43 deletions.
5 changes: 4 additions & 1 deletion ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ extension ConfidenceDemoApp {

// 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())
OpenFeatureAPI.shared.setProvider(provider: provider, initialContext: ctx)
Task {
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: ctx)
confidence.send(eventName: "my_event")
}
}
}
10 changes: 5 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ let package = Package(
.package(url: "[email protected]:open-feature/swift-sdk.git", from: "0.1.0"),
],
targets: [
.target(
name: "Confidence",
dependencies: [],
plugins: []
),
.target(
name: "ConfidenceProvider",
dependencies: [
Expand All @@ -29,11 +34,6 @@ let package = Package(
],
plugins: []
),
.target(
name: "Confidence",
dependencies: [],
plugins: []
),
.testTarget(
name: "ConfidenceProviderTests",
dependencies: [
Expand Down
67 changes: 48 additions & 19 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ import Foundation
public class Confidence: ConfidenceEventSender {
public var context: [String: String]
public let clientSecret: String
public let options: ConfidenceClientOptions
public var timeout: TimeInterval
public var region: ConfidenceRegion
public var initializationStrategy: InitializationStrategy

init(clientSecret: String, options: ConfidenceClientOptions) {
self.clientSecret = clientSecret
self.options = options
init(clientSecret: String,

Check warning on line 10 in Sources/Confidence/Confidence.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Multiline Parameters Brackets Violation: Multiline parameters should have their surrounding brackets in a new line (multiline_parameters_brackets)
timeout: TimeInterval,

Check warning on line 11 in Sources/Confidence/Confidence.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Indentation Width Violation: Code should be indented using one tab or 4 spaces (indentation_width)
region: ConfidenceRegion,
initializationStrategy: InitializationStrategy) {

Check warning on line 13 in Sources/Confidence/Confidence.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Multiline Parameters Brackets Violation: Multiline parameters should have their surrounding brackets in a new line (multiline_parameters_brackets)
self.context = [:]
self.clientSecret = clientSecret
self.timeout = timeout
self.region = region
self.initializationStrategy = initializationStrategy
}

// TODO: Implement actual event uploading to the backend
public func send(eventName: String) {
print("Sending \(eventName)")
print("Sending \(eventName) - Targeting key: \(context["targeting_key"] ?? "UNKNOWN")")
}

public func updateContextEntry(key: String, value: String) {
Expand All @@ -27,37 +35,58 @@ public class Confidence: ConfidenceEventSender {
context = [:]
}

// TODO: Implement creation of child instances
public func withContext(_ context: [String: String]) -> Self {
// TODO
return self
}
}

extension Confidence {
public struct Builder {
public class Builder {
let clientSecret: String
var options: ConfidenceClientOptions
var timeout: TimeInterval = 10.0
var region: ConfidenceRegion = .global
var initializationStrategy: InitializationStrategy = .fetchAndActivate

public init(clientSecret: String) {
self.clientSecret = clientSecret
self.options = ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: (clientSecret)))
}

init(clientSecret: String, options: ConfidenceClientOptions) {
self.clientSecret = clientSecret
self.options = options
public func withTimeout(timeout: TimeInterval) -> Builder {
self.timeout = timeout
return self
}

public func withOptions(options: ConfidenceClientOptions) -> Builder {
return Builder(
clientSecret: clientSecret,
options: options
)

public func withRegion(region: ConfidenceRegion) -> Builder {
self.region = region
return self
}

public func withInitializationstrategy(initializationStrategy: InitializationStrategy) -> Builder {
self.initializationStrategy = initializationStrategy
return self
}

public func build() -> Confidence {
return Confidence(clientSecret: clientSecret, options: ConfidenceClientOptions())
return Confidence(
clientSecret: clientSecret,
timeout: timeout,
region: region,
initializationStrategy: initializationStrategy
)
}
}
}

public enum InitializationStrategy {
case fetchAndActivate, activateAndFetchAsync
}

public enum ConfidenceRegion {
case global
case europe
case usa
}


Check warning on line 92 in Sources/Confidence/Confidence.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Newline Violation: Files should have a single trailing newline (trailing_newline)
2 changes: 2 additions & 0 deletions Sources/Confidence/ConfidenceEventSender.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Foundation

/// Sends events to Confidence. Contextual data is appended to each event
// TODO: Add functions for sending events with payload
public protocol ConfidenceEventSender: Contextual {
func send(eventName: String)
}
8 changes: 6 additions & 2 deletions Sources/Confidence/Contextual.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Foundation

/// A Contextual implementer maintains context data and can create child instances
/// that can still access their parent's data
public protocol Contextual {
var context: [String: String] { get set } // TODO Introdue complex types
// TODO: Add complex type to the context Dictionary
var context: [String: String] { get set }

func updateContextEntry(key: String, value: String)
func removeContextEntry(key: String)
func clearContext()

/// Creates a child Contextual instance that still has access
/// to its parent context
func withContext(_ context: [String: String]) -> Self
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import Confidence

public struct ConfidenceClientOptions {
public var credentials: ConfidenceClientCredentials // DEPRECATED
Expand All @@ -7,12 +8,12 @@ public struct ConfidenceClientOptions {
public var initializationStrategy: InitializationStrategy

public init(
credentials: ConfidenceClientCredentials? = nil,
credentials: ConfidenceClientCredentials,
timeout: TimeInterval? = nil,
region: ConfidenceRegion? = nil,
initializationStrategy: InitializationStrategy = .fetchAndActivate
) {
self.credentials = credentials ?? ConfidenceClientCredentials.clientSecret(secret: "")
self.credentials = credentials
self.timeout = timeout ?? 10.0
self.region = region ?? .global
self.initializationStrategy = initializationStrategy
Expand All @@ -29,13 +30,3 @@ public enum ConfidenceClientCredentials {
}
}
}

public enum ConfidenceRegion {
case global
case europe
case usa
}

public enum InitializationStrategy {
case fetchAndActivate, activateAndFetchAsync
}
17 changes: 13 additions & 4 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

if self.initializationStrategy == .activateAndFetchAsync {
// TODO: Set the entire context
confidence?.context = ["targeting_key": "CACHED"]
eventHandler.send(.ready)
}

Expand All @@ -70,6 +72,8 @@ public class ConfidenceFeatureProvider: FeatureProvider {

// signal the provider is ready after the network request is done
if self.initializationStrategy == .fetchAndActivate {
// TODO: Set the entire context
confidence?.context = ["targeting_key": initialContext.getTargetingKey()]
eventHandler.send(.ready)
}
} catch {
Expand Down Expand Up @@ -111,6 +115,8 @@ public class ConfidenceFeatureProvider: FeatureProvider {
// update the storage
try await store(with: newContext, resolveResult: resolveResult, refreshCache: true)
eventHandler.send(ProviderEvent.ready)
// TODO: Set the entire context
confidence?.context = ["targeting_key": newContext.getTargetingKey()]
} catch {
eventHandler.send(ProviderEvent.ready)
// do nothing
Expand Down Expand Up @@ -416,7 +422,8 @@ extension ConfidenceFeatureProvider {
var applyStorage: Storage = DefaultStorage.resolverApplyCache()
var confidence: Confidence?

/// Initializes the builder with the given credentails. DEPRECATED
/// DEPRECATED
/// Initializes the builder with the given credentails.
///
/// OpenFeatureAPI.shared.setProvider(provider:
/// ConfidenceFeatureProvider.Builder(credentials: .clientSecret(secret: "mysecret"))
Expand All @@ -427,9 +434,11 @@ extension ConfidenceFeatureProvider {

/// TODO
public init(confidence: Confidence) {
self.options = ConfidenceClientOptions(credentials: ConfidenceClientCredentials
.clientSecret(secret: confidence.clientSecret))
self.initializationStrategy = confidence.options.initializationStrategy
self.options = ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: confidence.clientSecret),
timeout: confidence.timeout,
region: confidence.region)
self.initializationStrategy = confidence.initializationStrategy
self.confidence = confidence
}

Expand Down

0 comments on commit 839db71

Please sign in to comment.