From ff5824fbdc2c8bc1aa58b10a74bb119bf4984366 Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Tue, 26 Jul 2022 13:39:53 +0100
Subject: [PATCH] Add AsyncRequestRestlike functor

---
 .../AsyncRequestRestlike.swift                | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift

diff --git a/Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift b/Sources/ElixxirDAppsSDK/AsyncRequestRestlike.swift
new file mode 100644
index 00000000..597f2b96
--- /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)")
+  )
+}
-- 
GitLab