Skip to content
Snippets Groups Projects
Commit fa1aa40b authored by Ahmed Shehata's avatar Ahmed Shehata
Browse files

Merge branch 'feature/api-models' into 'main'

Use codable models in API

See merge request elixxir/elixxir-dapps-sdk-swift!12
parents f02f3d7a 9c8cba1b
No related branches found
No related tags found
1 merge request!12Use codable models in API
import Bindings import Bindings
public struct ConnectionMaker { public struct ConnectionMaker {
public var connect: (Bool, Data, Data) throws -> Connection public var connect: (Bool, Data, Identity) throws -> Connection
public func callAsFunction( public func callAsFunction(
withAuthentication: Bool, withAuthentication: Bool,
recipientContact: Data, recipientContact: Data,
myIdentity: Data myIdentity: Identity
) throws -> Connection { ) throws -> Connection {
try connect(withAuthentication, recipientContact, myIdentity) try connect(withAuthentication, recipientContact, myIdentity)
} }
...@@ -15,18 +15,20 @@ public struct ConnectionMaker { ...@@ -15,18 +15,20 @@ public struct ConnectionMaker {
extension ConnectionMaker { extension ConnectionMaker {
public static func live(bindingsClient: BindingsClient) -> ConnectionMaker { public static func live(bindingsClient: BindingsClient) -> ConnectionMaker {
ConnectionMaker { withAuthentication, recipientContact, myIdentity in ConnectionMaker { withAuthentication, recipientContact, myIdentity in
let encoder = JSONEncoder()
let myIdentityData = try encoder.encode(myIdentity)
if withAuthentication { if withAuthentication {
return Connection.live( return Connection.live(
bindingsAuthenticatedConnection: try bindingsClient.connect( bindingsAuthenticatedConnection: try bindingsClient.connect(
withAuthentication: recipientContact, withAuthentication: recipientContact,
myIdentity: myIdentity myIdentity: myIdentityData
) )
) )
} else { } else {
return Connection.live( return Connection.live(
bindingsConnection: try bindingsClient.connect( bindingsConnection: try bindingsClient.connect(
recipientContact, recipientContact,
myIdentity: myIdentity myIdentity: myIdentityData
) )
) )
} }
......
import Bindings import Bindings
public struct ContactFactsProvider { public struct ContactFactsProvider {
public var get: (Data) throws -> Data public var get: (Data) throws -> [Fact]
public func callAsFunction(contact: Data) throws -> Data { public func callAsFunction(contact: Data) throws -> [Fact] {
try get(contact) try get(contact)
} }
} }
...@@ -11,13 +11,15 @@ public struct ContactFactsProvider { ...@@ -11,13 +11,15 @@ public struct ContactFactsProvider {
extension ContactFactsProvider { extension ContactFactsProvider {
public static let live = ContactFactsProvider { contact in public static let live = ContactFactsProvider { contact in
var error: NSError? var error: NSError?
let facts = BindingsGetFactsFromContact(contact, &error) let factsData = BindingsGetFactsFromContact(contact, &error)
if let error = error { if let error = error {
throw error throw error
} }
guard let facts = facts else { guard let factsData = factsData else {
fatalError("BindingsGetFactsFromContact returned `nil` without providing error") fatalError("BindingsGetFactsFromContact returned `nil` without providing error")
} }
let decoder = JSONDecoder()
let facts = try decoder.decode([Fact].self, from: factsData)
return facts return facts
} }
} }
......
import Bindings import Bindings
public struct ContactFactsSetter { public struct ContactFactsSetter {
public var set: (Data, Data) throws -> Data public var set: (Data, [Fact]) throws -> Data
public func callAsFunction(contact: Data, facts: Data) throws -> Data { public func callAsFunction(
contact: Data,
facts: [Fact]
) throws -> Data {
try set(contact, facts) try set(contact, facts)
} }
} }
extension ContactFactsSetter { extension ContactFactsSetter {
public static let live = ContactFactsSetter { contact, facts in public static let live = ContactFactsSetter { contact, facts in
let encoder = JSONEncoder()
let factsData = try encoder.encode(facts)
var error: NSError? var error: NSError?
let updatedContact = BindingsSetFactsOnContact(contact, facts, &error) let updatedContact = BindingsSetFactsOnContact(contact, factsData, &error)
if let error = error { if let error = error {
throw error throw error
} }
......
import Bindings import Bindings
public struct ContactFromIdentityProvider { public struct ContactFromIdentityProvider {
public var get: (Data) throws -> Data public var get: (Identity) throws -> Data
public func callAsFunction(identity: Data) throws -> Data { public func callAsFunction(identity: Identity) throws -> Data {
try get(identity) try get(identity)
} }
} }
extension ContactFromIdentityProvider { extension ContactFromIdentityProvider {
public static func live(bindingsClient: BindingsClient) -> ContactFromIdentityProvider { public static func live(bindingsClient: BindingsClient) -> ContactFromIdentityProvider {
ContactFromIdentityProvider(get: bindingsClient.getContactFromIdentity(_:)) ContactFromIdentityProvider { identity in
let encoder = JSONEncoder()
let identityData = try encoder.encode(identity)
let contactData = try bindingsClient.getContactFromIdentity(identityData)
return contactData
}
} }
} }
......
import Bindings import Bindings
public struct IdentityMaker { public struct IdentityMaker {
public var make: () throws -> Data public var make: () throws -> Identity
public func callAsFunction() throws -> Data { public func callAsFunction() throws -> Identity {
try make() try make()
} }
} }
...@@ -11,7 +11,9 @@ public struct IdentityMaker { ...@@ -11,7 +11,9 @@ public struct IdentityMaker {
extension IdentityMaker { extension IdentityMaker {
public static func live(bindingsClient: BindingsClient) -> IdentityMaker { public static func live(bindingsClient: BindingsClient) -> IdentityMaker {
IdentityMaker { IdentityMaker {
try bindingsClient.makeIdentity() let data = try bindingsClient.makeIdentity()
let decoder = JSONDecoder()
return try decoder.decode(Identity.self, from: data)
} }
} }
} }
......
...@@ -2,26 +2,28 @@ import Bindings ...@@ -2,26 +2,28 @@ import Bindings
public struct MessageDeliveryWaiter { public struct MessageDeliveryWaiter {
public enum Result: Equatable { public enum Result: Equatable {
case delivered(roundResults: Data) case delivered(roundResults: [Int])
case notDelivered(timedOut: Bool) case notDelivered(timedOut: Bool)
} }
public var wait: (Data, Int, @escaping (Result) -> Void) throws -> Void public var wait: (MessageSendReport, Int, @escaping (Result) -> Void) throws -> Void
public func callAsFunction( public func callAsFunction(
roundList: Data, report: MessageSendReport,
timeoutMS: Int, timeoutMS: Int,
callback: @escaping (Result) -> Void callback: @escaping (Result) -> Void
) throws -> Void { ) throws {
try wait(roundList, timeoutMS, callback) try wait(report, timeoutMS, callback)
} }
} }
extension MessageDeliveryWaiter { extension MessageDeliveryWaiter {
public static func live(bindingsClient: BindingsClient) -> MessageDeliveryWaiter { public static func live(bindingsClient: BindingsClient) -> MessageDeliveryWaiter {
MessageDeliveryWaiter { roundList, timeoutMS, callback in MessageDeliveryWaiter { report, timeoutMS, callback in
let encoder = JSONEncoder()
let reportData = try encoder.encode(report)
try bindingsClient.wait( try bindingsClient.wait(
forMessageDelivery: roundList, forMessageDelivery: reportData,
mdc: Callback(onCallback: callback), mdc: Callback(onCallback: callback),
timeoutMS: timeoutMS timeoutMS: timeoutMS
) )
...@@ -38,8 +40,14 @@ private final class Callback: NSObject, BindingsMessageDeliveryCallbackProtocol ...@@ -38,8 +40,14 @@ private final class Callback: NSObject, BindingsMessageDeliveryCallbackProtocol
let onCallback: (MessageDeliveryWaiter.Result) -> Void let onCallback: (MessageDeliveryWaiter.Result) -> Void
func eventCallback(_ delivered: Bool, timedOut: Bool, roundResults: Data?) { func eventCallback(_ delivered: Bool, timedOut: Bool, roundResults: Data?) {
if delivered, !timedOut, let roundResults = roundResults { if delivered, !timedOut, let roundResultsData = roundResults {
return onCallback(.delivered(roundResults: roundResults)) let decoder = JSONDecoder()
do {
let roundResults = try decoder.decode([Int].self, from: roundResultsData)
return onCallback(.delivered(roundResults: roundResults))
} catch {
fatalError("BindingsMessageDeliveryCallback roundResults decoding error: \(error)")
}
} }
if !delivered, roundResults == nil { if !delivered, roundResults == nil {
return onCallback(.notDelivered(timedOut: timedOut)) return onCallback(.notDelivered(timedOut: timedOut))
......
import Bindings import Bindings
public struct MessageListener { public struct MessageListener {
public var listen: (Int, String, @escaping (Data) -> Void) -> Void public var listen: (Int, String, @escaping (Message) -> Void) -> Void
public func callAsFunction( public func callAsFunction(
messageType: Int, messageType: Int,
listenerName: String = "MessageListener", listenerName: String = "MessageListener",
callback: @escaping (Data) -> Void callback: @escaping (Message) -> Void
) { ) {
listen(messageType, listenerName, callback) listen(messageType, listenerName, callback)
} }
...@@ -39,18 +39,25 @@ extension MessageListener { ...@@ -39,18 +39,25 @@ extension MessageListener {
} }
private class Listener: NSObject, BindingsListenerProtocol { private class Listener: NSObject, BindingsListenerProtocol {
init(listenerName: String, onHear: @escaping (Data) -> Void) { init(listenerName: String, onHear: @escaping (Message) -> Void) {
self.listenerName = listenerName self.listenerName = listenerName
self.onHear = onHear self.onHear = onHear
super.init() super.init()
} }
let listenerName: String let listenerName: String
let onHear: (Data) -> Void let onHear: (Message) -> Void
let decoder = JSONDecoder()
func hear(_ item: Data?) { func hear(_ item: Data?) {
guard let item = item else { return } guard let item = item else {
onHear(item) fatalError("BindingsListenerProtocol.hear received `nil`")
}
do {
onHear(try decoder.decode(Message.self, from: item))
} catch {
fatalError("Message decoding failed with error: \(error)")
}
} }
func name() -> String { func name() -> String {
......
import Bindings import Bindings
public struct MessageSender { public struct MessageSender {
public var send: (Int, Data) throws -> Data public var send: (Int, Data) throws -> MessageSendReport
public func callAsFunction( public func callAsFunction(
messageType: Int, messageType: Int,
payload: Data payload: Data
) throws -> Data { ) throws -> MessageSendReport {
try send(messageType, payload) try send(messageType, payload)
} }
} }
...@@ -28,7 +28,10 @@ extension MessageSender { ...@@ -28,7 +28,10 @@ extension MessageSender {
sendE2E: @escaping (Int, Data) throws -> Data sendE2E: @escaping (Int, Data) throws -> Data
) -> MessageSender { ) -> MessageSender {
MessageSender { messageType, payload in MessageSender { messageType, payload in
try sendE2E(messageType, payload) let reportData = try sendE2E(messageType, payload)
let decoder = JSONDecoder()
let report = try decoder.decode(MessageSendReport.self, from: reportData)
return report
} }
} }
} }
......
import Bindings import Bindings
public struct RestlikeRequestSender { public struct RestlikeRequestSender {
public var send: (Int, Int, Data) throws -> Data public var send: (Int, Int, RestlikeMessage) throws -> RestlikeMessage
public func callAsFunction( public func callAsFunction(
clientId: Int, clientId: Int,
connectionId: Int, connectionId: Int,
request: Data request: RestlikeMessage
) throws -> Data { ) throws -> RestlikeMessage {
try send(clientId, connectionId, request) try send(clientId, connectionId, request)
} }
} }
...@@ -15,20 +15,24 @@ public struct RestlikeRequestSender { ...@@ -15,20 +15,24 @@ public struct RestlikeRequestSender {
extension RestlikeRequestSender { extension RestlikeRequestSender {
public static func live(authenticated: Bool) -> RestlikeRequestSender { public static func live(authenticated: Bool) -> RestlikeRequestSender {
RestlikeRequestSender { clientId, connectionId, request in RestlikeRequestSender { clientId, connectionId, request in
let encoder = JSONEncoder()
let requestData = try encoder.encode(request)
var error: NSError? var error: NSError?
let response: Data? let responseData: Data?
if authenticated { if authenticated {
response = BindingsRestlikeRequestAuth(clientId, connectionId, request, &error) responseData = BindingsRestlikeRequestAuth(clientId, connectionId, requestData, &error)
} else { } else {
response = BindingsRestlikeRequest(clientId, connectionId, request, &error) responseData = BindingsRestlikeRequest(clientId, connectionId, requestData, &error)
} }
if let error = error { if let error = error {
throw error throw error
} }
guard let response = response else { guard let responseData = responseData else {
let functionName = "BindingsRestlikeRequest\(authenticated ? "Auth" : "")" let functionName = "BindingsRestlikeRequest\(authenticated ? "Auth" : "")"
fatalError("\(functionName) returned `nil` without providing error") fatalError("\(functionName) returned `nil` without providing error")
} }
let decoder = JSONDecoder()
let response = try decoder.decode(RestlikeMessage.self, from: responseData)
return response return response
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment