Skip to content

Commit

Permalink
fix: use storage provider (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew authored Oct 25, 2023
1 parent eed0504 commit 8360e37
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
24 changes: 16 additions & 8 deletions Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,45 @@ public enum PollerError: Error {
}

public protocol StorageProvider {
func set(_ value: Toggle?, for key: String)
func value(for key: String) -> Toggle?
func set(value: Toggle?, key: String)
func value(key: String) -> Toggle?
func clear()
}

public class DictionaryStorageProvider: StorageProvider {
private var storage: [String: Toggle] = [:]

public init() {}

public func set(_ value: Toggle?, for key: String) {
public func set(value: Toggle?, key: String) {
storage[key] = value
}

public func value(for key: String) -> Toggle? {
public func value(key: String) -> Toggle? {
return storage[key]
}

public func clear() {
storage = [:]
}
}

public class Poller {
var refreshInterval: Int?
var unleashUrl: URL
var timer: Timer?
var toggles: [String: Toggle] = [:]
var ready: Bool
var apiKey: String;
var etag: String;

private let session: PollerSession
private let storageProvider: StorageProvider
var storageProvider: StorageProvider

public init(refreshInterval: Int? = nil, unleashUrl: URL, apiKey: String, session: PollerSession = URLSession.shared, storageProvider: StorageProvider = DictionaryStorageProvider()) {
self.refreshInterval = refreshInterval
self.unleashUrl = unleashUrl
self.apiKey = apiKey
self.timer = nil
self.toggles = [:]
self.ready = false
self.etag = ""
self.session = session
Expand Down Expand Up @@ -81,11 +84,16 @@ public class Poller {
}

private func createFeatureMap(features: FeatureResponse) {
self.storageProvider.clear()
features.toggles.forEach { toggle in
self.storageProvider.set(toggle, for: toggle.name)
self.storageProvider.set(value: toggle, key: toggle.name)
}
}

public func getFeature(name: String) -> Toggle? {
return self.storageProvider.value(key: name);
}

func getFeatures(context: Context, completionHandler: ((PollerError?) -> Void)? = nil) -> Void {
guard let url = formatURL(context: context) else {
completionHandler?(.url)
Expand Down
4 changes: 2 additions & 2 deletions Sources/UnleashProxyClientSwift/UnleashProxyClientSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ public class UnleashClientBase {
}

public func isEnabled(name: String) -> Bool {
let enabled = poller.toggles[name]?.enabled ?? false
let enabled = poller.getFeature(name: name)?.enabled ?? false
metrics.count(name: name, enabled: enabled)
return enabled
}

public func getVariant(name: String) -> Variant {
let variant = poller.toggles[name]?.variant ?? Variant(name: "disabled", enabled: false, payload: nil)
let variant = poller.getFeature(name: name)?.variant ?? Variant(name: "disabled", enabled: false, payload: nil)
metrics.count(name: name, enabled: variant.enabled)
metrics.countVariant(name: name, variant: variant.name)
return variant
Expand Down
22 changes: 21 additions & 1 deletion Tests/UnleashProxyClientSwiftTests/MockPoller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ class MockPollerSession: PollerSession {
}
}

public class MockDictionaryStorageProvider: StorageProvider {
private var storage: [String: Toggle] = [:]

public init(storage: [String: Toggle]) {
self.storage = storage
}

public func set(value: Toggle?, key: String) {
storage[key] = value
}

public func value(key: String) -> Toggle? {
return storage[key]
}

public func clear() {
storage = [:]
}
}

class MockPoller: Poller {
var dataGenerator: () -> [String: Toggle];

Expand All @@ -33,6 +53,6 @@ class MockPoller: Poller {
}

override func getFeatures(context: Context, completionHandler: ((PollerError?) -> Void)? = nil) -> Void {
self.toggles = dataGenerator()
self.storageProvider = MockDictionaryStorageProvider(storage: dataGenerator())
}
}
2 changes: 1 addition & 1 deletion UnleashProxyClientSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "UnleashProxyClientSwift"
spec.version = "1.0.2"
spec.version = "1.0.3"
spec.summary = "Allows frontend clients to talk to unleash through the unleash edge, frontend API or the (deprecated) unleash proxy"
spec.homepage = "https://www.getunleash.io"
spec.license = { :type => "MIT", :file => "LICENSE" }
Expand Down

0 comments on commit 8360e37

Please sign in to comment.