Skip to content
Snippets Groups Projects
Commit 7f6c57f1 authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Remove wrappers of removed Bindings API

parent 67ab30a9
No related branches found
No related tags found
2 merge requests!128Update Bindings,!102Release 1.0.0
Showing
with 0 additions and 497 deletions
import Bindings
import XCTestDynamicOverlay
public struct BroadcastListener {
public init(handle: @escaping (Result<BroadcastMessage, NSError>) -> Void) {
self.handle = handle
}
public var handle: (Result<BroadcastMessage, NSError>) -> Void
}
extension BroadcastListener {
public static let unimplemented = BroadcastListener(
handle: XCTUnimplemented("\(Self.self)")
)
}
extension BroadcastListener {
func makeBindingsBroadcastListener() -> BindingsBroadcastListenerProtocol {
class CallbackObject: NSObject, BindingsBroadcastListenerProtocol {
init(_ callback: BroadcastListener) {
self.callback = callback
}
let callback: BroadcastListener
func callback(_ p0: Data?, p1: Error?) {
if let error = p1 {
callback.handle(.failure(error as NSError))
} else if let data = p0 {
do {
callback.handle(.success(try BroadcastMessage.decode(data)))
} catch {
callback.handle(.failure(error as NSError))
}
} else {
fatalError("BindingsBroadcastListener received `nil` data and `nil` error")
}
}
}
return CallbackObject(self)
}
}
import Bindings
public struct Channel {
public var broadcast: ChannelBroadcast
public var broadcastAsymmetric: ChannelBroadcastAsymmetric
public var get: ChannelGet
public var listen: ChannelListen
public var maxAsymmetricPayloadSize: ChannelMaxAsymmetricPayloadSize
public var maxPayloadSize: ChannelMaxPayloadSize
public var stop: ChannelStop
}
extension Channel {
public static func live(_ bindingsChannel: BindingsChannel) -> Channel {
Channel(
broadcast: .live(bindingsChannel),
broadcastAsymmetric: .live(bindingsChannel),
get: .live(bindingsChannel),
listen: .live(bindingsChannel),
maxAsymmetricPayloadSize: .live(bindingsChannel),
maxPayloadSize: .live(bindingsChannel),
stop: .live(bindingsChannel)
)
}
}
extension Channel {
public static let unimplemented = Channel(
broadcast: .unimplemented,
broadcastAsymmetric: .unimplemented,
get: .unimplemented,
listen: .unimplemented,
maxAsymmetricPayloadSize: .unimplemented,
maxPayloadSize: .unimplemented,
stop: .unimplemented
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelBroadcast {
public var run: (Data) throws -> BroadcastReport
public func callAsFunction(_ payload: Data) throws -> BroadcastReport {
try run(payload)
}
}
extension ChannelBroadcast {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelBroadcast {
ChannelBroadcast { payload in
let reportData = try bindingsChannel.broadcast(payload)
return try BroadcastReport.decode(reportData)
}
}
}
extension ChannelBroadcast {
public static let unimplemented = ChannelBroadcast(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelBroadcastAsymmetric {
public var run: (Data, Data) throws -> BroadcastReport
public func callAsFunction(
payload: Data,
privateKey: Data
) throws -> BroadcastReport {
try run(payload, privateKey)
}
}
extension ChannelBroadcastAsymmetric {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelBroadcastAsymmetric {
ChannelBroadcastAsymmetric { payload, privateKey in
let reportData = try bindingsChannel.broadcastAsymmetric(payload, pk: privateKey)
return try BroadcastReport.decode(reportData)
}
}
}
extension ChannelBroadcastAsymmetric {
public static let unimplemented = ChannelBroadcastAsymmetric(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelGet {
public var run: () throws -> ChannelDef
public func callAsFunction() throws -> ChannelDef {
try run()
}
}
extension ChannelGet {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelGet {
ChannelGet {
try ChannelDef.decode(bindingsChannel.get())
}
}
}
extension ChannelGet {
public static let unimplemented = ChannelGet(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelListen {
public var run: (Int, BroadcastListener) throws -> Void
public func callAsFunction(
method: Int,
callback: BroadcastListener
) throws {
try run(method, callback)
}
}
extension ChannelListen {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelListen {
ChannelListen { method, callback in
try bindingsChannel.listen(
callback.makeBindingsBroadcastListener(),
method: method
)
}
}
}
extension ChannelListen {
public static let unimplemented = ChannelListen(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelMaxAsymmetricPayloadSize {
public var run: () -> Int
public func callAsFunction() -> Int {
run()
}
}
extension ChannelMaxAsymmetricPayloadSize {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelMaxAsymmetricPayloadSize {
ChannelMaxAsymmetricPayloadSize(run: bindingsChannel.maxAsymmetricPayloadSize)
}
}
extension ChannelMaxAsymmetricPayloadSize {
public static let unimplemented = ChannelMaxAsymmetricPayloadSize(
run: XCTUnimplemented("\(Self.self)", placeholder: 0)
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelMaxPayloadSize {
public var run: () -> Int
public func callAsFunction() -> Int {
run()
}
}
extension ChannelMaxPayloadSize {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelMaxPayloadSize {
ChannelMaxPayloadSize(run: bindingsChannel.maxPayloadSize)
}
}
extension ChannelMaxPayloadSize {
public static let unimplemented = ChannelMaxPayloadSize(
run: XCTUnimplemented("\(Self.self)", placeholder: 0)
)
}
import Bindings
import XCTestDynamicOverlay
public struct ChannelStop {
public var run: () -> Void
public func callAsFunction() {
run()
}
}
extension ChannelStop {
public static func live(_ bindingsChannel: BindingsChannel) -> ChannelStop {
ChannelStop(run: bindingsChannel.stop)
}
}
extension ChannelStop {
public static let unimplemented = ChannelStop(
run: XCTUnimplemented("\(Self.self)")
)
}
import Bindings
import XCTestDynamicOverlay
public struct NewBroadcastChannel {
public var run: (Int, ChannelDef) throws -> Channel
public func callAsFunction(
cMixId: Int,
channelDef: ChannelDef
) throws -> Channel {
try run(cMixId, channelDef)
}
}
extension NewBroadcastChannel {
public static let live = NewBroadcastChannel { cMixId, channelDef in
var error: NSError?
let bindingsChannel = BindingsNewBroadcastChannel(
cMixId,
try channelDef.encode(),
&error
)
if let error = error {
throw error
}
guard let bindingsChannel = bindingsChannel else {
fatalError("BindingsNewBroadcastChannel returned `nil` without providing error")
}
return .live(bindingsChannel)
}
}
extension NewBroadcastChannel {
public static let unimplemented = NewBroadcastChannel(
run: XCTUnimplemented("\(Self.self)")
)
}
import Foundation
public struct BroadcastMessage: Equatable {
public init(
roundId: Int,
ephId: [Int],
roundURL: String,
payload: Data
) {
self.roundId = roundId
self.ephId = ephId
self.roundURL = roundURL
self.payload = payload
}
public var roundId: Int
public var ephId: [Int]
public var roundURL: String
public var payload: Data
}
extension BroadcastMessage: Codable {
enum CodingKeys: String, CodingKey {
case roundId = "RoundID"
case ephId = "EphID"
case roundURL = "RoundURL"
case payload = "Payload"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import Foundation
public struct BroadcastReport: Equatable {
public init(
rounds: [Int],
ephId: [Int],
roundURL: String
) {
self.rounds = rounds
self.ephId = ephId
self.roundURL = roundURL
}
public var rounds: [Int]
public var ephId: [Int]
public var roundURL: String
}
extension BroadcastReport: Codable {
enum CodingKeys: String, CodingKey {
case rounds = "Rounds"
case ephId = "EphID"
case roundURL = "RoundURL"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import Foundation
public struct ChannelDef: Equatable {
public init(
name: String,
description: String,
salt: Data,
pubKey: Data
) {
self.name = name
self.description = description
self.salt = salt
self.pubKey = pubKey
}
public var name: String
public var description: String
public var salt: Data
public var pubKey: Data
}
extension ChannelDef: Codable {
enum CodingKeys: String, CodingKey {
case name = "Name"
case description = "Description"
case salt = "Salt"
case pubKey = "PubKey"
}
public static func decode(_ data: Data) throws -> Self {
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
try JSONEncoder().encode(self)
}
}
import CustomDump
import XCTest
@testable import XXClient
final class BroadcastMessageTests: XCTestCase {
func testCoding() throws {
let roundId: Int = 42
let ephId: [Int] = [0, 0, 0, 0, 0, 0, 24, 61]
let roundURL = "https://dashboard.xx.network/rounds/25?xxmessenger=true"
let payloadB64 = "SGVsbG8sIGJyb2FkY2FzdCBmcmllbmRzIQ=="
let jsonString = """
{
"RoundID": \(roundId),
"EphID": [\(ephId.map { "\($0)" }.joined(separator: ", "))],
"RoundURL": "\(roundURL)",
"Payload": "\(payloadB64)"
}
"""
let jsonData = jsonString.data(using: .utf8)!
let model = try BroadcastMessage.decode(jsonData)
XCTAssertNoDifference(model, BroadcastMessage(
roundId: roundId,
ephId: ephId,
roundURL: roundURL,
payload: Data(base64Encoded: payloadB64)!
))
let encodedModel = try model.encode()
let decodedModel = try BroadcastMessage.decode(encodedModel)
XCTAssertNoDifference(decodedModel, model)
}
}
import CustomDump
import XCTest
@testable import XXClient
final class BroadcastReportTests: XCTestCase {
func testCoding() throws {
let rounds: [Int] = [25, 26, 29]
let ephId: [Int] = [0, 0, 0, 0, 0, 0, 24, 61]
let roundURL = "https://dashboard.xx.network/rounds/25?xxmessenger=true"
let jsonString = """
{
"Rounds": [\(rounds.map { "\($0)" }.joined(separator: ", "))],
"EphID": [\(ephId.map { "\($0)" }.joined(separator: ", "))],
"RoundURL": "\(roundURL)"
}
"""
let jsonData = jsonString.data(using: .utf8)!
let model = try BroadcastReport.decode(jsonData)
XCTAssertNoDifference(model, BroadcastReport(
rounds: rounds,
ephId: ephId,
roundURL: roundURL
))
let encodedModel = try model.encode()
let decodedModel = try BroadcastReport.decode(encodedModel)
XCTAssertNoDifference(decodedModel, model)
}
}
import CustomDump
import XCTest
@testable import XXClient
final class ChannelDefTests: XCTestCase {
func testCoding() throws {
let name = "My broadcast channel"
let description = "A broadcast channel for me to test things"
let saltB64 = "gpUqW7N22sffMXsvPLE7BA=="
let pubKeyB64 = "LS0tLS1CRUdJTiBSU0EgUFVCTElDIEtFWS0tLS0tCk1DZ0NJUUN2YkZVckJKRFpqT3Y0Y0MvUHZZdXNvQkFtUTFkb3Znb044aHRuUjA2T3F3SURBUUFCCi0tLS0tRU5EIFJTQSBQVUJMSUMgS0VZLS0tLS0="
let jsonString = """
{
"Name": "\(name)",
"Description": "\(description)",
"Salt": "\(saltB64)",
"PubKey": "\(pubKeyB64)"
}
"""
let jsonData = jsonString.data(using: .utf8)!
let model = try ChannelDef.decode(jsonData)
XCTAssertNoDifference(model, ChannelDef(
name: name,
description: description,
salt: Data(base64Encoded: saltB64)!,
pubKey: Data(base64Encoded: pubKeyB64)!
))
let encodedModel = try model.encode()
let decodedModel = try ChannelDef.decode(encodedModel)
XCTAssertNoDifference(decodedModel, model)
}
}
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