@@ -83,36 +83,52 @@ public final class Flagsmith: @unchecked Sendable {
8383 /// - identity: ID of the user (optional)
8484 /// - completion: Closure with Result which contains array of Flag objects in case of success or Error in case of failure
8585 public func getFeatureFlags( forIdentity identity: String ? = nil ,
86+ traits: [ Trait ] ? = nil ,
8687 completion: @Sendable @escaping ( Result < [ Flag ] , any Error > ) -> Void )
8788 {
8889 if let identity = identity {
89- getIdentity ( identity) { result in
90- switch result {
91- case let . success( thisIdentity) :
92- completion ( . success( thisIdentity. flags) )
93- case let . failure( error) :
94- if self . defaultFlags. isEmpty {
95- completion ( . failure( error) )
96- } else {
97- completion ( . success( self . defaultFlags) )
90+ if let traits = traits {
91+ apiManager. request ( . postTraits( identity: identity, traits: traits) ) { ( result: Result < Traits , Error > ) in
92+ switch result {
93+ case let . success( result) :
94+ completion ( . success( result. flags) )
95+ case let . failure( error) :
96+ self . handleFlagsError ( error, completion: completion)
97+ }
98+ }
99+ } else {
100+ getIdentity ( identity) { result in
101+ switch result {
102+ case let . success( thisIdentity) :
103+ completion ( . success( thisIdentity. flags) )
104+ case let . failure( error) :
105+ self . handleFlagsError ( error, completion: completion)
98106 }
99107 }
100108 }
101109 } else {
102- apiManager . request ( . getFlags ) { ( result : Result < [ Flag ] , Error > ) in
103- switch result {
104- case let . success ( flags ) :
105- completion ( . success ( flags ) )
106- case let . failure ( error ) :
107- if self . defaultFlags . isEmpty {
108- completion ( . failure ( error ) )
109- } else {
110- completion ( . success ( self . defaultFlags ) )
110+ if let _ = traits {
111+ completion ( . failure ( FlagsmithError . invalidArgument ( " You must provide an identity to set traits " ) ) )
112+ } else {
113+ apiManager . request ( . getFlags ) { ( result : Result < [ Flag ] , Error > ) in
114+ switch result {
115+ case let . success ( flags ) :
116+ completion ( . success ( flags ) )
117+ case let . failure ( error ) :
118+ self . handleFlagsError ( error , completion : completion )
111119 }
112120 }
113121 }
114122 }
115123 }
124+
125+ private func handleFlagsError( _ error: any Error , completion: @Sendable @escaping ( Result < [ Flag ] , any Error > ) -> Void ) {
126+ if self . defaultFlags. isEmpty {
127+ completion ( . failure( error) )
128+ } else {
129+ completion ( . success( self . defaultFlags) )
130+ }
131+ }
116132
117133 /// Check feature exists and is enabled optionally for a specific identity
118134 ///
0 commit comments