From 4ae1a96adbe700a706f9d78eee1e015101f739df Mon Sep 17 00:00:00 2001
From: Dariusz Rybicki <dariusz@elixxir.io>
Date: Thu, 2 Jun 2022 11:01:10 +0200
Subject: [PATCH] Add Connection and ConnectionMaker

---
 Sources/ElixxirDAppsSDK/Client.swift          |  7 ++-
 Sources/ElixxirDAppsSDK/Connection.swift      | 31 +++++++++++++
 Sources/ElixxirDAppsSDK/ConnectionMaker.swift | 44 +++++++++++++++++++
 3 files changed, 80 insertions(+), 2 deletions(-)
 create mode 100644 Sources/ElixxirDAppsSDK/Connection.swift
 create mode 100644 Sources/ElixxirDAppsSDK/ConnectionMaker.swift

diff --git a/Sources/ElixxirDAppsSDK/Client.swift b/Sources/ElixxirDAppsSDK/Client.swift
index 6cfc8fe9..38e236ac 100644
--- a/Sources/ElixxirDAppsSDK/Client.swift
+++ b/Sources/ElixxirDAppsSDK/Client.swift
@@ -4,6 +4,7 @@ public struct Client {
   public var networkFollower: NetworkFollower
   public var waitForNetwork: NetworkWaiter
   public var makeIdentity: IdentityMaker
+  public var connect: ConnectionMaker
 }
 
 extension Client {
@@ -11,7 +12,8 @@ extension Client {
     Client(
       networkFollower: .live(bindingsClient: bindingsClient),
       waitForNetwork: .live(bindingsClient: bindingsClient),
-      makeIdentity: .live(bindingsClient: bindingsClient)
+      makeIdentity: .live(bindingsClient: bindingsClient),
+      connect: .live(bindingsClient: bindingsClient)
     )
   }
 }
@@ -21,7 +23,8 @@ extension Client {
   public static let failing = Client(
     networkFollower: .failing,
     waitForNetwork: .failing,
-    makeIdentity: .failing
+    makeIdentity: .failing,
+    connect: .failing
   )
 }
 #endif
diff --git a/Sources/ElixxirDAppsSDK/Connection.swift b/Sources/ElixxirDAppsSDK/Connection.swift
new file mode 100644
index 00000000..2633c603
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/Connection.swift
@@ -0,0 +1,31 @@
+import Bindings
+
+public struct Connection {
+  public var isAuthenticated: () -> Bool
+}
+
+extension Connection {
+  public static func live(
+    bindingsConnection: BindingsConnection
+  ) -> Connection {
+    Connection(
+      isAuthenticated: { false }
+    )
+  }
+
+  public static func live(
+    bindingsAuthenticatedConnection: BindingsAuthenticatedConnection
+  ) -> Connection {
+    Connection(
+      isAuthenticated: bindingsAuthenticatedConnection.isAuthenticated
+    )
+  }
+}
+
+#if DEBUG
+extension Connection {
+  public static let failing = Connection(
+    isAuthenticated: { false }
+  )
+}
+#endif
diff --git a/Sources/ElixxirDAppsSDK/ConnectionMaker.swift b/Sources/ElixxirDAppsSDK/ConnectionMaker.swift
new file mode 100644
index 00000000..a898075f
--- /dev/null
+++ b/Sources/ElixxirDAppsSDK/ConnectionMaker.swift
@@ -0,0 +1,44 @@
+import Bindings
+
+public struct ConnectionMaker {
+  public var connect: (Bool, Data, Data) throws -> Connection
+
+  public func callAsFunction(
+    withAuthentication: Bool,
+    recipientContact: Data,
+    myIdentity: Data
+  ) throws -> Connection {
+    try connect(withAuthentication, recipientContact, myIdentity)
+  }
+}
+
+extension ConnectionMaker {
+  public static func live(bindingsClient: BindingsClient) -> ConnectionMaker {
+    ConnectionMaker { withAuthentication, recipientContact, myIdentity in
+      if withAuthentication {
+        return Connection.live(
+          bindingsConnection: try bindingsClient.connect(
+            recipientContact,
+            myIdentity: myIdentity
+          )
+        )
+      } else {
+        return Connection.live(
+          bindingsAuthenticatedConnection: try bindingsClient.connect(
+            withAuthentication: recipientContact,
+            myIdentity: myIdentity
+          )
+        )
+      }
+    }
+  }
+}
+
+#if DEBUG
+extension ConnectionMaker {
+  public static let failing = ConnectionMaker { _, _, _ in
+    struct NotImplemented: Error {}
+    throw NotImplemented()
+  }
+}
+#endif
-- 
GitLab