diff --git a/Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift b/Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift new file mode 100644 index 0000000000000000000000000000000000000000..597f2b96b97de4d92947250acdc21265220ff47f --- /dev/null +++ b/Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift @@ -0,0 +1,42 @@ +import Bindings +import XCTestDynamicOverlay + +public struct AsyncRequestRestlike { + public var run: (Int, Data, RestlikeMessage, Data, RestlikeCallback) throws -> Void + + public func callAsFunction( + e2eId: Int, + recipient: Data, + request: RestlikeMessage, + paramsJSON: Data, + callback: RestlikeCallback + ) throws { + try run(e2eId, recipient, request, paramsJSON, callback) + } +} + +extension AsyncRequestRestlike { + public static let live = AsyncRequestRestlike { e2dId, recipient, request, paramsJSON, callback in + var error: NSError? + let result = BindingsAsyncRequestRestLike( + e2dId, + recipient, + try request.encode(), + paramsJSON, + callback.makeBindingsRestlikeCallback(), + &error + ) + if let error = error { + throw error + } + guard result else { + fatalError("BindingsAsyncRequestRestLike returned `false` without providing error") + } + } +} + +extension AsyncRequestRestlike { + public static let unimplemented = AsyncRequestRestlike( + run: XCTUnimplemented("\(Self.self)") + ) +}