2022-01-29 18:59:42 +01:00
|
|
|
import Foundation
|
|
|
|
import CryptoKit
|
|
|
|
|
|
|
|
struct Client {
|
|
|
|
|
|
|
|
let server: URL
|
|
|
|
|
|
|
|
private let delegate = NeverCacheDelegate()
|
|
|
|
|
|
|
|
init(server: URL) {
|
|
|
|
self.server = server
|
|
|
|
}
|
|
|
|
|
2022-05-01 14:07:43 +02:00
|
|
|
func deviceStatus(authToken: Data) async -> ClientState {
|
|
|
|
await send(path: .getDeviceStatus, data: authToken).state
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
2022-05-01 14:07:43 +02:00
|
|
|
func send(_ message: Message, authToken: Data) async -> (state: ClientState, response: Message?) {
|
|
|
|
let serverMessage = ServerMessage(authToken: authToken, message: message)
|
|
|
|
return await send(path: .postMessage, data: serverMessage.encoded)
|
|
|
|
}
|
|
|
|
|
|
|
|
private func send(path: RouteAPI, data: Data) async -> (state: ClientState, response: Message?) {
|
|
|
|
let url = server.appendingPathComponent(path.rawValue)
|
2022-01-29 18:59:42 +01:00
|
|
|
var request = URLRequest(url: url)
|
2022-05-01 14:07:43 +02:00
|
|
|
request.httpBody = data
|
2022-01-29 18:59:42 +01:00
|
|
|
request.httpMethod = "POST"
|
2022-04-09 17:43:33 +02:00
|
|
|
guard let data = await fulfill(request) else {
|
|
|
|
return (.deviceNotAvailable(.serverNotReached), nil)
|
|
|
|
}
|
|
|
|
guard let byte = data.first else {
|
|
|
|
return (.internalError("Empty response"), nil)
|
|
|
|
}
|
|
|
|
guard let status = MessageResult(rawValue: byte) else {
|
|
|
|
return (.internalError("Invalid message response: \(byte)"), nil)
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
let result = ClientState(keyResult: status)
|
|
|
|
guard data.count == Message.length + 1 else {
|
|
|
|
return (result, nil)
|
|
|
|
}
|
|
|
|
let messageData = Array(data.advanced(by: 1))
|
|
|
|
let message = Message(decodeFrom: messageData)
|
|
|
|
return (result, message)
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
|
2022-04-09 17:43:33 +02:00
|
|
|
private func fulfill(_ request: URLRequest) async -> Data? {
|
2022-01-29 18:59:42 +01:00
|
|
|
do {
|
|
|
|
let (data, response) = try await URLSession.shared.data(for: request)
|
|
|
|
guard let code = (response as? HTTPURLResponse)?.statusCode else {
|
|
|
|
print("No response from server")
|
2022-04-09 17:43:33 +02:00
|
|
|
return nil
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
guard code == 200 else {
|
|
|
|
print("Invalid server response \(code)")
|
2022-04-09 17:43:33 +02:00
|
|
|
return nil
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
2022-04-09 17:43:33 +02:00
|
|
|
return data
|
2022-01-29 18:59:42 +01:00
|
|
|
} catch {
|
|
|
|
print("Request failed: \(error)")
|
2022-04-09 17:43:33 +02:00
|
|
|
return nil
|
2022-01-29 18:59:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class NeverCacheDelegate: NSObject, NSURLConnectionDataDelegate {
|
|
|
|
|
|
|
|
func connection(_ connection: NSURLConnection, willCacheResponse cachedResponse: CachedURLResponse) -> CachedURLResponse? {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|