Skip to content

Commit

Permalink
feat: Add SDK id and version to requests (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria authored Nov 16, 2023
1 parent 2c19f40 commit e1cb474
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@ final class FlagApplierWithRetries: FlagApplier {
private let httpClient: HttpClient
private let options: ConfidenceClientOptions
private let cacheDataInteractor: CacheDataActor
private let metadata: ConfidenceMetadata

init(
httpClient: HttpClient,
storage: Storage,
options: ConfidenceClientOptions,
metadata: ConfidenceMetadata,
cacheDataInteractor: CacheDataActor? = nil,
triggerBatch: Bool = true
) {
self.storage = storage
self.httpClient = httpClient
self.options = options
self.metadata = metadata

let storedData = try? storage.load(defaultValue: CacheData.empty())
self.cacheDataInteractor = cacheDataInteractor ?? CacheDataInteractor(cacheData: storedData ?? .empty())
Expand Down Expand Up @@ -119,7 +122,8 @@ final class FlagApplierWithRetries: FlagApplier {
flags: applyFlagRequestItems,
sendTime: Date.backport.nowISOString,
clientSecret: options.credentials.getSecret(),
resolveToken: resolveToken
resolveToken: resolveToken,
sdk: Sdk(id: metadata.name, version: metadata.version)
)

performRequest(request: request) { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class RemoteConfidenceClient: ConfidenceClient {
private let targetingKey = "targeting_key"
private let flagApplier: FlagApplier
private var options: ConfidenceClientOptions
private let metadata: ConfidenceMetadata

private var httpClient: HttpClient
private var applyOnResolve: Bool
Expand All @@ -13,12 +14,14 @@ public class RemoteConfidenceClient: ConfidenceClient {
options: ConfidenceClientOptions,
session: URLSession? = nil,
applyOnResolve: Bool,
flagApplier: FlagApplier
flagApplier: FlagApplier,
metadata: ConfidenceMetadata
) {
self.options = options
self.httpClient = NetworkClient(session: session, region: options.region)
self.flagApplier = flagApplier
self.applyOnResolve = applyOnResolve
self.metadata = metadata
}

// MARK: Resolver
Expand All @@ -28,7 +31,9 @@ public class RemoteConfidenceClient: ConfidenceClient {
flags: flags.map { "flags/\($0)" },
evaluationContext: try getEvaluationContextStruct(ctx: ctx),
clientSecret: options.credentials.getSecret(),
apply: applyOnResolve)
apply: applyOnResolve,
sdk: Sdk(id: metadata.name, version: metadata.version)
)

do {
let result: HttpClientResponse<ResolveFlagsResponse> =
Expand Down Expand Up @@ -108,6 +113,7 @@ struct ResolveFlagsRequest: Codable {
var evaluationContext: Struct
var clientSecret: String
var apply: Bool
var sdk: Sdk
}

struct ResolveFlagsResponse: Codable {
Expand Down Expand Up @@ -149,6 +155,7 @@ struct ApplyFlagsRequest: Codable {
var sendTime: String
var clientSecret: String
var resolveToken: String
var sdk: Sdk
}

struct ApplyFlagsResponse: Codable {
Expand Down Expand Up @@ -184,6 +191,16 @@ public enum ConfidenceRegion: String {
case usa = "us"
}

struct Sdk: Codable {
init(id: String?, version: String?) {
self.id = id ?? "SDK_ID_SWIFT_PROVIDER"
self.version = version ?? "unknown"
}

var id: String
var version: String
}

private func displayName(resolvedFlag: ResolvedFlag) throws -> String {
let flagNameComponents = resolvedFlag.flag.components(separatedBy: "/")
if flagNameComponents.count <= 1 || flagNameComponents[0] != "flags" {
Expand Down
12 changes: 9 additions & 3 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import os
// swiftlint:disable type_body_length
// swiftlint:disable file_length
public class ConfidenceFeatureProvider: FeatureProvider {
public var metadata: ProviderMetadata
public var hooks: [any Hook] = []
public let metadata: ProviderMetadata = ConfidenceMetadata()
private let lock = UnfairLock()
private var resolver: Resolver
private let client: ConfidenceClient
Expand All @@ -22,6 +22,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {

/// Should not be called externally, use `ConfidenceFeatureProvider.Builder` instead.
init(
metadata: ProviderMetadata,
client: RemoteConfidenceClient,
cache: ProviderCache,
storage: Storage,
Expand All @@ -31,6 +32,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
initializationStrategy: InitializationStrategy
) {
self.client = client
self.metadata = metadata
self.cache = cache
self.overrides = overrides
self.flagApplier = flagApplier
Expand Down Expand Up @@ -393,6 +395,7 @@ extension ConfidenceFeatureProvider {
extension ConfidenceFeatureProvider {
public struct Builder {
var options: ConfidenceClientOptions
let metadata = ConfidenceMetadata(version: "0.1.1") // x-release-please-version
var session: URLSession?
var localOverrides: [String: LocalOverride] = [:]
var storage: Storage = DefaultStorage.resolverFlagsCache()
Expand Down Expand Up @@ -574,7 +577,8 @@ extension ConfidenceFeatureProvider {
?? FlagApplierWithRetries(
httpClient: NetworkClient(region: options.region),
storage: DefaultStorage.applierFlagsCache(),
options: options
options: options,
metadata: metadata
)

let cache = cache ?? InMemoryProviderCache.from(storage: storage)
Expand All @@ -583,10 +587,12 @@ extension ConfidenceFeatureProvider {
options: options,
session: self.session,
applyOnResolve: false,
flagApplier: flagApplier
flagApplier: flagApplier,
metadata: metadata
)

return ConfidenceFeatureProvider(
metadata: metadata,
client: client,
cache: cache,
storage: storage,
Expand Down
1 change: 0 additions & 1 deletion Sources/ConfidenceProvider/InitializationStrategy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ import Foundation
public enum InitializationStrategy {
case fetchAndActivate, activateAndFetchAsync
}

3 changes: 2 additions & 1 deletion Sources/ConfidenceProvider/Utils/ConfidenceMetadata.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import OpenFeature

public struct ConfidenceMetadata: ProviderMetadata {
public var name: String? = "Confidence"
public var name: String? = "SDK_ID_SWIFT_PROVIDER"
public var version: String?
}
28 changes: 21 additions & 7 deletions Tests/ConfidenceProviderTests/FlagApplierWithRetriesTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
private let options = ConfidenceClientOptions(credentials: .clientSecret(secret: "test"))
private var storage = StorageMock()
private var httpClient = HttpClientMock()
private let metadata = ConfidenceMetadata(name: "test-provider-name", version: "0.0.0.")

override func setUp() {
storage = StorageMock()
Expand All @@ -22,7 +23,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApply_differentTokens() async {
// Given flag applier
let applier = FlagApplierWithRetries(
httpClient: httpClient, storage: storage, options: options, triggerBatch: false
httpClient: httpClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)

// When 3 apply calls are issued with different tokens
Expand All @@ -37,7 +38,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
func testApply_duplicateEventsAreNotSent() async {
// Given flag applier
let applier = FlagApplierWithRetries(
httpClient: httpClient, storage: storage, options: options, triggerBatch: false
httpClient: httpClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)

// When 3 identical apply calls are issued
Expand All @@ -56,6 +57,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: httpClient,
storage: storage,
options: options,
metadata: metadata,
cacheDataInteractor: cacheDataInteractor,
triggerBatch: false
)
Expand All @@ -80,6 +82,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: httpClient,
storage: storage,
options: options,
metadata: metadata,
cacheDataInteractor: cacheDataInteractor,
triggerBatch: false
)
Expand Down Expand Up @@ -114,6 +117,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: httpClient,
storage: storage,
options: options,
metadata: metadata,
triggerBatch: false
)
}
Expand Down Expand Up @@ -141,7 +145,8 @@ class FlagApplierWithRetriesTest: XCTestCase {
_ = FlagApplierWithRetries(
httpClient: httpClient,
storage: prefilledStorage,
options: options
options: options,
metadata: metadata
)

await waitForExpectations(timeout: 5.0)
Expand Down Expand Up @@ -171,7 +176,8 @@ class FlagApplierWithRetriesTest: XCTestCase {
_ = FlagApplierWithRetries(
httpClient: partiallyFailingHttpClient,
storage: prefilledStorage,
options: options
options: options,
metadata: metadata
)

await waitForExpectations(timeout: 5.0)
Expand Down Expand Up @@ -200,6 +206,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: httpClient,
storage: storage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand Down Expand Up @@ -243,6 +250,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: offlineClient,
storage: storage,
options: options,
metadata: metadata,
cacheDataInteractor: cacheDataInteractor,
triggerBatch: false
)
Expand Down Expand Up @@ -285,7 +293,8 @@ class FlagApplierWithRetriesTest: XCTestCase {
_ = FlagApplierWithRetries(
httpClient: httpClient,
storage: prefilledStorage,
options: options
options: options,
metadata: metadata
)

await waitForExpectations(timeout: 1.0)
Expand All @@ -310,6 +319,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: httpClient,
storage: prefilledStorage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand Down Expand Up @@ -337,6 +347,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: offlineClient,
storage: prefilledStorage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand All @@ -350,7 +361,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
// Given offline http client and flag applier
let offlineClient = HttpClientMock(testMode: .error)
let applier = FlagApplierWithRetries(
httpClient: offlineClient, storage: storage, options: options, triggerBatch: false
httpClient: offlineClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)

// When 3 apply calls are issued with different flag names
Expand Down Expand Up @@ -379,7 +390,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
// Given offline http client and flag applier
let offlineClient = HttpClientMock(testMode: .error)
let applier = FlagApplierWithRetries(
httpClient: offlineClient, storage: storage, options: options, triggerBatch: false
httpClient: offlineClient, storage: storage, options: options, metadata: metadata, triggerBatch: false
)

// When 3 apply calls are issued with different tokens
Expand Down Expand Up @@ -417,6 +428,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: offlineClient,
storage: prefilledStorage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand Down Expand Up @@ -449,6 +461,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: offlineClient,
storage: prefilledStorage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand Down Expand Up @@ -480,6 +493,7 @@ class FlagApplierWithRetriesTest: XCTestCase {
httpClient: offlineClient,
storage: prefilledStorage,
options: options,
metadata: metadata,
triggerBatch: false
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class RemoteConfidenceClientTest: XCTestCase {
options: .init(credentials: .clientSecret(secret: "test")),
session: session,
applyOnResolve: true,
flagApplier: flagApplier
flagApplier: flagApplier,
metadata: ConfidenceMetadata()
)

let result = try await client.resolve(ctx: MutableContext(targetingKey: "user1"))
Expand Down
5 changes: 4 additions & 1 deletion release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"bump-patch-for-minor-pre-major": true,
"versioning": "default",
"include-v-in-tag": false,
"extra-files": ["README.md"]
"extra-files": [
"README.md",
"Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift"
]
}
},
"changelog-sections": [
Expand Down

0 comments on commit e1cb474

Please sign in to comment.