Compare commits

..

No commits in common. "master" and "0.1.0" have entirely different histories.

5 changed files with 25 additions and 47 deletions

1
.gitignore vendored
View File

@ -8,4 +8,3 @@ DerivedData/
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Package.resolved
.swiftpm/

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>

View File

@ -14,15 +14,14 @@ 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-API.git", branch: "main"),
.package(url: "https://github.com/apple/swift-crypto.git", "1.0.0" ..< "3.0.0")
],
targets: [
.target(
name: "Push",
dependencies: [
.product(name: "PushMessageDefinitions", package: "Push-Definitions"),
.product(name: "PushAPI", package: "Push-API"),
.product(name: "Crypto", package: "swift-crypto")
]),
]

View File

@ -1,18 +1,13 @@
import Foundation
import PushMessageDefinitions
import PushAPI
import SwiftUI
// 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.
*/
@ -81,8 +76,7 @@ public final class PushClient {
*/
public func getUnapprovedDevices(masterKey: String) async -> [DeviceRegistration]? {
let hash = hash(masterKey)
let request = AdminRequest(keyHash: hash, application: application)
guard let data = await post(.listUnapprovedDevices, body: request) else {
guard let data = await post(.listUnapprovedDevices, bodyData: hash) else {
return nil
}
do {
@ -145,12 +139,8 @@ 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) : (Data, URLResponse) = try await data(for: request)
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
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 {
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()
}
}
}

View File

@ -0,0 +1,11 @@
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!")
}
}