Skip to content

Commit

Permalink
Merge pull request #495 from flipt-io/ars-202410301603
Browse files Browse the repository at this point in the history
make swift sdk types public
  • Loading branch information
arussellsaw authored Oct 30, 2024
2 parents d45fd4c + e27fbc3 commit 581dab6
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions flipt-client-swift/Sources/FliptClient/FliptClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "",
Expand All @@ -22,59 +22,59 @@ public class FliptClient {
self.ref = ref
self.updateInterval = updateInterval
self.fetchMode = fetchMode

let clientOptions = ClientOptions(
url: url,
authentication: authentication,
updateInterval: updateInterval,
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
Expand All @@ -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,
Expand Down Expand Up @@ -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!)
Expand All @@ -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
}
Expand All @@ -164,31 +164,29 @@ public class FliptClient {
}




enum ClientError: Error {
public enum ClientError: Error {
case invalidOptions
case invalidRequest
case evaluationFailed(message: String)
case parsingError
}
}

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 {

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):
Expand All @@ -199,40 +197,40 @@ 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<T: Encodable>: Encodable {
public struct ClientOptions<T: Encodable>: Encodable {
let url: String
let authentication: T?
let updateInterval: Int
let reference: String
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
Expand All @@ -243,26 +241,26 @@ struct VariantEvaluationResponse: Codable {
let timestamp: String
}

struct BooleanEvaluationResponse: Codable {
public struct BooleanEvaluationResponse: Codable {
let enabled: Bool
let flag_key: String
let reason: String
let request_duration_millis: Double
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?
Expand Down

0 comments on commit 581dab6

Please sign in to comment.