7 Commits

Author SHA1 Message Date
4d18e331b6 Bump iOS version 2022-08-03 20:57:53 +02:00
39ef55f342 Prepare for new dependencies 2022-06-30 18:58:10 +02:00
75d1772d0e Improve availability check 2022-06-22 08:41:01 +02:00
e8c7147e2f Fix Linux networking 2022-06-09 16:27:37 +02:00
9654da1d4f Attempt fix 2022-06-09 16:11:29 +02:00
8fd1148cbd Reduce platform requirements 2022-06-09 16:00:21 +02:00
04dfb12052 Import network module on linux 2022-06-09 15:40:40 +02:00
2 changed files with 43 additions and 4 deletions

View File

@ -1,4 +1,4 @@
// swift-tools-version: 5.5
// swift-tools-version: 5.6
import PackageDescription
@ -14,8 +14,8 @@ let package = Package(
targets: ["Push"]),
],
dependencies: [
.package(url: "https://christophhagen.de/git/ch/Push-Definitions.git", from: "1.0.0"),
.package(url: "https://github.com/swift-server-community/APNSwift.git", from: "4.0.0"),
.package(url: "https://christophhagen.de/git/ch/Push-Definitions.git", from: "2.0.0-alpha.2"),
.package(url: "https://github.com/swift-server-community/APNSwift.git", from: "5.0.0-alpha.4"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
],
targets: [

View File

@ -1,12 +1,18 @@
import Foundation
import PushMessageDefinitions
// Use crypto replacement on Linux
#if canImport(CryptoKit)
import CryptoKit
#else
import Crypto
#endif
// Import network types on Linux
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
/**
A client to interact with a push server.
*/
@ -139,8 +145,12 @@ public final class PushClient {
var request = URLRequest(url: server.appendingPathComponent(route.rawValue))
request.httpBody = bodyData
request.httpMethod = "POST"
return await post(request)
}
private func post(_ request: URLRequest) async -> Data? {
do {
let (data, response) = try await URLSession.shared.data(for: request)
let (data, response) : (Data, URLResponse) = try await data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
return nil
}
@ -155,8 +165,37 @@ 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 {
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()
}
}
}