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
public struct ConnectionMaker {
public var connect: (Bool, Data, Data) throws -> Connection
public var connect: (Bool, Data, Identity) throws -> Connection
public func callAsFunction(
withAuthentication: Bool,
recipientContact: Data,
myIdentity: Data
myIdentity: Identity
) throws -> Connection {
try connect(withAuthentication, recipientContact, myIdentity)
}
......@@ -15,18 +15,20 @@ public struct ConnectionMaker {
extension ConnectionMaker {
public static func live(bindingsClient: BindingsClient) -> ConnectionMaker {
ConnectionMaker { withAuthentication, recipientContact, myIdentity in
let encoder = JSONEncoder()
let myIdentityData = try encoder.encode(myIdentity)
if withAuthentication {
return Connection.live(
bindingsAuthenticatedConnection: try bindingsClient.connect(
withAuthentication: recipientContact,
myIdentity: myIdentity
myIdentity: myIdentityData
)
)
} else {
return Connection.live(
bindingsConnection: try bindingsClient.connect(
recipientContact,
myIdentity: myIdentity
myIdentity: myIdentityData
)
)
}
......
import Bindings
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)
}
}
......@@ -11,13 +11,15 @@ public struct ContactFactsProvider {
extension ContactFactsProvider {
public static let live = ContactFactsProvider { contact in
var error: NSError?
let facts = BindingsGetFactsFromContact(contact, &error)
let factsData = BindingsGetFactsFromContact(contact, &error)
if let error = error {
throw error
}
guard let facts = facts else {
guard let factsData = factsData else {
fatalError("BindingsGetFactsFromContact returned `nil` without providing error")
}
let decoder = JSONDecoder()
let facts = try decoder.decode([Fact].self, from: factsData)
return facts
}
}
......
import Bindings
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)
}
}
extension ContactFactsSetter {
public static let live = ContactFactsSetter { contact, facts in
let encoder = JSONEncoder()
let factsData = try encoder.encode(facts)
var error: NSError?
let updatedContact = BindingsSetFactsOnContact(contact, facts, &error)
let updatedContact = BindingsSetFactsOnContact(contact, factsData, &error)
if let error = error {
throw error
}
......
import Bindings
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)
}
}
extension 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
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()
}
}
......@@ -11,7 +11,9 @@ public struct IdentityMaker {
extension IdentityMaker {
public static func live(bindingsClient: BindingsClient) -> 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
public struct MessageDeliveryWaiter {
public enum Result: Equatable {
case delivered(roundResults: Data)
case delivered(roundResults: [Int])
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(
roundList: Data,
report: MessageSendReport,
timeoutMS: Int,
callback: @escaping (Result) -> Void
) throws -> Void {
try wait(roundList, timeoutMS, callback)
) throws {
try wait(report, timeoutMS, callback)
}
}
extension 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(
forMessageDelivery: roundList,
forMessageDelivery: reportData,
mdc: Callback(onCallback: callback),
timeoutMS: timeoutMS
)
......@@ -38,8 +40,14 @@ private final class Callback: NSObject, BindingsMessageDeliveryCallbackProtocol
let onCallback: (MessageDeliveryWaiter.Result) -> Void
func eventCallback(_ delivered: Bool, timedOut: Bool, roundResults: Data?) {
if delivered, !timedOut, let roundResults = roundResults {
if delivered, !timedOut, let roundResultsData = 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 {
return onCallback(.notDelivered(timedOut: timedOut))
......
import Bindings
public struct MessageListener {
public var listen: (Int, String, @escaping (Data) -> Void) -> Void
public var listen: (Int, String, @escaping (Message) -> Void) -> Void
public func callAsFunction(
messageType: Int,
listenerName: String = "MessageListener",
callback: @escaping (Data) -> Void
callback: @escaping (Message) -> Void
) {
listen(messageType, listenerName, callback)
}
......@@ -39,18 +39,25 @@ extension MessageListener {
}
private class Listener: NSObject, BindingsListenerProtocol {
init(listenerName: String, onHear: @escaping (Data) -> Void) {
init(listenerName: String, onHear: @escaping (Message) -> Void) {
self.listenerName = listenerName
self.onHear = onHear
super.init()
}
let listenerName: String
let onHear: (Data) -> Void
let onHear: (Message) -> Void
let decoder = JSONDecoder()
func hear(_ item: Data?) {
guard let item = item else { return }
onHear(item)
guard let item = item else {
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 {
......
import Bindings
public struct MessageSender {
public var send: (Int, Data) throws -> Data
public var send: (Int, Data) throws -> MessageSendReport
public func callAsFunction(
messageType: Int,
payload: Data
) throws -> Data {
) throws -> MessageSendReport {
try send(messageType, payload)
}
}
......@@ -28,7 +28,10 @@ extension MessageSender {
sendE2E: @escaping (Int, Data) throws -> Data
) -> MessageSender {
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
public struct RestlikeRequestSender {
public var send: (Int, Int, Data) throws -> Data
public var send: (Int, Int, RestlikeMessage) throws -> RestlikeMessage
public func callAsFunction(
clientId: Int,
connectionId: Int,
request: Data
) throws -> Data {
request: RestlikeMessage
) throws -> RestlikeMessage {
try send(clientId, connectionId, request)
}
}
......@@ -15,20 +15,24 @@ public struct RestlikeRequestSender {
extension RestlikeRequestSender {
public static func live(authenticated: Bool) -> RestlikeRequestSender {
RestlikeRequestSender { clientId, connectionId, request in
let encoder = JSONEncoder()
let requestData = try encoder.encode(request)
var error: NSError?
let response: Data?
let responseData: Data?
if authenticated {
response = BindingsRestlikeRequestAuth(clientId, connectionId, request, &error)
responseData = BindingsRestlikeRequestAuth(clientId, connectionId, requestData, &error)
} else {
response = BindingsRestlikeRequest(clientId, connectionId, request, &error)
responseData = BindingsRestlikeRequest(clientId, connectionId, requestData, &error)
}
if let error = error {
throw error
}
guard let response = response else {
guard let responseData = responseData else {
let functionName = "BindingsRestlikeRequest\(authenticated ? "Auth" : "")"
fatalError("\(functionName) returned `nil` without providing error")
}
let decoder = JSONDecoder()
let response = try decoder.decode(RestlikeMessage.self, from: responseData)
return response
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment