Skip to content

Commit

Permalink
Removed getIdentityFlags method and added traits variable to getFeatu…
Browse files Browse the repository at this point in the history
…reFlags method
  • Loading branch information
jackforesightmobile committed Jun 20, 2024
1 parent f6a4e64 commit 7046559
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 33 deletions.
67 changes: 34 additions & 33 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 Expand Up @@ -268,21 +284,6 @@ public final class Flagsmith: @unchecked Sendable {
completion(result.map(\.traits))
}
}

/// Get flags for an identity and set traits in the same call
///
/// - Parameters:
/// - traits: Traits to be created or updated
/// - identity: ID of the user
/// - completion: Closure with Result which contains a list of Flags in case of success or Error in case of failure
public func getIdentityFlags(_ traits: [Trait],
forIdentity identity: String,
completion: @Sendable @escaping (Result<[Flag], any Error>) -> Void)
{
apiManager.request(.postTraits(identity: identity, traits: traits)) { (result: Result<Traits, Error>) in
completion(result.map(\.flags))
}
}

/// Get both feature flags and user traits for the provided 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

0 comments on commit 7046559

Please sign in to comment.