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

Added getIdentityFlags method #59

Merged
merged 4 commits into from
Sep 4, 2024
Merged
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
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 @@
/// - 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)
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
} 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
Loading