Skip to content

Commit

Permalink
Added getIdentityFlags method (#59)
Browse files Browse the repository at this point in the history
* Implemented getting flags by identity and setting traits at the same time

* Fix formatting

* Removed getIdentityFlags method and added traits variable to getFeatureFlags method
  • Loading branch information
jackforesightmobile authored Sep 4, 2024
1 parent 31e9be3 commit 0ae106d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 19 deletions.
52 changes: 34 additions & 18 deletions FlagsmithClient/Classes/Flagsmith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,36 +83,52 @@ public final class Flagsmith: @unchecked Sendable {
/// - identity: ID of the user (optional)
/// - completion: Closure with Result which contains array of Flag objects in case of success or Error in case of failure
public func getFeatureFlags(forIdentity identity: String? = nil,
traits: [Trait]? = nil,
completion: @Sendable @escaping (Result<[Flag], any Error>) -> Void)
{
if let identity = identity {
getIdentity(identity) { result in
switch result {
case let .success(thisIdentity):
completion(.success(thisIdentity.flags))
case let .failure(error):
if self.defaultFlags.isEmpty {
completion(.failure(error))
} else {
completion(.success(self.defaultFlags))
if let traits = traits {
apiManager.request(.postTraits(identity: identity, traits: traits)) { (result: Result<Traits, Error>) in
switch result {
case let .success(result):
completion(.success(result.flags))
case let .failure(error):
self.handleFlagsError(error, completion: completion)
}
}
} else {
getIdentity(identity) { result in
switch result {
case let .success(thisIdentity):
completion(.success(thisIdentity.flags))
case let .failure(error):
self.handleFlagsError(error, completion: completion)
}
}
}
} else {
apiManager.request(.getFlags) { (result: Result<[Flag], Error>) in
switch result {
case let .success(flags):
completion(.success(flags))
case let .failure(error):
if self.defaultFlags.isEmpty {
completion(.failure(error))
} else {
completion(.success(self.defaultFlags))
if let _ = traits {

Check warning on line 110 in FlagsmithClient/Classes/Flagsmith.swift

View workflow job for this annotation

GitHub Actions / swift-lint

Unused Optional Binding Violation: Prefer `!= nil` over `let _ =` (unused_optional_binding)
completion(.failure(FlagsmithError.invalidArgument("You must provide an identity to set traits")))
} else {
apiManager.request(.getFlags) { (result: Result<[Flag], Error>) in
switch result {
case let .success(flags):
completion(.success(flags))
case let .failure(error):
self.handleFlagsError(error, completion: completion)
}
}
}
}
}

private func handleFlagsError(_ error: any Error, completion: @Sendable @escaping (Result<[Flag], any Error>) -> Void) {
if self.defaultFlags.isEmpty {
completion(.failure(error))
} else {
completion(.success(self.defaultFlags))
}
}

/// Check feature exists and is enabled optionally for a specific identity
///
Expand Down
4 changes: 4 additions & 0 deletions FlagsmithClient/Classes/FlagsmithError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public enum FlagsmithError: LocalizedError, Sendable {
case decoding(DecodingError)
/// Unknown or unhandled error was encountered.
case unhandled(any Error)
/// Invalid argument error
case invalidArgument(String)

public var errorDescription: String? {
switch self {
Expand All @@ -36,6 +38,8 @@ public enum FlagsmithError: LocalizedError, Sendable {
return "API Response could not be decoded: \(error.localizedDescription)"
case let .unhandled(error):
return "An unknown or unhandled error was encountered: \(error.localizedDescription)"
case let .invalidArgument(error):
return "Invalid argument error: \(error)"
}
}

Expand Down
7 changes: 7 additions & 0 deletions FlagsmithClient/Classes/Traits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ import Foundation
public struct Traits: Codable, Sendable {
public let traits: [Trait]
public let identifier: String?
public let flags: [Flag]

init(traits: [Trait], identifier: String?, flags: [Flag] = []) {
self.traits = traits
self.identifier = identifier
self.flags = flags
}
}
3 changes: 2 additions & 1 deletion FlagsmithClient/Tests/RouterTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ final class RouterTests: FlagsmithClientTestCase {
"trait_value" : 42
}
],
"identifier" : "A1B2C3D4"
"identifier" : "A1B2C3D4",
"flags": []
}
""".json(using: .utf8)
let body = try request.httpBody.json()
Expand Down

0 comments on commit 0ae106d

Please sign in to comment.