Compare commits
No commits in common. "master" and "0.4.1" have entirely different histories.
@ -1,4 +1,4 @@
|
|||||||
// swift-tools-version: 5.6
|
// swift-tools-version: 5.5
|
||||||
|
|
||||||
import PackageDescription
|
import PackageDescription
|
||||||
|
|
||||||
|
@ -1,18 +1,12 @@
|
|||||||
import Foundation
|
import Foundation
|
||||||
import PushMessageDefinitions
|
import PushMessageDefinitions
|
||||||
|
|
||||||
// Use crypto replacement on Linux
|
|
||||||
#if canImport(CryptoKit)
|
#if canImport(CryptoKit)
|
||||||
import CryptoKit
|
import CryptoKit
|
||||||
#else
|
#else
|
||||||
import Crypto
|
import Crypto
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Import network types on Linux
|
|
||||||
#if canImport(FoundationNetworking)
|
|
||||||
import FoundationNetworking
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A client to interact with a push server.
|
A client to interact with a push server.
|
||||||
*/
|
*/
|
||||||
@ -145,12 +139,8 @@ public final class PushClient {
|
|||||||
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
|
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
|
||||||
request.httpBody = bodyData
|
request.httpBody = bodyData
|
||||||
request.httpMethod = "POST"
|
request.httpMethod = "POST"
|
||||||
return await post(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func post(_ request: URLRequest) async -> Data? {
|
|
||||||
do {
|
do {
|
||||||
let (data, response) : (Data, URLResponse) = try await data(for: request)
|
let (data, response) = try await URLSession.shared.data(for: request)
|
||||||
guard let httpResponse = response as? HTTPURLResponse else {
|
guard let httpResponse = response as? HTTPURLResponse else {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -165,37 +155,8 @@ public final class PushClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func data(for request: URLRequest) async throws -> (Data, URLResponse) {
|
|
||||||
#if !canImport(FoundationNetworking)
|
|
||||||
if #available(iOS 15.0, macOS 12.0, *) {
|
|
||||||
return try await URLSession.shared.data(for: request)
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
return try await URLSession.shared.dataRequest(request)
|
|
||||||
}
|
|
||||||
|
|
||||||
private func hash(_ masterKey: String) -> Data {
|
private func hash(_ masterKey: String) -> Data {
|
||||||
Data(SHA256.hash(data: masterKey.data(using: .utf8)!))
|
Data(SHA256.hash(data: masterKey.data(using: .utf8)!))
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension URLSession {
|
|
||||||
|
|
||||||
func dataRequest(_ request: URLRequest) async throws -> (Data, URLResponse) {
|
|
||||||
try await withCheckedThrowingContinuation { continuation in
|
|
||||||
dataTask(with: request) { data, response, error in
|
|
||||||
if let error = error {
|
|
||||||
print("Failed with error: \(error)")
|
|
||||||
continuation.resume(throwing: error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
guard let data = data, let response = response else {
|
|
||||||
continuation.resume(throwing: URLError(.unknown))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
continuation.resume(returning: (data, response))
|
|
||||||
}.resume()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user