Compare commits

..

15 Commits

Author SHA1 Message Date
Christoph Hagen
6f4988ecd0 Move back to stable dependency 2022-08-04 22:10:44 +02:00
Christoph Hagen
4d18e331b6 Bump iOS version 2022-08-03 20:57:53 +02:00
Christoph Hagen
39ef55f342 Prepare for new dependencies 2022-06-30 18:58:10 +02:00
Christoph Hagen
75d1772d0e Improve availability check 2022-06-22 08:41:01 +02:00
Christoph Hagen
e8c7147e2f Fix Linux networking 2022-06-09 16:27:37 +02:00
Christoph Hagen
9654da1d4f Attempt fix 2022-06-09 16:11:29 +02:00
Christoph Hagen
8fd1148cbd Reduce platform requirements 2022-06-09 16:00:21 +02:00
Christoph Hagen
04dfb12052 Import network module on linux 2022-06-09 15:40:40 +02:00
Christoph Hagen
f0589b9b90 Remove unused import 2022-06-09 13:58:58 +02:00
Christoph Hagen
1131cb17c6 Update dependencies 2022-06-09 13:52:17 +02:00
Christoph Hagen
c05643c6f1 Remove test files 2022-06-09 13:52:08 +02:00
Christoph Hagen
fde2062507 Reduce swift tools version 2022-06-09 11:38:53 +02:00
Christoph Hagen
e4883f9b2b Update route 2022-06-07 17:21:47 +02:00
Christoph Hagen
910f7e5f91 Specify dependency version 2022-06-07 14:14:40 +02:00
Christoph Hagen
1092d776cd Ignore swift pm files 2022-06-07 14:11:18 +02:00
5 changed files with 47 additions and 25 deletions

1
.gitignore vendored
View File

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

View File

@ -1,8 +0,0 @@
<?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,14 +14,15 @@ let package = Package(
targets: ["Push"]),
],
dependencies: [
.package(url: "https://christophhagen.de/git/ch/Push-API.git", branch: "main"),
.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")
]),
]

View File

@ -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.
*/
@ -76,7 +81,8 @@ public final class PushClient {
*/
public func getUnapprovedDevices(masterKey: String) async -> [DeviceRegistration]? {
let hash = hash(masterKey)
guard let data = await post(.listUnapprovedDevices, bodyData: hash) else {
let request = AdminRequest(keyHash: hash, application: application)
guard let data = await post(.listUnapprovedDevices, body: request) else {
return nil
}
do {
@ -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()
}
}
}

View File

@ -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!")
}
}