Skip to content
This repository has been archived by the owner on Sep 13, 2024. It is now read-only.

Implemented getting flags by identity and setting traits at the same time #1

Merged
merged 2 commits into from
May 31, 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
15 changes: 15 additions & 0 deletions FlagsmithClient/Classes/Flagsmith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,21 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to do something with the error?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just mirrored the other methods that do things with traits

completion(result.map(\.flags))
}
}

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