Skip to content

Commit

Permalink
Smaller refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Apr 11, 2024
1 parent a41f6f3 commit 88cbee6
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 35 deletions.
6 changes: 2 additions & 4 deletions Sources/Common/Http/HttpClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ public enum HttpClientError: Error {
}

extension HTTPURLResponse {
public func mapStatusToError(error: HttpError?, flag: String = "unknown") -> Error {
public func mapStatusToError(error: HttpError?) -> Error {
let defaultError = ConfidenceError.internalError(
message: "General error: \(error?.message ?? "Unknown error")")

switch self.status {
case .notFound:
return ConfidenceError.badRequest(message: flag) // TODO
case .badRequest:
case .notFound, .badRequest:
return ConfidenceError.badRequest(message: error?.message ?? "")
default:
return defaultError
Expand Down
1 change: 0 additions & 1 deletion Sources/Common/Http/NetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ extension NetworkClient {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
if response.response.status == .ok {
// TODO: Inspect and log errors
response.decodedData = try decoder.decode(T.self, from: responseData)
} else {
do {
Expand Down
11 changes: 11 additions & 0 deletions Sources/Common/Http/Sdk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import Foundation

public struct Sdk: Codable {
public init(id: String?, version: String?) {
self.id = id ?? "SDK_ID_SWIFT_PROVIDER"
self.version = version ?? "unknown"
}

var id: String
var version: String
}
7 changes: 5 additions & 2 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ public class Confidence: ConfidenceEventSender {
self.parent = parent
}

// TODO: Implement actual event uploading to the backend
public func send(definition: String, payload: ConfidenceStruct) {
print("Sending: \"\(definition)\".\nMessage: \(payload)\nContext: \(context)")
Task {
try? await client.send(definition: definition, payload: payload)
// TODO: This will be called inside the EventSenderEngine once implemented
try? await client.upload(batch: [
ConfidenceClientEvent(definition: definition, payload: payload)
])
}
}

Expand Down Expand Up @@ -105,6 +107,7 @@ extension Confidence {
client: RemoteConfidenceClient(
options: ConfidenceClientOptions(
credentials: ConfidenceClientCredentials.clientSecret(secret: clientSecret),
timeout: timeout,
region: region),
metadata: ConfidenceMetadata(
name: "SDK_ID_SWIFT_CONFIDENCE",
Expand Down
7 changes: 6 additions & 1 deletion Sources/Confidence/ConfidenceClient/ConfidenceClient.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import Foundation

public protocol ConfidenceClient {
func send(definition: String, payload: ConfidenceStruct) async throws
func upload(batch: [ConfidenceClientEvent]) async throws
}

public struct ConfidenceClientEvent {
var definition: String
var payload: ConfidenceStruct
}
21 changes: 5 additions & 16 deletions Sources/Confidence/ConfidenceClient/RemoteConfidenceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public class RemoteConfidenceClient: ConfidenceClient {
self.metadata = metadata
}

public func send(definition: String, payload: ConfidenceStruct) async throws {
public func upload(batch: [ConfidenceClientEvent]) async throws {
let timeString = Date.backport.nowISOString
let request = PublishEventRequest(
events: [
events: batch.map { event in
Event(
eventDefinition: "eventDefinitions/\(definition)",
payload: payload,
eventDefinition: "eventDefinitions/\(event.definition)",
payload: event.payload,
eventTime: timeString
)
],
},
clientSecret: options.credentials.getSecret(),
sendTime: timeString,
sdk: Sdk(id: metadata.name, version: metadata.version)
Expand Down Expand Up @@ -99,14 +99,3 @@ struct EventError: Decodable {
case unknown
}
}


struct Sdk: Encodable {
init(id: String?, version: String?) {
self.id = id ?? "SDK_ID_SWIFT_PROVIDER"
self.version = version ?? "unknown"
}

var id: String
var version: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ struct ApplyFlagsRequest: Codable {
struct ApplyFlagsResponse: Codable {
}

struct Sdk: Codable {
init(id: String?, version: String?) {
self.id = id ?? "SDK_ID_SWIFT_PROVIDER"
self.version = version ?? "unknown"
}

var id: String
var version: String
}

private func displayName(resolvedFlag: ResolvedFlag) throws -> String {
let flagNameComponents = resolvedFlag.flag.components(separatedBy: "/")
if flagNameComponents.count <= 1 || flagNameComponents[0] != "flags" {
Expand Down
2 changes: 1 addition & 1 deletion Tests/ConfidenceTests/Helpers/ConfidenceClientMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
@testable import Confidence

class ConfidenceClientMock: ConfidenceClient {
func send(definition: String, payload: ConfidenceStruct) async throws {
func upload(batch: [ConfidenceClientEvent]) async throws {
// NO-OP
}
}

0 comments on commit 88cbee6

Please sign in to comment.