From 28f7f025856ddee14db937cace42c15c5c6ec080 Mon Sep 17 00:00:00 2001 From: Dariusz Rybicki <dariusz@elixxir.io> Date: Fri, 22 Jul 2022 13:08:54 +0100 Subject: [PATCH] Add ConnectionSend functor --- Sources/ElixxirDAppsSDK/Connection.swift | 4 ++ Sources/ElixxirDAppsSDK/ConnectionSend.swift | 37 +++++++++++++++ .../Legacy/MessageSender.swift | 46 ------------------- 3 files changed, 41 insertions(+), 46 deletions(-) create mode 100644 Sources/ElixxirDAppsSDK/ConnectionSend.swift delete mode 100644 Sources/ElixxirDAppsSDK/Legacy/MessageSender.swift diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift index 8009d9c1..04d10312 100644 --- a/Sources/ElixxirDAppsSDK/Connection.swift +++ b/Sources/ElixxirDAppsSDK/Connection.swift @@ -4,6 +4,7 @@ public struct Connection { public var isAuthenticated: ConnectionIsAuthenticated public var getId: ConnectionGetId public var getPartner: ConnectionGetPartner + public var send: ConnectionSend public var close: ConnectionClose } @@ -13,6 +14,7 @@ extension Connection { isAuthenticated: .live(bindingsConnection), getId: .live(bindingsConnection), getPartner: .live(bindingsConnection), + send: .live(bindingsConnection), close: .live(bindingsConnection) ) } @@ -22,6 +24,7 @@ extension Connection { isAuthenticated: .live(bindingsConnection), getId: .live(bindingsConnection), getPartner: .live(bindingsConnection), + send: .live(bindingsConnection), close: .live(bindingsConnection) ) } @@ -32,6 +35,7 @@ extension Connection { isAuthenticated: .unimplemented, getId: .unimplemented, getPartner: .unimplemented, + send: .unimplemented, close: .unimplemented ) } diff --git a/Sources/ElixxirDAppsSDK/ConnectionSend.swift b/Sources/ElixxirDAppsSDK/ConnectionSend.swift new file mode 100644 index 00000000..7d4061d8 --- /dev/null +++ b/Sources/ElixxirDAppsSDK/ConnectionSend.swift @@ -0,0 +1,37 @@ +import Bindings +import XCTestDynamicOverlay + +public struct ConnectionSend { + public var run: (Int, Data) throws -> MessageSendReport + + public func callAsFunction( + messageType: Int, + payload: Data + ) throws -> MessageSendReport { + try run(messageType, payload) + } +} + +extension ConnectionSend { + public static func live(_ bindingsConnection: BindingsConnection) -> ConnectionSend { + ConnectionSend { messageType, payload in + try MessageSendReport.decode( + bindingsConnection.sendE2E(messageType, payload: payload) + ) + } + } + + public static func live(_ bindingsConnection: BindingsAuthenticatedConnection) -> ConnectionSend { + ConnectionSend { messageType, payload in + try MessageSendReport.decode( + bindingsConnection.sendE2E(messageType, payload: payload) + ) + } + } +} + +extension ConnectionSend { + public static let unimplemented = ConnectionSend( + run: XCTUnimplemented("\(Self.self)") + ) +} diff --git a/Sources/ElixxirDAppsSDK/Legacy/MessageSender.swift b/Sources/ElixxirDAppsSDK/Legacy/MessageSender.swift deleted file mode 100644 index 4e493b13..00000000 --- a/Sources/ElixxirDAppsSDK/Legacy/MessageSender.swift +++ /dev/null @@ -1,46 +0,0 @@ -import Bindings - -public struct MessageSender { - public var send: (Int, Data) throws -> MessageSendReport - - public func callAsFunction( - messageType: Int, - payload: Data - ) throws -> MessageSendReport { - try send(messageType, payload) - } -} - -extension MessageSender { - public static func live( - bindingsConnection: BindingsConnection - ) -> MessageSender { - MessageSender.live(sendE2E: bindingsConnection.sendE2E(_:payload:)) - } - - public static func live( - bindingsAuthenticatedConnection: BindingsAuthenticatedConnection - ) -> MessageSender { - MessageSender.live(sendE2E: bindingsAuthenticatedConnection.sendE2E(_:payload:)) - } - - private static func live( - sendE2E: @escaping (Int, Data) throws -> Data - ) -> MessageSender { - MessageSender { messageType, payload in - let reportData = try sendE2E(messageType, payload) - let decoder = JSONDecoder() - let report = try decoder.decode(MessageSendReport.self, from: reportData) - return report - } - } -} - -#if DEBUG -extension MessageSender { - public static let failing = MessageSender { _, _ in - struct NotImplemented: Error {} - throw NotImplemented() - } -} -#endif -- GitLab