Compare commits
11 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
6f4988ecd0 | ||
|
4d18e331b6 | ||
|
39ef55f342 | ||
|
75d1772d0e | ||
|
e8c7147e2f | ||
|
9654da1d4f | ||
|
8fd1148cbd | ||
|
04dfb12052 | ||
|
f0589b9b90 | ||
|
1131cb17c6 | ||
|
c05643c6f1 |
@ -1,4 +1,4 @@
|
||||
// swift-tools-version: 5.5
|
||||
// swift-tools-version: 5.6
|
||||
|
||||
import PackageDescription
|
||||
|
||||
@ -14,15 +14,15 @@ let package = Package(
|
||||
targets: ["Push"]),
|
||||
],
|
||||
dependencies: [
|
||||
.package(url: "https://christophhagen.de/git/ch/Push-API.git", from: "0.7.2"),
|
||||
.package(url: "https://github.com/kylebrowning/APNSwift.git", from: "4.0.0"),
|
||||
.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://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
|
||||
],
|
||||
targets: [
|
||||
.target(
|
||||
name: "Push",
|
||||
dependencies: [
|
||||
.product(name: "PushAPI", package: "Push-API"),
|
||||
.product(name: "PushMessageDefinitions", package: "Push-Definitions"),
|
||||
.product(name: "Crypto", package: "swift-crypto")
|
||||
]),
|
||||
]
|
||||
|
@ -1,13 +1,18 @@
|
||||
import Foundation
|
||||
import PushAPI
|
||||
import SwiftUI
|
||||
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.
|
||||
*/
|
||||
@ -140,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
|
||||
}
|
||||
@ -156,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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +0,0 @@
|
||||
import XCTest
|
||||
@testable import Push_iOS
|
||||
|
||||
final class Push_iOSTests: XCTestCase {
|
||||
func testExample() throws {
|
||||
// This is an example of a functional test case.
|
||||
// Use XCTAssert and related functions to verify your tests produce the correct
|
||||
// results.
|
||||
XCTAssertEqual(Push_iOS().text, "Hello, World!")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user