From 6a1eca8b94bf895c4bea01d65e005fc47e883d46 Mon Sep 17 00:00:00 2001 From: Alex Russell-Saw Date: Wed, 30 Oct 2024 16:03:29 +0000 Subject: [PATCH 1/2] make swift sdk types public Signed-off-by: Alex Russell-Saw --- .../Sources/FliptClient/FliptClient.swift | 84 +++++++++---------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/flipt-client-swift/Sources/FliptClient/FliptClient.swift b/flipt-client-swift/Sources/FliptClient/FliptClient.swift index 535cd93c..bc0d73c6 100644 --- a/flipt-client-swift/Sources/FliptClient/FliptClient.swift +++ b/flipt-client-swift/Sources/FliptClient/FliptClient.swift @@ -9,8 +9,8 @@ public class FliptClient { private var ref: String = "" private var updateInterval: Int = 0 private var fetchMode: FetchMode = .polling - - init(namespace: String = "default", + + public init(namespace: String = "default", url: String = "", authentication: Authentication? = nil, ref: String = "", @@ -22,7 +22,7 @@ public class FliptClient { self.ref = ref self.updateInterval = updateInterval self.fetchMode = fetchMode - + let clientOptions = ClientOptions( url: url, authentication: authentication, @@ -30,51 +30,51 @@ public class FliptClient { reference: ref, fetchMode: fetchMode ) - + guard let jsonData = try? JSONEncoder().encode(clientOptions) else { throw ClientError.invalidOptions } - + let jsonStr = String(data: jsonData, encoding: .utf8) let namespaceCString = strdup(namespace) let clientOptionsCString = strdup(jsonStr) - + engine = initialize_engine(namespaceCString, clientOptionsCString) - + free(namespaceCString) free(clientOptionsCString) } - + deinit { close() } - - func close() { + + public func close() { if let engine = engine { destroy_engine(engine) self.engine = nil } } - - func evaluateVariant(flagKey: String, entityID: String, evalContext: [String: String]) throws -> VariantEvaluationResponse { + + public func evaluateVariant(flagKey: String, entityID: String, evalContext: [String: String]) throws -> VariantEvaluationResponse { let evaluationRequest = EvaluationRequest( flag_key: flagKey, entity_id: entityID, context: evalContext ) - + guard let requestData = try? JSONEncoder().encode(evaluationRequest) else { throw ClientError.invalidRequest } - + let requestCString = strdup(String(data: requestData, encoding: .utf8)) - + let variantResponse = evaluate_variant(engine, requestCString) free(requestCString) - + let responseString = String(cString: variantResponse!) destroy_string(UnsafeMutablePointer(mutating: variantResponse)) - + do { let variantResult = try JSONDecoder().decode(VariantResult.self, from: Data(responseString.utf8)) // Use variantResult here @@ -84,15 +84,15 @@ public class FliptClient { guard let result = variantResult.result else { throw ClientError.evaluationFailed(message: "missing result") } - + return result } catch { throw ClientError.parsingError } - + } - - func evaluateBoolean(flagKey: String, entityID: String, evalContext: [String: String]) throws -> BooleanEvaluationResponse { + + public func evaluateBoolean(flagKey: String, entityID: String, evalContext: [String: String]) throws -> BooleanEvaluationResponse { let evaluationRequest = EvaluationRequest( flag_key: flagKey, entity_id: entityID, @@ -121,8 +121,8 @@ public class FliptClient { return booleanResult.result! } - - func listFlags() throws -> [Flag] { + + public func listFlags() throws -> [Flag] { let flagsResponse = list_flags(engine) let responseString = String(cString: flagsResponse!) @@ -138,8 +138,8 @@ public class FliptClient { return listFlagsResult.result! } - - func evaluateBatch(requests: [EvaluationRequest]) throws -> BatchEvaluationResponse { + + public func evaluateBatch(requests: [EvaluationRequest]) throws -> BatchEvaluationResponse { guard let requestsData = try? JSONEncoder().encode(requests) else { throw ClientError.invalidRequest } @@ -164,9 +164,7 @@ public class FliptClient { } - - - enum ClientError: Error { + public enum ClientError: Error { case invalidOptions case invalidRequest case evaluationFailed(message: String) @@ -174,20 +172,20 @@ public class FliptClient { } } -enum Authentication: Encodable { +public enum Authentication: Encodable { case clientToken(String) case jwtToken(String) - + // Custom Codable logic to encode/decode based on the case private enum CodingKeys: String, CodingKey { case client_token case jwt_token } - + enum AuthenticationType: String, Codable { case clientToken, jwtToken } - + func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) switch self { @@ -199,26 +197,26 @@ enum Authentication: Encodable { } } -struct EvaluationRequest: Codable { +public struct EvaluationRequest: Codable { let flag_key: String let entity_id: String let context: [String: String] } -struct ClientTokenAuthentication: Codable { +public struct ClientTokenAuthentication: Codable { let token: String } -struct JWTAuthentication: Codable { +public struct JWTAuthentication: Codable { let token: String } -enum FetchMode: String, Codable { +public enum FetchMode: String, Codable { case streaming case polling } -struct ClientOptions: Encodable { +public struct ClientOptions: Encodable { let url: String let authentication: T? let updateInterval: Int @@ -226,13 +224,13 @@ struct ClientOptions: Encodable { let fetchMode: FetchMode } -struct Flag: Codable { +public struct Flag: Codable { let key: String let enabled: Bool let type: String } -struct VariantEvaluationResponse: Codable { +public struct VariantEvaluationResponse: Codable { let match: Bool let segment_keys: [String] let reason: String @@ -243,7 +241,7 @@ struct VariantEvaluationResponse: Codable { let timestamp: String } -struct BooleanEvaluationResponse: Codable { +public struct BooleanEvaluationResponse: Codable { let enabled: Bool let flag_key: String let reason: String @@ -251,18 +249,18 @@ struct BooleanEvaluationResponse: Codable { let timestamp: String } -struct ErrorEvaluationResponse: Codable { +public struct ErrorEvaluationResponse: Codable { let flag_key: String let namespace_key: String let reason: String } -struct BatchEvaluationResponse: Codable { +public struct BatchEvaluationResponse: Codable { let responses: [Response] let request_duration_millis: Double } -struct Response: Codable { +public struct Response: Codable { let type: String let variant_evaluation_response: VariantEvaluationResponse? let boolean_evaluation_response: BooleanEvaluationResponse? From e27fbc3f98704fc72d14d4b355646173fc22e8f2 Mon Sep 17 00:00:00 2001 From: Alex Russell-Saw Date: Wed, 30 Oct 2024 16:12:53 +0000 Subject: [PATCH 2/2] fix codable Signed-off-by: Alex Russell-Saw --- flipt-client-swift/Sources/FliptClient/FliptClient.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flipt-client-swift/Sources/FliptClient/FliptClient.swift b/flipt-client-swift/Sources/FliptClient/FliptClient.swift index bc0d73c6..0673ce2a 100644 --- a/flipt-client-swift/Sources/FliptClient/FliptClient.swift +++ b/flipt-client-swift/Sources/FliptClient/FliptClient.swift @@ -182,11 +182,11 @@ public enum Authentication: Encodable { case jwt_token } - enum AuthenticationType: String, Codable { + public enum AuthenticationType: String, Codable { case clientToken, jwtToken } - func encode(to encoder: Encoder) throws { + public func encode(to encoder: Encoder) throws { var container = encoder.container(keyedBy: CodingKeys.self) switch self { case .clientToken(let token):