mirror of https://github.com/oxen-io/session-ios
Clean up TestCallServer
parent
8b187641b8
commit
49d93b9cfd
@ -1,51 +0,0 @@
|
|||||||
import Foundation
|
|
||||||
import PromiseKit
|
|
||||||
|
|
||||||
public struct RoomInfo {
|
|
||||||
public let roomID: String
|
|
||||||
public let wssURL: String
|
|
||||||
public let wssPostURL: String
|
|
||||||
public let clientID: String
|
|
||||||
public let isInitiator: String
|
|
||||||
public let messages: [String]?
|
|
||||||
}
|
|
||||||
|
|
||||||
public enum MockCallServer {
|
|
||||||
|
|
||||||
private static func getRoomURL(for roomID: String) -> String {
|
|
||||||
let base = TestCallConfig.defaultServerURL + "/join/"
|
|
||||||
return base + "\(roomID)"
|
|
||||||
}
|
|
||||||
private static func getLeaveURL(roomID: String, userID: String) -> String {
|
|
||||||
let base = TestCallConfig.defaultServerURL + "/leave/"
|
|
||||||
return base + "\(roomID)/\(userID)"
|
|
||||||
}
|
|
||||||
private static func getMessageURL(roomID: String, userID: String) -> String {
|
|
||||||
let base = TestCallConfig.defaultServerURL + "/message/"
|
|
||||||
return base + "\(roomID)/\(userID)"
|
|
||||||
}
|
|
||||||
|
|
||||||
public static func join(roomID: String) -> Promise<RoomInfo> {
|
|
||||||
HTTP.execute(.post, getRoomURL(for: roomID)).map2 { json in
|
|
||||||
guard let status = json["result"] as? String else { throw HTTP.Error.invalidJSON }
|
|
||||||
if status == "FULL" { preconditionFailure() }
|
|
||||||
guard let info = json["params"] as? JSON,
|
|
||||||
let roomID = info["room_id"] as? String,
|
|
||||||
let wssURL = info["wss_url"] as? String,
|
|
||||||
let wssPostURL = info["wss_post_url"] as? String,
|
|
||||||
let clientID = info["client_id"] as? String,
|
|
||||||
let isInitiator = info["is_initiator"] as? String else { throw HTTP.Error.invalidJSON }
|
|
||||||
let messages = info["messages"] as? [String]
|
|
||||||
return RoomInfo(roomID: roomID, wssURL: wssURL, wssPostURL: wssPostURL,
|
|
||||||
clientID: clientID, isInitiator: isInitiator, messages: messages)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static func leave(roomID: String, userID: String) -> Promise<Void> {
|
|
||||||
return HTTP.execute(.post, getLeaveURL(roomID: roomID, userID: userID)).map2 { _ in }
|
|
||||||
}
|
|
||||||
|
|
||||||
public static func send(_ message: Data, roomID: String, userID: String) -> Promise<Void> {
|
|
||||||
HTTP.execute(.post, getMessageURL(roomID: roomID, userID: userID), body: message).map2 { _ in }
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
public struct RoomInfo {
|
||||||
|
public let roomID: String
|
||||||
|
public let wssURL: String
|
||||||
|
public let wssPostURL: String
|
||||||
|
public let clientID: String
|
||||||
|
public let isInitiator: Bool
|
||||||
|
public let messages: [String]?
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
import Foundation
|
||||||
|
import PromiseKit
|
||||||
|
|
||||||
|
public enum TestCallServer {
|
||||||
|
|
||||||
|
public enum Error : LocalizedError {
|
||||||
|
case roomFull
|
||||||
|
|
||||||
|
public var errorDescription: String? {
|
||||||
|
switch self {
|
||||||
|
case .roomFull: return "The room is full."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func join(roomID: String) -> Promise<RoomInfo> {
|
||||||
|
let url = "\(TestCallConfig.defaultServerURL)/join/\(roomID)"
|
||||||
|
return HTTP.execute(.post, url).map2 { json in
|
||||||
|
guard let status = json["result"] as? String else { throw HTTP.Error.invalidJSON }
|
||||||
|
guard status != "FULL" else { throw Error.roomFull }
|
||||||
|
guard let info = json["params"] as? JSON,
|
||||||
|
let roomID = info["room_id"] as? String,
|
||||||
|
let wssURL = info["wss_url"] as? String,
|
||||||
|
let wssPostURL = info["wss_post_url"] as? String,
|
||||||
|
let clientID = info["client_id"] as? String else { throw HTTP.Error.invalidJSON }
|
||||||
|
let isInitiator: Bool
|
||||||
|
if let bool = info["is_initiator"] as? Bool {
|
||||||
|
isInitiator = bool
|
||||||
|
} else if let string = info["is_initiator"] as? String {
|
||||||
|
isInitiator = (string == "true")
|
||||||
|
} else {
|
||||||
|
throw HTTP.Error.invalidJSON
|
||||||
|
}
|
||||||
|
let messages = info["messages"] as? [String]
|
||||||
|
return RoomInfo(roomID: roomID, wssURL: wssURL, wssPostURL: wssPostURL,
|
||||||
|
clientID: clientID, isInitiator: isInitiator, messages: messages)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func leave(roomID: String, userID: String) -> Promise<Void> {
|
||||||
|
let url = "\(TestCallConfig.defaultServerURL)/leave/\(roomID)/\(userID)"
|
||||||
|
return HTTP.execute(.post, url).map2 { _ in }
|
||||||
|
}
|
||||||
|
|
||||||
|
public static func send(_ message: Data, roomID: String, userID: String) -> Promise<Void> {
|
||||||
|
let url = "\(TestCallConfig.defaultServerURL)/message/\(roomID)/\(userID)"
|
||||||
|
return HTTP.execute(.post, url, body: message).map2 { _ in }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue